2020-02-17 02:59:56 +00:00
|
|
|
#include <Interpreters/InterpreterSetRoleQuery.h>
|
|
|
|
#include <Parsers/ASTSetRoleQuery.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ASTRolesOrUsersSet.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Access/RolesOrUsersSet.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <Access/AccessControlManager.h>
|
|
|
|
#include <Access/User.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SET_NON_GRANTED_ROLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterSetRoleQuery::execute()
|
|
|
|
{
|
|
|
|
const auto & query = query_ptr->as<const ASTSetRoleQuery &>();
|
|
|
|
if (query.kind == ASTSetRoleQuery::Kind::SET_DEFAULT_ROLE)
|
|
|
|
setDefaultRole(query);
|
|
|
|
else
|
|
|
|
setRole(query);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSetRoleQuery::setRole(const ASTSetRoleQuery & query)
|
|
|
|
{
|
|
|
|
auto & access_control = context.getAccessControlManager();
|
|
|
|
auto & session_context = context.getSessionContext();
|
|
|
|
auto user = session_context.getUser();
|
|
|
|
|
|
|
|
if (query.kind == ASTSetRoleQuery::Kind::SET_ROLE_DEFAULT)
|
|
|
|
{
|
|
|
|
session_context.setCurrentRolesDefault();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-30 20:10:45 +00:00
|
|
|
RolesOrUsersSet roles_from_query{*query.roles, access_control};
|
2020-04-29 19:35:56 +00:00
|
|
|
boost::container::flat_set<UUID> new_current_roles;
|
2020-02-17 02:59:56 +00:00
|
|
|
if (roles_from_query.all)
|
|
|
|
{
|
2020-04-20 22:07:00 +00:00
|
|
|
for (const auto & id : user->granted_roles.roles)
|
2020-02-17 02:59:56 +00:00
|
|
|
if (roles_from_query.match(id))
|
2020-04-29 19:35:56 +00:00
|
|
|
new_current_roles.emplace(id);
|
2020-02-17 02:59:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto & id : roles_from_query.getMatchingIDs())
|
|
|
|
{
|
2020-04-29 19:35:56 +00:00
|
|
|
if (!user->granted_roles.roles.count(id))
|
2020-02-17 02:59:56 +00:00
|
|
|
throw Exception("Role should be granted to set current", ErrorCodes::SET_NON_GRANTED_ROLE);
|
2020-04-29 19:35:56 +00:00
|
|
|
new_current_roles.emplace(id);
|
2020-02-17 02:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
session_context.setCurrentRoles(new_current_roles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSetRoleQuery::setDefaultRole(const ASTSetRoleQuery & query)
|
|
|
|
{
|
2020-05-29 06:54:27 +00:00
|
|
|
context.checkAccess(AccessType::ALTER_USER);
|
2020-02-17 02:59:56 +00:00
|
|
|
|
|
|
|
auto & access_control = context.getAccessControlManager();
|
2020-05-30 20:10:45 +00:00
|
|
|
std::vector<UUID> to_users = RolesOrUsersSet{*query.to_users, access_control, context.getUserID()}.getMatchingIDs(access_control);
|
|
|
|
RolesOrUsersSet roles_from_query{*query.roles, access_control};
|
2020-02-17 02:59:56 +00:00
|
|
|
|
|
|
|
auto update_func = [&](const AccessEntityPtr & entity) -> AccessEntityPtr
|
|
|
|
{
|
|
|
|
auto updated_user = typeid_cast<std::shared_ptr<User>>(entity->clone());
|
|
|
|
updateUserSetDefaultRoles(*updated_user, roles_from_query);
|
|
|
|
return updated_user;
|
|
|
|
};
|
|
|
|
|
|
|
|
access_control.update(to_users, update_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
void InterpreterSetRoleQuery::updateUserSetDefaultRoles(User & user, const RolesOrUsersSet & roles_from_query)
|
2020-02-17 02:59:56 +00:00
|
|
|
{
|
|
|
|
if (!roles_from_query.all)
|
|
|
|
{
|
|
|
|
for (const auto & id : roles_from_query.getMatchingIDs())
|
|
|
|
{
|
2020-04-29 19:35:56 +00:00
|
|
|
if (!user.granted_roles.roles.count(id))
|
2020-02-17 02:59:56 +00:00
|
|
|
throw Exception("Role should be granted to set default", ErrorCodes::SET_NON_GRANTED_ROLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.default_roles = roles_from_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|