aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Access/GrantedRoles.h
blob: ac2528220895fa273cd246e4d022aeb7186f36d5 (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
66
67
#pragma once

#include <Core/UUID.h>
#include <boost/container/flat_set.hpp>
#include <vector>
#include <unordered_map>


namespace DB
{
struct RolesOrUsersSet;

/// Roles when they are granted to a role or user.
/// Stores both the roles themselves and the roles with admin option.
class GrantedRoles
{
public:
    void grant(const UUID & role_);
    void grant(const std::vector<UUID> & roles_);
    void grantWithAdminOption(const UUID & role_);
    void grantWithAdminOption(const std::vector<UUID> & roles_);

    void revoke(const UUID & role_);
    void revoke(const std::vector<UUID> & roles_);
    void revokeAdminOption(const UUID & role_);
    void revokeAdminOption(const std::vector<UUID> & roles_);

    bool isEmpty() const { return roles.empty(); }

    bool isGranted(const UUID & role_) const;
    bool isGrantedWithAdminOption(const UUID & role_) const;

    const boost::container::flat_set<UUID> & getGranted() const { return roles; }
    const boost::container::flat_set<UUID> & getGrantedWithAdminOption() const { return roles_with_admin_option; }

    std::vector<UUID> findGranted(const std::vector<UUID> & ids) const;
    std::vector<UUID> findGranted(const boost::container::flat_set<UUID> & ids) const;
    std::vector<UUID> findGranted(const RolesOrUsersSet & ids) const;
    std::vector<UUID> findGrantedWithAdminOption(const std::vector<UUID> & ids) const;
    std::vector<UUID> findGrantedWithAdminOption(const boost::container::flat_set<UUID> & ids) const;
    std::vector<UUID> findGrantedWithAdminOption(const RolesOrUsersSet & ids) const;

    struct Element
    {
        std::vector<UUID> ids;
        bool admin_option = false;
        bool empty() const { return ids.empty(); }
    };
    using Elements = std::vector<Element>;

    /// Retrieves the information about grants.
    Elements getElements() const;

    void makeUnion(const GrantedRoles & other);
    void makeIntersection(const GrantedRoles & other);

    friend bool operator ==(const GrantedRoles & left, const GrantedRoles & right) { return (left.roles == right.roles) && (left.roles_with_admin_option == right.roles_with_admin_option); }
    friend bool operator !=(const GrantedRoles & left, const GrantedRoles & right) { return !(left == right); }

    std::vector<UUID> findDependencies() const;
    void replaceDependencies(const std::unordered_map<UUID, UUID> & old_to_new_ids);

private:
    boost::container::flat_set<UUID> roles;
    boost::container::flat_set<UUID> roles_with_admin_option;
};
}