2019-03-29 20:31:06 +00:00
|
|
|
#include <Interpreters/UsersManager.h>
|
|
|
|
|
2017-11-27 21:31:13 +00:00
|
|
|
#include <Common/Exception.h>
|
2019-07-28 13:12:26 +00:00
|
|
|
#include <Poco/Net/IPAddress.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2019-03-22 11:18:24 +00:00
|
|
|
|
2017-11-27 21:31:13 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_USER;
|
|
|
|
extern const int IP_ADDRESS_NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:31:06 +00:00
|
|
|
using UserPtr = UsersManager::UserPtr;
|
2017-11-30 14:53:12 +00:00
|
|
|
|
2019-03-29 20:31:06 +00:00
|
|
|
void UsersManager::loadFromConfig(const Poco::Util::AbstractConfiguration & config)
|
2017-11-27 21:31:13 +00:00
|
|
|
{
|
|
|
|
Container new_users;
|
|
|
|
|
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
|
|
|
config.keys("users", config_keys);
|
|
|
|
|
|
|
|
for (const std::string & key : config_keys)
|
2017-11-30 14:53:12 +00:00
|
|
|
{
|
|
|
|
auto user = std::make_shared<const User>(key, "users." + key, config);
|
|
|
|
new_users.emplace(key, std::move(user));
|
|
|
|
}
|
2017-11-27 21:31:13 +00:00
|
|
|
|
|
|
|
users = std::move(new_users);
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:31:06 +00:00
|
|
|
UserPtr UsersManager::authorizeAndGetUser(
|
2017-11-27 21:31:13 +00:00
|
|
|
const String & user_name,
|
|
|
|
const String & password,
|
|
|
|
const Poco::Net::IPAddress & address) const
|
|
|
|
{
|
|
|
|
auto it = users.find(user_name);
|
|
|
|
|
|
|
|
if (users.end() == it)
|
|
|
|
throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER);
|
|
|
|
|
2017-11-30 14:53:12 +00:00
|
|
|
if (!it->second->addresses.contains(address))
|
2017-11-27 21:31:13 +00:00
|
|
|
throw Exception("User " + user_name + " is not allowed to connect from address " + address.toString(), ErrorCodes::IP_ADDRESS_NOT_ALLOWED);
|
|
|
|
|
2019-10-08 13:44:44 +00:00
|
|
|
it->second->authentication.checkPassword(password, user_name);
|
2017-11-27 21:31:13 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:31:06 +00:00
|
|
|
UserPtr UsersManager::getUser(const String & user_name) const
|
2017-11-27 21:31:13 +00:00
|
|
|
{
|
|
|
|
auto it = users.find(user_name);
|
|
|
|
|
|
|
|
if (users.end() == it)
|
|
|
|
throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER);
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:31:06 +00:00
|
|
|
bool UsersManager::hasAccessToDatabase(const std::string & user_name, const std::string & database_name) const
|
2017-11-27 21:31:13 +00:00
|
|
|
{
|
|
|
|
auto it = users.find(user_name);
|
|
|
|
|
|
|
|
if (users.end() == it)
|
|
|
|
throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER);
|
|
|
|
|
2017-11-30 14:53:12 +00:00
|
|
|
auto user = it->second;
|
|
|
|
return user->databases.empty() || user->databases.count(database_name);
|
2017-11-27 21:31:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 08:22:34 +00:00
|
|
|
bool UsersManager::hasAccessToDictionary(const std::string & user_name, const std::string & dictionary_name) const
|
|
|
|
{
|
|
|
|
auto it = users.find(user_name);
|
|
|
|
|
|
|
|
if (users.end() == it)
|
|
|
|
throw Exception("Unknown user " + user_name, ErrorCodes::UNKNOWN_USER);
|
|
|
|
|
|
|
|
auto user = it->second;
|
|
|
|
return user->dictionaries.empty() || user->dictionaries.count(dictionary_name);
|
|
|
|
}
|
2017-11-27 21:31:13 +00:00
|
|
|
}
|