user_registry.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef _USER_REGISTRY_H
  15. #define _USER_REGISTRY_H
  16. /// @file user_registry.h Defines the class, UserRegistry.
  17. #include <dhcp/hwaddr.h>
  18. #include <dhcp/duid.h>
  19. #include <exceptions/exceptions.h>
  20. #include <user.h>
  21. #include <user_data_source.h>
  22. #include <string>
  23. namespace user_chk {
  24. /// @brief Thrown UserRegistry encounters an error
  25. class UserRegistryError : public isc::Exception {
  26. public:
  27. UserRegistryError(const char* file, size_t line, const char* what) :
  28. isc::Exception(file, line, what)
  29. {}
  30. };
  31. /// @brief Defines a map of unique Users keyed by UserId.
  32. typedef std::map<UserId,UserPtr> UserMap;
  33. /// @brief Embodies an update-able, searchable list of unique users
  34. /// This class provides the means to create and maintain a searchable list
  35. /// of unique users. List entries are pointers to instances of User, keyed
  36. /// by their UserIds.
  37. /// Users may be added and removed from the list individually or the list
  38. /// may be updated by loading it from a data source, such as a file.
  39. class UserRegistry {
  40. public:
  41. /// @brief Constructor
  42. ///
  43. /// Creates a new registry with an empty list of users and no data source.
  44. UserRegistry();
  45. /// @brief Destructor
  46. ~UserRegistry();
  47. /// @brief Adds a given user to the registry.
  48. ///
  49. /// @param user A pointer to the user to add
  50. ///
  51. /// @throw UserRegistryError if the user is null or if the user already
  52. /// exists in the registry.
  53. void addUser(UserPtr& user);
  54. /// @brief Finds a user in the registry by user id
  55. ///
  56. /// @param id The user id for which to search
  57. ///
  58. /// @return A pointer to the user if found or an null pointer if not.
  59. const UserPtr& findUser(const UserId& id) const;
  60. /// @brief Removes a user from the registry by user id
  61. ///
  62. /// Removes the user entry if found, if not simply return.
  63. ///
  64. /// @param id The user id of the user to remove
  65. void removeUser(const UserId& id);
  66. /// @brief Finds a user in the registry by hardware address
  67. ///
  68. /// @param hwaddr The hardware address for which to search
  69. ///
  70. /// @return A pointer to the user if found or an null pointer if not.
  71. const UserPtr& findUser(const isc::dhcp::HWAddr& hwaddr) const;
  72. /// @brief Finds a user in the registry by DUID
  73. ///
  74. /// @param duid The DUID for which to search
  75. ///
  76. /// @return A pointer to the user if found or an null pointer if not.
  77. const UserPtr& findUser(const isc::dhcp::DUID& duid) const;
  78. /// @brief Updates the registry from its data source.
  79. ///
  80. /// This method will replace the contents of the registry with new content
  81. /// read from its data source. It will attempt to open the source and
  82. /// then add users from the source to the registry until the source is
  83. /// exhausted. If an error occurs accessing the source the registry
  84. /// contents will be restored to that of before the call to refresh.
  85. ///
  86. /// @throw UserRegistryError if the data source has not been set (is null)
  87. /// or if an error occurs accessing the data source.
  88. void refresh();
  89. /// @brief Removes all entries from the registry.
  90. void clearall();
  91. /// @brief Returns a reference to the data source.
  92. const UserDataSourcePtr& getSource();
  93. /// @brief Sets the data source to the given value.
  94. ///
  95. /// @param source reference to the data source to use.
  96. ///
  97. /// @throw UserRegistryError if new source value is null.
  98. void setSource(UserDataSourcePtr& source);
  99. private:
  100. /// @brief The registry of users.
  101. UserMap users_;
  102. /// @brief The current data source of users.
  103. UserDataSourcePtr source_;
  104. };
  105. /// @brief Define a smart pointer to a UserRegistry.
  106. typedef boost::shared_ptr<UserRegistry> UserRegistryPtr;
  107. } // namespace user_chk
  108. #endif