aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Access/EnabledRoles.h
blob: 5de76abe409e059ab714fdcad3adfa162b6c6672 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

#include <Core/UUID.h>
#include <base/scope_guard.h>
#include <boost/container/flat_set.hpp>
#include <list>
#include <memory>
#include <mutex>
#include <vector>


namespace DB
{
struct EnabledRolesInfo;

class EnabledRoles
{
public:
    struct Params
    {
        boost::container::flat_set<UUID> current_roles;
        boost::container::flat_set<UUID> current_roles_with_admin_option;

        auto toTuple() const { return std::tie(current_roles, current_roles_with_admin_option); }
        friend bool operator ==(const Params & lhs, const Params & rhs) { return lhs.toTuple() == rhs.toTuple(); }
        friend bool operator !=(const Params & lhs, const Params & rhs) { return !(lhs == rhs); }
        friend bool operator <(const Params & lhs, const Params & rhs) { return lhs.toTuple() < rhs.toTuple(); }
        friend bool operator >(const Params & lhs, const Params & rhs) { return rhs < lhs; }
        friend bool operator <=(const Params & lhs, const Params & rhs) { return !(rhs < lhs); }
        friend bool operator >=(const Params & lhs, const Params & rhs) { return !(lhs < rhs); }
    };

    ~EnabledRoles();

    /// Returns all the roles specified in the constructor.
    std::shared_ptr<const EnabledRolesInfo> getRolesInfo() const;

    using OnChangeHandler = std::function<void(const std::shared_ptr<const EnabledRolesInfo> & info)>;

    /// Called when either the specified roles or the roles granted to the specified roles are changed.
    scope_guard subscribeForChanges(const OnChangeHandler & handler) const;

private:
    friend class RoleCache;
    explicit EnabledRoles(const Params & params_);

    const Params params;

    /// Called by RoleCache to store `EnabledRolesInfo` in this `EnabledRoles` after the calculation is done.
    void setRolesInfo(const std::shared_ptr<const EnabledRolesInfo> & info_, scope_guard * notifications);

    std::shared_ptr<const EnabledRolesInfo> info;
    mutable std::mutex info_mutex;

    struct Handlers
    {
        std::list<OnChangeHandler> list;
        std::mutex mutex;
    };

    /// shared_ptr is here for safety because EnabledRoles can be destroyed before all subscriptions are removed.
    std::shared_ptr<Handlers> handlers;
};

}