2020-02-17 02:59:56 +00:00
|
|
|
#include <Interpreters/InterpreterSetRoleQuery.h>
|
|
|
|
#include <Parsers/ASTSetRoleQuery.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Parsers/ASTExtendedRoleSet.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/ExtendedRoleSet.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-03-07 17:37:38 +00:00
|
|
|
ExtendedRoleSet roles_from_query{*query.roles, access_control};
|
2020-02-17 02:59:56 +00:00
|
|
|
std::vector<UUID> new_current_roles;
|
|
|
|
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))
|
|
|
|
new_current_roles.push_back(id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto & id : roles_from_query.getMatchingIDs())
|
|
|
|
{
|
2020-04-20 22:07:00 +00:00
|
|
|
if (!user->granted_roles.roles.contains(id))
|
2020-02-17 02:59:56 +00:00
|
|
|
throw Exception("Role should be granted to set current", ErrorCodes::SET_NON_GRANTED_ROLE);
|
|
|
|
new_current_roles.push_back(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session_context.setCurrentRoles(new_current_roles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSetRoleQuery::setDefaultRole(const ASTSetRoleQuery & query)
|
|
|
|
{
|
|
|
|
context.checkAccess(AccessType::CREATE_USER | AccessType::DROP_USER);
|
|
|
|
|
|
|
|
auto & access_control = context.getAccessControlManager();
|
2020-03-07 17:37:38 +00:00
|
|
|
std::vector<UUID> to_users = ExtendedRoleSet{*query.to_users, access_control, context.getUserID()}.getMatchingIDs(access_control);
|
|
|
|
ExtendedRoleSet 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-03-07 17:37:38 +00:00
|
|
|
void InterpreterSetRoleQuery::updateUserSetDefaultRoles(User & user, const ExtendedRoleSet & 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-20 22:07:00 +00:00
|
|
|
if (!user.granted_roles.roles.contains(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|