1 | #include <Interpreters/UsersManager.h> |
2 | |
3 | #include <Common/Exception.h> |
4 | #include <Poco/Util/AbstractConfiguration.h> |
5 | |
6 | |
7 | namespace DB |
8 | { |
9 | |
10 | namespace ErrorCodes |
11 | { |
12 | extern const int UNKNOWN_USER; |
13 | } |
14 | |
15 | using UserPtr = UsersManager::UserPtr; |
16 | |
17 | void UsersManager::loadFromConfig(const Poco::Util::AbstractConfiguration & config) |
18 | { |
19 | Container new_users; |
20 | |
21 | Poco::Util::AbstractConfiguration::Keys config_keys; |
22 | config.keys("users" , config_keys); |
23 | |
24 | for (const std::string & key : config_keys) |
25 | { |
26 | auto user = std::make_shared<const User>(key, "users." + key, config); |
27 | new_users.emplace(key, std::move(user)); |
28 | } |
29 | |
30 | users = std::move(new_users); |
31 | } |
32 | |
33 | UserPtr UsersManager::authorizeAndGetUser( |
34 | const String & user_name, |
35 | const String & password, |
36 | const Poco::Net::IPAddress & address) const |
37 | { |
38 | auto it = users.find(user_name); |
39 | |
40 | if (users.end() == it) |
41 | throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER); |
42 | |
43 | it->second->allowed_client_hosts.checkContains(address, user_name); |
44 | it->second->authentication.checkPassword(password, user_name); |
45 | return it->second; |
46 | } |
47 | |
48 | UserPtr UsersManager::getUser(const String & user_name) const |
49 | { |
50 | auto it = users.find(user_name); |
51 | |
52 | if (users.end() == it) |
53 | throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER); |
54 | |
55 | return it->second; |
56 | } |
57 | |
58 | bool UsersManager::hasAccessToDatabase(const std::string & user_name, const std::string & database_name) const |
59 | { |
60 | auto it = users.find(user_name); |
61 | |
62 | if (users.end() == it) |
63 | throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER); |
64 | |
65 | auto user = it->second; |
66 | return !user->databases.has_value() || user->databases->count(database_name); |
67 | } |
68 | |
69 | bool UsersManager::hasAccessToDictionary(const std::string & user_name, const std::string & dictionary_name) const |
70 | { |
71 | auto it = users.find(user_name); |
72 | |
73 | if (users.end() == it) |
74 | throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER); |
75 | |
76 | auto user = it->second; |
77 | return !user->dictionaries.has_value() || user->dictionaries->count(dictionary_name); |
78 | } |
79 | } |
80 | |