ClickHouse/src/Access/EnabledRoles.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.6 KiB
C++
Raw Normal View History

#include <Access/EnabledRoles.h>
#include <Access/Role.h>
#include <Access/EnabledRolesInfo.h>
#include <boost/range/algorithm/copy.hpp>
namespace DB
{
EnabledRoles::EnabledRoles(const Params & params_) : params(params_), handlers(std::make_shared<Handlers>())
{
}
EnabledRoles::~EnabledRoles() = default;
std::shared_ptr<const EnabledRolesInfo> EnabledRoles::getRolesInfo() const
{
std::lock_guard lock{info_mutex};
return info;
}
2021-06-15 19:55:21 +00:00
scope_guard EnabledRoles::subscribeForChanges(const OnChangeHandler & handler) const
{
std::lock_guard lock{handlers->mutex};
handlers->list.push_back(handler);
auto it = std::prev(handlers->list.end());
2023-05-13 00:57:31 +00:00
return [my_handlers = handlers, it]
{
2023-05-13 00:57:31 +00:00
std::lock_guard lock2{my_handlers->mutex};
my_handlers->list.erase(it);
};
}
void EnabledRoles::setRolesInfo(const std::shared_ptr<const EnabledRolesInfo> & info_, scope_guard * notifications)
{
{
std::lock_guard lock{info_mutex};
if (info && info_ && *info == *info_)
return;
2020-10-12 15:39:39 +00:00
info = info_;
}
2020-10-12 15:39:39 +00:00
if (notifications)
2020-10-12 15:39:39 +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(
2023-05-13 00:57:31 +00:00
[my_info = info, my_handlers_to_notify = std::move(handlers_to_notify)]
{
2023-05-13 00:57:31 +00:00
for (const auto & handler : my_handlers_to_notify)
handler(my_info);
}));
}
}
}