2020-03-18 14:11:44 +00:00
|
|
|
#include <Interpreters/InterpreterCreateSettingsProfileQuery.h>
|
|
|
|
#include <Parsers/ASTCreateSettingsProfileQuery.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ASTRolesOrUsersSet.h>
|
2020-03-18 14:11:44 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-11-03 13:47:26 +00:00
|
|
|
#include <Interpreters/executeDDLQueryOnCluster.h>
|
2020-03-18 14:11:44 +00:00
|
|
|
#include <Access/AccessControlManager.h>
|
|
|
|
#include <Access/SettingsProfile.h>
|
|
|
|
#include <Access/AccessFlags.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void updateSettingsProfileFromQueryImpl(
|
|
|
|
SettingsProfile & profile,
|
|
|
|
const ASTCreateSettingsProfileQuery & query,
|
2020-05-30 14:18:08 +00:00
|
|
|
const String & override_name,
|
|
|
|
const std::optional<SettingsProfileElements> & override_settings,
|
2020-05-30 20:10:45 +00:00
|
|
|
const std::optional<RolesOrUsersSet> & override_to_roles)
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
if (!override_name.empty())
|
|
|
|
profile.setName(override_name);
|
|
|
|
else if (!query.new_name.empty())
|
|
|
|
profile.setName(query.new_name);
|
|
|
|
else if (query.names.size() == 1)
|
|
|
|
profile.setName(query.names.front());
|
|
|
|
|
|
|
|
if (override_settings)
|
|
|
|
profile.elements = *override_settings;
|
2020-03-18 14:11:44 +00:00
|
|
|
else if (query.settings)
|
2020-05-30 14:18:08 +00:00
|
|
|
profile.elements = *query.settings;
|
2020-03-18 14:11:44 +00:00
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
if (override_to_roles)
|
|
|
|
profile.to_roles = *override_to_roles;
|
2020-03-18 14:11:44 +00:00
|
|
|
else if (query.to_roles)
|
2020-05-30 14:18:08 +00:00
|
|
|
profile.to_roles = *query.to_roles;
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterCreateSettingsProfileQuery::execute()
|
|
|
|
{
|
2020-04-05 23:03:20 +00:00
|
|
|
auto & query = query_ptr->as<ASTCreateSettingsProfileQuery &>();
|
2021-04-10 23:33:54 +00:00
|
|
|
auto & access_control = getContext()->getAccessControlManager();
|
2020-03-18 14:11:44 +00:00
|
|
|
if (query.alter)
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext()->checkAccess(AccessType::ALTER_SETTINGS_PROFILE);
|
2020-03-18 14:11:44 +00:00
|
|
|
else
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext()->checkAccess(AccessType::CREATE_SETTINGS_PROFILE);
|
2020-03-18 14:11:44 +00:00
|
|
|
|
2020-04-05 23:03:20 +00:00
|
|
|
if (!query.cluster.empty())
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
query.replaceCurrentUserTag(getContext()->getUserName());
|
|
|
|
return executeDDLQueryOnCluster(query_ptr, getContext());
|
2020-04-05 23:03:20 +00:00
|
|
|
}
|
|
|
|
|
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-05-30 20:10:45 +00:00
|
|
|
std::optional<RolesOrUsersSet> roles_from_query;
|
2020-03-18 14:11:44 +00:00
|
|
|
if (query.to_roles)
|
2021-04-10 23:33:54 +00:00
|
|
|
roles_from_query = RolesOrUsersSet{*query.to_roles, access_control, getContext()->getUserID()};
|
2020-03-18 14:11:44 +00:00
|
|
|
|
|
|
|
if (query.alter)
|
|
|
|
{
|
|
|
|
auto update_func = [&](const AccessEntityPtr & entity) -> AccessEntityPtr
|
|
|
|
{
|
|
|
|
auto updated_profile = typeid_cast<std::shared_ptr<SettingsProfile>>(entity->clone());
|
2020-05-30 14:18:08 +00:00
|
|
|
updateSettingsProfileFromQueryImpl(*updated_profile, query, {}, settings_from_query, roles_from_query);
|
2020-03-18 14:11:44 +00:00
|
|
|
return updated_profile;
|
|
|
|
};
|
|
|
|
if (query.if_exists)
|
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
auto ids = access_control.find<SettingsProfile>(query.names);
|
|
|
|
access_control.tryUpdate(ids, update_func);
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
else
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.update(access_control.getIDs<SettingsProfile>(query.names), update_func);
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
std::vector<AccessEntityPtr> new_profiles;
|
|
|
|
for (const auto & name : query.names)
|
|
|
|
{
|
|
|
|
auto new_profile = std::make_shared<SettingsProfile>();
|
|
|
|
updateSettingsProfileFromQueryImpl(*new_profile, query, name, settings_from_query, roles_from_query);
|
|
|
|
new_profiles.emplace_back(std::move(new_profile));
|
|
|
|
}
|
2020-03-18 14:11:44 +00:00
|
|
|
|
|
|
|
if (query.if_not_exists)
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.tryInsert(new_profiles);
|
2020-03-18 14:11:44 +00:00
|
|
|
else if (query.or_replace)
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.insertOrReplace(new_profiles);
|
2020-03-18 14:11:44 +00:00
|
|
|
else
|
2020-05-30 14:18:08 +00:00
|
|
|
access_control.insert(new_profiles);
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterCreateSettingsProfileQuery::updateSettingsProfileFromQuery(SettingsProfile & SettingsProfile, const ASTCreateSettingsProfileQuery & query)
|
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
updateSettingsProfileFromQueryImpl(SettingsProfile, query, {}, {}, {});
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
}
|