2020-02-04 22:37:04 +00:00
|
|
|
#include <Interpreters/InterpreterCreateUserQuery.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <Interpreters/InterpreterSetRoleQuery.h>
|
2020-11-03 13:47:26 +00:00
|
|
|
#include <Interpreters/executeDDLQueryOnCluster.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <Parsers/ASTCreateUserQuery.h>
|
2020-05-30 14:18:08 +00:00
|
|
|
#include <Parsers/ASTUserNameWithHost.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ASTRolesOrUsersSet.h>
|
2020-02-04 22:37:04 +00:00
|
|
|
#include <Access/AccessControlManager.h>
|
|
|
|
#include <Access/User.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/ContextAccess.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <boost/range/algorithm/copy.hpp>
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
namespace
|
2020-02-17 02:59:56 +00:00
|
|
|
{
|
2020-03-18 14:11:44 +00:00
|
|
|
void updateUserFromQueryImpl(
|
|
|
|
User & user,
|
|
|
|
const ASTCreateUserQuery & query,
|
2020-05-30 14:18:08 +00:00
|
|
|
const std::shared_ptr<ASTUserNameWithHost> & override_name,
|
2020-05-30 20:10:45 +00:00
|
|
|
const std::optional<RolesOrUsersSet> & override_default_roles,
|
2020-05-30 14:18:08 +00:00
|
|
|
const std::optional<SettingsProfileElements> & override_settings)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
if (override_name)
|
|
|
|
user.setName(override_name->toString());
|
|
|
|
else if (!query.new_name.empty())
|
|
|
|
user.setName(query.new_name);
|
|
|
|
else if (query.names->size() == 1)
|
|
|
|
user.setName(query.names->front()->toString());
|
2020-02-21 19:27:12 +00:00
|
|
|
|
|
|
|
if (query.authentication)
|
|
|
|
user.authentication = *query.authentication;
|
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
if (override_name && !override_name->host_pattern.empty())
|
|
|
|
{
|
|
|
|
user.allowed_client_hosts = AllowedClientHosts{};
|
|
|
|
user.allowed_client_hosts.addLikePattern(override_name->host_pattern);
|
|
|
|
}
|
|
|
|
else if (query.hosts)
|
2020-02-21 19:27:12 +00:00
|
|
|
user.allowed_client_hosts = *query.hosts;
|
2020-05-30 14:18:08 +00:00
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
if (query.remove_hosts)
|
|
|
|
user.allowed_client_hosts.remove(*query.remove_hosts);
|
|
|
|
if (query.add_hosts)
|
|
|
|
user.allowed_client_hosts.add(*query.add_hosts);
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
auto set_default_roles = [&](const RolesOrUsersSet & default_roles_)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
if (!query.alter && !default_roles_.all)
|
|
|
|
user.granted_roles.grant(default_roles_.getMatchingIDs());
|
2020-02-21 19:27:12 +00:00
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
InterpreterSetRoleQuery::updateUserSetDefaultRoles(user, default_roles_);
|
|
|
|
};
|
2020-03-18 14:11:44 +00:00
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
if (override_default_roles)
|
|
|
|
set_default_roles(*override_default_roles);
|
|
|
|
else if (query.default_roles)
|
|
|
|
set_default_roles(*query.default_roles);
|
2020-03-18 14:11:44 +00:00
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
if (override_settings)
|
|
|
|
user.settings = *override_settings;
|
|
|
|
else if (query.settings)
|
|
|
|
user.settings = *query.settings;
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
2020-02-17 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
BlockIO InterpreterCreateUserQuery::execute()
|
|
|
|
{
|
2020-04-22 06:01:33 +00:00
|
|
|
const auto & query = query_ptr->as<const ASTCreateUserQuery &>();
|
2020-02-04 22:37:04 +00:00
|
|
|
auto & access_control = context.getAccessControlManager();
|
2020-03-07 17:37:38 +00:00
|
|
|
auto access = context.getAccess();
|
|
|
|
access->checkAccess(query.alter ? AccessType::ALTER_USER : AccessType::CREATE_USER);
|
2020-02-04 22:37:04 +00:00
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
std::optional<RolesOrUsersSet> default_roles_from_query;
|
2020-02-17 02:59:56 +00:00
|
|
|
if (query.default_roles)
|
|
|
|
{
|
2020-05-30 20:10:45 +00:00
|
|
|
default_roles_from_query = RolesOrUsersSet{*query.default_roles, access_control};
|
2020-02-17 02:59:56 +00:00
|
|
|
if (!query.alter && !default_roles_from_query->all)
|
|
|
|
{
|
|
|
|
for (const UUID & role : default_roles_from_query->getMatchingIDs())
|
2020-03-07 17:37:38 +00:00
|
|
|
access->checkAdminOption(role);
|
2020-02-17 02:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:03:20 +00:00
|
|
|
if (!query.cluster.empty())
|
|
|
|
return executeDDLQueryOnCluster(query_ptr, context);
|
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
std::optional<SettingsProfileElements> settings_from_query;
|
|
|
|
if (query.settings)
|
|
|
|
settings_from_query = SettingsProfileElements{*query.settings, access_control};
|
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
if (query.alter)
|
|
|
|
{
|
|
|
|
auto update_func = [&](const AccessEntityPtr & entity) -> AccessEntityPtr
|
|
|
|
{
|
|
|
|
auto updated_user = typeid_cast<std::shared_ptr<User>>(entity->clone());
|
2020-05-30 14:18:08 +00:00
|
|
|
updateUserFromQueryImpl(*updated_user, query, {}, default_roles_from_query, settings_from_query);
|
2020-02-04 22:37:04 +00:00
|
|
|
return updated_user;
|
|
|
|
};
|
2020-05-30 14:18:08 +00:00
|
|
|
Strings names = query.names->toStrings();
|
2020-02-04 22:37:04 +00:00
|
|
|
if (query.if_exists)
|
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
auto ids = access_control.find<User>(names);
|
|
|
|
access_control.tryUpdate(ids, update_func);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
else
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.update(access_control.getIDs<User>(names), update_func);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
std::vector<AccessEntityPtr> new_users;
|
|
|
|
for (const auto & name : *query.names)
|
|
|
|
{
|
|
|
|
auto new_user = std::make_shared<User>();
|
|
|
|
updateUserFromQueryImpl(*new_user, query, name, default_roles_from_query, settings_from_query);
|
|
|
|
new_users.emplace_back(std::move(new_user));
|
|
|
|
}
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
if (query.if_not_exists)
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.tryInsert(new_users);
|
2020-02-04 22:37:04 +00:00
|
|
|
else if (query.or_replace)
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.insertOrReplace(new_users);
|
2020-02-04 22:37:04 +00:00
|
|
|
else
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.insert(new_users);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
void InterpreterCreateUserQuery::updateUserFromQuery(User & user, const ASTCreateUserQuery & query)
|
2020-02-04 22:37:04 +00:00
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
updateUserFromQueryImpl(user, query, {}, {}, {});
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
2020-02-17 02:59:56 +00:00
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|