user_registry.cc 2.8 KB
Newer Older
1
// Copyright (C) 2013-2015,2017 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// 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/.
6 7 8 9

#include <user_registry.h>
#include <user.h>

10 11
namespace user_chk {

12 13 14 15 16 17
UserRegistry::UserRegistry() {
}

UserRegistry::~UserRegistry(){
}

18
void
19 20
UserRegistry::addUser(UserPtr& user) {
    if (!user) {
21
        isc_throw (UserRegistryError, "UserRegistry cannot add blank user");
22 23
    }

24 25 26 27
    UserPtr found_user;
    if ((found_user = findUser(user->getUserId()))) {
        isc_throw (UserRegistryError, "UserRegistry duplicate user: "
                   << user->getUserId());
28 29 30 31 32
    }

    users_[user->getUserId()] = user;
}

33
const UserPtr&
34 35 36 37
UserRegistry::findUser(const UserId& id) const {
    static UserPtr empty;
    UserMap::const_iterator it = users_.find(id);
    if (it != users_.end()) {
38
        const UserPtr tmp = (*it).second;
39 40 41 42 43 44
        return ((*it).second);
    }

    return empty;
}

45
void
46 47 48 49 50 51 52 53
UserRegistry::removeUser(const UserId& id) {
    static UserPtr empty;
    UserMap::iterator it = users_.find(id);
    if (it != users_.end()) {
        users_.erase(it);
    }
}

54
const UserPtr&
55 56 57 58 59
UserRegistry::findUser(const isc::dhcp::HWAddr& hwaddr) const {
    UserId id(UserId::HW_ADDRESS, hwaddr.hwaddr_);
    return (findUser(id));
}

60
const UserPtr&
61 62 63 64 65 66
UserRegistry::findUser(const isc::dhcp::DUID& duid) const {
    UserId id(UserId::DUID, duid.getDuid());
    return (findUser(id));
}

void UserRegistry::refresh() {
67 68 69 70 71
    if (!source_) {
        isc_throw(UserRegistryError,
                  "UserRegistry: cannot refresh, no data source");
    }

72
    // If the source isn't open, open it.
73 74 75 76
    if (!source_->isOpen()) {
        source_->open();
    }

77 78 79 80
    // Make a copy in case something goes wrong midstream.
    UserMap backup(users_);

    // Empty the registry then read users from source until source is empty.
81 82 83 84 85 86 87
    clearall();
    try {
        UserPtr user;
        while ((user = source_->readNextUser())) {
            addUser(user);
        }
    } catch (const std::exception& ex) {
88
        // Source was compromised so restore registry from backup.
89 90
        users_ = backup;
        // Close the source.
91 92 93 94 95
        source_->close();
        isc_throw (UserRegistryError, "UserRegistry: refresh failed during read"
                   << ex.what());
    }

96
    // Close the source.
97
    source_->close();
98 99 100 101 102 103 104
}

void UserRegistry::clearall() {
    users_.clear();
}

void UserRegistry::setSource(UserDataSourcePtr& source) {
105 106 107 108 109
    if (!source) {
        isc_throw (UserRegistryError,
                   "UserRegistry: data source cannot be set to null");
    }

110 111 112 113 114 115 116
    source_ = source;
}

const UserDataSourcePtr& UserRegistry::getSource() {
    return (source_);
}

117
} // namespace user_chk