2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/EnabledRoles.h>
|
|
|
|
#include <Access/Role.h>
|
|
|
|
#include <Access/EnabledRolesInfo.h>
|
|
|
|
#include <boost/range/algorithm/copy.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
EnabledRoles::EnabledRoles(const Params & params_) : params(params_), handlers(std::make_shared<Handlers>())
|
2020-03-07 17:37:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EnabledRoles::~EnabledRoles() = default;
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<const EnabledRolesInfo> EnabledRoles::getRolesInfo() const
|
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
std::lock_guard lock{info_mutex};
|
2020-03-07 17:37:38 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
scope_guard EnabledRoles::subscribeForChanges(const OnChangeHandler & handler) const
|
2020-03-07 17:37:38 +00:00
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
std::lock_guard lock{handlers->mutex};
|
|
|
|
handlers->list.push_back(handler);
|
|
|
|
auto it = std::prev(handlers->list.end());
|
2020-03-07 17:37:38 +00:00
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
return [handlers=handlers, it]
|
2020-03-07 17:37:38 +00:00
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
std::lock_guard lock2{handlers->mutex};
|
|
|
|
handlers->list.erase(it);
|
2020-03-07 17:37:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
void EnabledRoles::setRolesInfo(const std::shared_ptr<const EnabledRolesInfo> & info_, scope_guard * notifications)
|
2020-03-07 17:37:38 +00:00
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock{info_mutex};
|
|
|
|
if (info && info_ && *info == *info_)
|
|
|
|
return;
|
2020-10-12 15:39:39 +00:00
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
info = info_;
|
|
|
|
}
|
2020-10-12 15:39:39 +00:00
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
if (notifications)
|
2020-10-12 15:39:39 +00:00
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
std::vector<OnChangeHandler> handlers_to_notify;
|
|
|
|
{
|
|
|
|
std::lock_guard lock{handlers->mutex};
|
|
|
|
boost::range::copy(handlers->list, std::back_inserter(handlers_to_notify));
|
|
|
|
}
|
|
|
|
|
|
|
|
notifications->join(scope_guard(
|
|
|
|
[info = info, handlers_to_notify = std::move(handlers_to_notify)]
|
|
|
|
{
|
|
|
|
for (const auto & handler : handlers_to_notify)
|
|
|
|
handler(info);
|
|
|
|
}));
|
|
|
|
}
|
2020-03-07 17:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|