2020-04-20 22:07:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Core/UUID.h>
|
|
|
|
#include <boost/container/flat_set.hpp>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-02-26 22:37:00 +00:00
|
|
|
struct RolesOrUsersSet;
|
|
|
|
|
2020-04-20 22:07:00 +00:00
|
|
|
/// Roles when they are granted to a role or user.
|
|
|
|
/// Stores both the roles themselves and the roles with admin option.
|
2021-02-26 22:37:00 +00:00
|
|
|
class GrantedRoles
|
2020-04-20 22:07:00 +00:00
|
|
|
{
|
2021-02-26 22:37:00 +00:00
|
|
|
public:
|
|
|
|
void grant(const UUID & role_);
|
2020-04-20 22:07:00 +00:00
|
|
|
void grant(const std::vector<UUID> & roles_);
|
2021-02-26 22:37:00 +00:00
|
|
|
void grantWithAdminOption(const UUID & role_);
|
2020-04-20 22:07:00 +00:00
|
|
|
void grantWithAdminOption(const std::vector<UUID> & roles_);
|
|
|
|
|
2021-02-26 22:37:00 +00:00
|
|
|
void revoke(const UUID & role_);
|
2020-04-20 22:07:00 +00:00
|
|
|
void revoke(const std::vector<UUID> & roles_);
|
2021-02-26 22:37:00 +00:00
|
|
|
void revokeAdminOption(const UUID & role_);
|
2020-04-20 22:07:00 +00:00
|
|
|
void revokeAdminOption(const std::vector<UUID> & roles_);
|
|
|
|
|
2021-02-26 22:37:00 +00:00
|
|
|
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
|
2020-04-20 22:07:00 +00:00
|
|
|
{
|
2021-02-26 22:37:00 +00:00
|
|
|
std::vector<UUID> ids;
|
|
|
|
bool admin_option = false;
|
|
|
|
bool empty() const { return ids.empty(); }
|
2020-04-20 22:07:00 +00:00
|
|
|
};
|
2021-02-26 22:37:00 +00:00
|
|
|
using Elements = std::vector<Element>;
|
2020-04-20 22:07:00 +00:00
|
|
|
|
|
|
|
/// Retrieves the information about grants.
|
2021-02-26 22:37:00 +00:00
|
|
|
Elements getElements() const;
|
|
|
|
|
|
|
|
void makeUnion(const GrantedRoles & other);
|
|
|
|
void makeIntersection(const GrantedRoles & other);
|
2020-04-20 22:07:00 +00:00
|
|
|
|
|
|
|
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); }
|
2021-02-26 22:37:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
boost::container::flat_set<UUID> roles;
|
|
|
|
boost::container::flat_set<UUID> roles_with_admin_option;
|
2020-04-20 22:07:00 +00:00
|
|
|
};
|
|
|
|
}
|