2020-03-04 22:27:03 +00:00
|
|
|
#include <Access/SettingsProfilesCache.h>
|
2021-11-02 11:06:20 +00:00
|
|
|
#include <Access/AccessControl.h>
|
2020-03-04 22:27:03 +00:00
|
|
|
#include <Access/SettingsProfile.h>
|
2021-07-22 16:07:03 +00:00
|
|
|
#include <Access/SettingsProfilesInfo.h>
|
2020-03-04 22:27:03 +00:00
|
|
|
#include <Common/quoteString.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int THERE_IS_NO_PROFILE;
|
|
|
|
}
|
|
|
|
|
2021-11-02 11:06:20 +00:00
|
|
|
SettingsProfilesCache::SettingsProfilesCache(const AccessControl & access_control_)
|
|
|
|
: access_control(access_control_) {}
|
2020-03-04 22:27:03 +00:00
|
|
|
|
|
|
|
SettingsProfilesCache::~SettingsProfilesCache() = default;
|
|
|
|
|
|
|
|
|
|
|
|
void SettingsProfilesCache::ensureAllProfilesRead()
|
|
|
|
{
|
|
|
|
/// `mutex` is already locked.
|
|
|
|
if (all_profiles_read)
|
|
|
|
return;
|
|
|
|
all_profiles_read = true;
|
|
|
|
|
2021-11-02 11:06:20 +00:00
|
|
|
subscription = access_control.subscribeForChanges<SettingsProfile>(
|
2020-03-04 22:27:03 +00:00
|
|
|
[&](const UUID & id, const AccessEntityPtr & entity)
|
|
|
|
{
|
|
|
|
if (entity)
|
|
|
|
profileAddedOrChanged(id, typeid_cast<SettingsProfilePtr>(entity));
|
|
|
|
else
|
|
|
|
profileRemoved(id);
|
|
|
|
});
|
|
|
|
|
2021-11-02 11:06:20 +00:00
|
|
|
for (const UUID & id : access_control.findAll<SettingsProfile>())
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
2021-11-02 11:06:20 +00:00
|
|
|
auto profile = access_control.tryRead<SettingsProfile>(id);
|
2020-03-04 22:27:03 +00:00
|
|
|
if (profile)
|
|
|
|
{
|
|
|
|
all_profiles.emplace(id, profile);
|
|
|
|
profiles_by_name[profile->getName()] = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SettingsProfilesCache::profileAddedOrChanged(const UUID & profile_id, const SettingsProfilePtr & new_profile)
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
auto it = all_profiles.find(profile_id);
|
|
|
|
if (it == all_profiles.end())
|
|
|
|
{
|
|
|
|
all_profiles.emplace(profile_id, new_profile);
|
|
|
|
profiles_by_name[new_profile->getName()] = profile_id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto old_profile = it->second;
|
|
|
|
it->second = new_profile;
|
|
|
|
if (old_profile->getName() != new_profile->getName())
|
|
|
|
profiles_by_name.erase(old_profile->getName());
|
|
|
|
profiles_by_name[new_profile->getName()] = profile_id;
|
|
|
|
}
|
2021-07-22 16:07:03 +00:00
|
|
|
profile_infos_cache.clear();
|
2020-03-04 22:27:03 +00:00
|
|
|
mergeSettingsAndConstraints();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SettingsProfilesCache::profileRemoved(const UUID & profile_id)
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
auto it = all_profiles.find(profile_id);
|
|
|
|
if (it == all_profiles.end())
|
|
|
|
return;
|
|
|
|
profiles_by_name.erase(it->second->getName());
|
|
|
|
all_profiles.erase(it);
|
2021-07-22 16:07:03 +00:00
|
|
|
profile_infos_cache.clear();
|
2020-03-04 22:27:03 +00:00
|
|
|
mergeSettingsAndConstraints();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SettingsProfilesCache::setDefaultProfileName(const String & default_profile_name)
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ensureAllProfilesRead();
|
|
|
|
|
|
|
|
if (default_profile_name.empty())
|
|
|
|
{
|
|
|
|
default_profile_id = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = profiles_by_name.find(default_profile_name);
|
|
|
|
if (it == profiles_by_name.end())
|
|
|
|
throw Exception("Settings profile " + backQuote(default_profile_name) + " not found", ErrorCodes::THERE_IS_NO_PROFILE);
|
|
|
|
|
|
|
|
default_profile_id = it->second;
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:39:18 +00:00
|
|
|
|
2020-03-04 22:27:03 +00:00
|
|
|
void SettingsProfilesCache::mergeSettingsAndConstraints()
|
|
|
|
{
|
|
|
|
/// `mutex` is already locked.
|
2020-04-16 12:31:57 +00:00
|
|
|
for (auto i = enabled_settings.begin(), e = enabled_settings.end(); i != e;)
|
|
|
|
{
|
|
|
|
auto enabled = i->second.lock();
|
|
|
|
if (!enabled)
|
|
|
|
i = enabled_settings.erase(i);
|
|
|
|
else
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
|
|
|
mergeSettingsAndConstraintsFor(*enabled);
|
2020-04-16 12:31:57 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
|
2020-03-04 22:27:03 +00:00
|
|
|
void SettingsProfilesCache::mergeSettingsAndConstraintsFor(EnabledSettings & enabled) const
|
|
|
|
{
|
|
|
|
SettingsProfileElements merged_settings;
|
|
|
|
if (default_profile_id)
|
|
|
|
{
|
|
|
|
SettingsProfileElement new_element;
|
|
|
|
new_element.parent_profile = *default_profile_id;
|
|
|
|
merged_settings.emplace_back(new_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & [profile_id, profile] : all_profiles)
|
|
|
|
if (profile->to_roles.match(enabled.params.user_id, enabled.params.enabled_roles))
|
|
|
|
{
|
|
|
|
SettingsProfileElement new_element;
|
|
|
|
new_element.parent_profile = profile_id;
|
|
|
|
merged_settings.emplace_back(new_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
merged_settings.merge(enabled.params.settings_from_enabled_roles);
|
|
|
|
merged_settings.merge(enabled.params.settings_from_user);
|
|
|
|
|
2021-11-02 11:06:20 +00:00
|
|
|
auto info = std::make_shared<SettingsProfilesInfo>(access_control);
|
2021-07-22 16:07:03 +00:00
|
|
|
info->profiles = enabled.params.settings_from_user.toProfileIDs();
|
|
|
|
substituteProfiles(merged_settings, info->profiles_with_implicit, info->names_of_profiles);
|
|
|
|
info->settings = merged_settings.toSettingsChanges();
|
2021-11-02 11:06:20 +00:00
|
|
|
info->constraints = merged_settings.toSettingsConstraints(access_control);
|
2020-03-04 22:27:03 +00:00
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
enabled.setInfo(std::move(info));
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
void SettingsProfilesCache::substituteProfiles(
|
|
|
|
SettingsProfileElements & elements,
|
|
|
|
std::vector<UUID> & substituted_profiles,
|
|
|
|
std::unordered_map<UUID, String> & names_of_substituted_profiles) const
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
2021-07-19 09:04:04 +00:00
|
|
|
/// We should substitute profiles in reversive order because the same profile can occur
|
|
|
|
/// in `elements` multiple times (with some other settings in between) and in this case
|
|
|
|
/// the last occurrence should override all the previous ones.
|
2021-07-22 16:07:03 +00:00
|
|
|
boost::container::flat_set<UUID> substituted_profiles_set;
|
2021-07-19 09:04:04 +00:00
|
|
|
size_t i = elements.size();
|
|
|
|
while (i != 0)
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
2021-07-19 09:04:04 +00:00
|
|
|
auto & element = elements[--i];
|
2020-05-12 20:31:30 +00:00
|
|
|
if (!element.parent_profile)
|
|
|
|
continue;
|
|
|
|
|
2021-07-19 09:04:04 +00:00
|
|
|
auto profile_id = *element.parent_profile;
|
2020-05-12 20:31:30 +00:00
|
|
|
element.parent_profile.reset();
|
2021-07-22 16:07:03 +00:00
|
|
|
if (substituted_profiles_set.count(profile_id))
|
2020-05-12 20:31:30 +00:00
|
|
|
continue;
|
|
|
|
|
2021-07-19 09:04:04 +00:00
|
|
|
auto profile_it = all_profiles.find(profile_id);
|
|
|
|
if (profile_it == all_profiles.end())
|
2020-05-12 20:31:30 +00:00
|
|
|
continue;
|
|
|
|
|
2021-07-19 09:04:04 +00:00
|
|
|
const auto & profile = profile_it->second;
|
|
|
|
const auto & profile_elements = profile->elements;
|
|
|
|
elements.insert(elements.begin() + i, profile_elements.begin(), profile_elements.end());
|
|
|
|
i += profile_elements.size();
|
2021-07-22 16:07:03 +00:00
|
|
|
substituted_profiles.push_back(profile_id);
|
|
|
|
substituted_profiles_set.insert(profile_id);
|
|
|
|
names_of_substituted_profiles.emplace(profile_id, profile->getName());
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
2021-07-22 16:07:03 +00:00
|
|
|
std::reverse(substituted_profiles.begin(), substituted_profiles.end());
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const EnabledSettings> SettingsProfilesCache::getEnabledSettings(
|
|
|
|
const UUID & user_id,
|
|
|
|
const SettingsProfileElements & settings_from_user,
|
2020-04-29 19:35:56 +00:00
|
|
|
const boost::container::flat_set<UUID> & enabled_roles,
|
2020-03-04 22:27:03 +00:00
|
|
|
const SettingsProfileElements & settings_from_enabled_roles)
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ensureAllProfilesRead();
|
|
|
|
|
|
|
|
EnabledSettings::Params params;
|
|
|
|
params.user_id = user_id;
|
|
|
|
params.settings_from_user = settings_from_user;
|
|
|
|
params.enabled_roles = enabled_roles;
|
|
|
|
params.settings_from_enabled_roles = settings_from_enabled_roles;
|
|
|
|
|
|
|
|
auto it = enabled_settings.find(params);
|
|
|
|
if (it != enabled_settings.end())
|
|
|
|
{
|
|
|
|
auto from_cache = it->second.lock();
|
|
|
|
if (from_cache)
|
|
|
|
return from_cache;
|
|
|
|
enabled_settings.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<EnabledSettings> res(new EnabledSettings(params));
|
|
|
|
enabled_settings.emplace(std::move(params), res);
|
|
|
|
mergeSettingsAndConstraintsFor(*res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
std::shared_ptr<const SettingsProfilesInfo> SettingsProfilesCache::getSettingsProfileInfo(const UUID & profile_id)
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ensureAllProfilesRead();
|
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
if (auto pos = this->profile_infos_cache.get(profile_id))
|
|
|
|
return *pos;
|
2020-03-04 22:27:03 +00:00
|
|
|
|
|
|
|
SettingsProfileElements elements = all_profiles[profile_id]->elements;
|
|
|
|
|
2021-11-02 11:06:20 +00:00
|
|
|
auto info = std::make_shared<SettingsProfilesInfo>(access_control);
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
info->profiles.push_back(profile_id);
|
|
|
|
info->profiles_with_implicit.push_back(profile_id);
|
|
|
|
substituteProfiles(elements, info->profiles_with_implicit, info->names_of_profiles);
|
|
|
|
info->settings = elements.toSettingsChanges();
|
2021-11-02 11:06:20 +00:00
|
|
|
info->constraints.merge(elements.toSettingsConstraints(access_control));
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
profile_infos_cache.add(profile_id, info);
|
|
|
|
return info;
|
|
|
|
}
|
2020-03-04 22:27:03 +00:00
|
|
|
|
|
|
|
}
|