summaryrefslogtreecommitdiffstats
path: root/src/hooks/dhcp/user_chk/user_registry.h
blob: d99c0c7262bd0fa48d19964ba70a6a9728d4d1af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef _USER_REGISTRY_H
#define _USER_REGISTRY_H

/// @file user_registry.h Defines the class, UserRegistry.

#include <dhcp/hwaddr.h>
#include <dhcp/duid.h>
#include <exceptions/exceptions.h>
#include <user.h>
#include <user_data_source.h>

#include <string>

namespace user_chk {

/// @brief Thrown UserRegistry encounters an error
class UserRegistryError : public isc::Exception {
public:
    UserRegistryError(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what)
    {}
};

/// @brief Defines a map of unique Users keyed by UserId.
typedef std::map<UserId,UserPtr> UserMap;

/// @brief Embodies an update-able, searchable list of unique users
/// This class provides the means to create and maintain a searchable list
/// of unique users. List entries are pointers to instances of User, keyed
/// by their UserIds.
/// Users may be added and removed from the list individually or the list
/// may be updated by loading it from a data source, such as a file.
class UserRegistry {
public:
    /// @brief Constructor
    ///
    /// Creates a new registry with an empty list of users and no data source.
    UserRegistry();

    /// @brief Destructor
    ~UserRegistry();

    /// @brief Adds a given user to the registry.
    ///
    /// @param user A pointer to the user to add
    ///
    /// @throw UserRegistryError if the user is null or if the user already
    /// exists in the registry.
    void addUser(UserPtr& user);

    /// @brief Finds a user in the registry by user id
    ///
    /// @param id The user id for which to search
    ///
    /// @return A pointer to the user if found or an null pointer if not.
    const UserPtr& findUser(const UserId& id) const;

    /// @brief Removes a user from the registry by user id
    ///
    /// Removes the user entry if found, if not simply return.
    ///
    /// @param id The user id of the user to remove
    void removeUser(const UserId&  id);

    /// @brief Finds a user in the registry by hardware address
    ///
    /// @param hwaddr The hardware address for which to search
    ///
    /// @return A pointer to the user if found or an null pointer if not.
    const UserPtr& findUser(const isc::dhcp::HWAddr& hwaddr) const;

    /// @brief Finds a user in the registry by DUID
    ///
    /// @param duid The DUID for which to search
    ///
    /// @return A pointer to the user if found or an null pointer if not.
    const UserPtr& findUser(const isc::dhcp::DUID& duid) const;

    /// @brief Updates the registry from its data source.
    ///
    /// This method will replace the contents of the registry with new content
    /// read from its data source.  It will attempt to open the source and
    /// then add users from the source to the registry until the source is
    /// exhausted.  If an error occurs accessing the source the registry
    /// contents will be restored to that of before the call to refresh.
    ///
    /// @throw UserRegistryError if the data source has not been set (is null)
    /// or if an error occurs accessing the data source.
    void refresh();

    /// @brief Removes all entries from the registry.
    void clearall();

    /// @brief Returns a reference to the data source.
    const UserDataSourcePtr& getSource();

    /// @brief Sets the data source to the given value.
    ///
    /// @param source reference to the data source to use.
    ///
    /// @throw UserRegistryError if new source value is null.
    void setSource(UserDataSourcePtr& source);

private:
    /// @brief The registry of users.
    UserMap users_;

    /// @brief The current data source of users.
    UserDataSourcePtr source_;
};

/// @brief Define a smart pointer to a UserRegistry.
typedef boost::shared_ptr<UserRegistry> UserRegistryPtr;

} // namespace user_chk

#endif