23 using std::runtime_error;
30 role->removeGrantee(
this);
38 std::set<std::string> roles;
39 std::stack<const Grantee*> g;
44 for (
auto direct_role : r->roles_) {
46 roles.insert(direct_role->getName());
52 return std::vector(roles.begin(), roles.end());
59 std::stack<const Grantee*> roles;
61 while (!roles.empty()) {
67 for (
auto granted_role : r->roles_) {
68 roles.push(granted_role);
77 auto dbObject =
findDbObject(
object.getObjectKey(), only_direct);
79 throw runtime_error(
"Can not get privileges because " +
getName() +
80 " has no privileges to " +
object.
getName());
82 object.grantPrivileges(*dbObject);
88 auto dbObjectIt = privs.find(objectKey);
89 if (dbObjectIt != privs.end()) {
90 dbObject = dbObjectIt->second.get();
97 for (
const auto& priv : privs) {
98 if (priv.second->getObjectKey().dbId == dbId) {
106 auto* dbObject =
findDbObject(
object.getObjectKey(),
false);
110 dbObject->grantPrivileges(
object);
116 dbObject->grantPrivileges(
object);
125 directIt->second->setName(
object.
getName());
130 effectiveIt->second->setName(
object.
getName());
137 auto dbObject =
findDbObject(
object.getObjectKey(),
true);
139 !dbObject->getPrivileges().hasAny()) {
140 throw runtime_error(
"Can not revoke privileges because " +
getName() +
141 " has no privileges to " +
object.
getName());
143 bool object_removed =
false;
144 dbObject->revokePrivileges(
object);
145 if (!dbObject->getPrivileges().hasAny()) {
147 object_removed =
true;
150 auto* cachedDbObject =
findDbObject(
object.getObjectKey(),
false);
151 if (cachedDbObject && cachedDbObject->getPrivileges().hasAny()) {
152 cachedDbObject->revokePrivileges(
object);
153 if (!cachedDbObject->getPrivileges().hasAny()) {
160 return object_removed ?
nullptr : dbObject;
165 for (
const auto* granted_role :
roles_) {
166 if (role == granted_role) {
172 throw runtime_error(
"Role " + role->
getName() +
" have been granted to " +
name_ +
193 return req == (base & req);
222 if (objectKey.
dbId != -1) {
246 if (objectKey.
dbId != -1) {
257 auto dbObject =
findDbObject(roleDbObject.first,
false);
259 dbObject->updatePrivileges(*roleDbObject.second);
262 boost::make_unique<DBObject>(*roleDbObject.second.get());
271 dbObject.second->resetPrivileges();
275 if (effectivePrivileges_.find(it->first) != effectivePrivileges_.end()) {
276 effectivePrivileges_[it->first]->updatePrivileges(*it->second);
280 for (
auto role :
roles_) {
281 if (role->getDbObjects(
false)->size() > 0) {
286 for (
auto dbObjectIt = effectivePrivileges_.begin();
287 dbObjectIt != effectivePrivileges_.end();) {
288 if (!dbObjectIt->second->getPrivileges().hasAny()) {
289 dbObjectIt = effectivePrivileges_.erase(dbObjectIt);
298 for (
auto privs : sources) {
299 for (
auto iter = privs->begin(); iter != privs->end();) {
300 if (iter->first.dbId == dbId) {
301 iter = privs->erase(iter);
311 std::stack<Grantee*> grantees;
313 while (!grantees.empty()) {
314 auto* grantee = grantees.top();
316 if (!grantee->isUser()) {
317 Role* r =
dynamic_cast<Role*
>(grantee);
320 throw runtime_error(
"Granting role " + newRole->
getName() +
" to " +
getName() +
321 " creates cycle in grantee graph.");
331 int32_t new_owner_id,
334 if (object_key.objectId != -1 && object_key.dbId == db_id &&
336 object->setOwner(new_owner_id);
341 if (object_key.objectId != -1 && object_key.dbId == db_id &&
343 object->setOwner(new_owner_id);
350 if (grantee_object_key == object_key) {
351 object->setOwner(new_owner_id);
356 if (grantee_object_key == object_key) {
357 object->setOwner(new_owner_id);
364 auto current_grantee = *it;
366 current_grantee->revokeRole(
this);
375 throw runtime_error(
"Role " +
getName() +
" have been granted to " +
376 grantee->
getName() +
" already.");
384 throw runtime_error(
"Role " +
getName() +
" have not been granted to " +
390 std::vector<Grantee*> grantees;
392 grantees.push_back(grantee);
400 grantee->revokeAllOnDatabase(dbId);
408 grantee->updatePrivileges();
415 grantee->renameDbObject(
object);
DBObjectMap effectivePrivileges_
bool contains(const T &container, const U &element)
bool hasAnyPrivilegesOnDb(int32_t dbId, bool only_direct) const
void revokeAllOnDatabase(int32_t dbId) override
DBObjectKey getObjectKey() const
void renameDbObject(const DBObject &object) override
virtual void updatePrivileges()
void reassignObjectOwner(DBObjectKey &object_key, int32_t new_owner_id)
virtual void grantPrivileges(const DBObject &object)
virtual DBObject * revokePrivileges(const DBObject &object)
virtual void addGrantee(Grantee *grantee)
static bool hasEnoughPrivs(const DBObject *real, const DBObject *requested)
static bool hasAnyPrivs(const DBObject *real, const DBObject *)
const std::string & getName() const
DBObject * findDbObject(const DBObjectKey &objectKey, bool only_direct) const
void updatePrivileges() override
virtual bool hasAnyPrivileges(const DBObject &objectRequested, bool only_direct) const
virtual void revokeAllOnDatabase(int32_t dbId)
Grantee(const std::string &name)
virtual void revokeRole(Role *role)
std::vector< Grantee * > getGrantees() const
std::map< DBObjectKey, std::unique_ptr< DBObject >> DBObjectMap
const DBObjectMap * getDbObjects(bool only_direct) const
const AccessPrivileges & getPrivileges() const
virtual void removeGrantee(Grantee *grantee)
std::unordered_set< Role * > roles_
void checkCycles(Role *newRole)
virtual void grantRole(Role *role)
bool hasRole(Role *role, bool only_direct) const
void reassignObjectOwners(const std::set< int32_t > &old_owner_ids, int32_t new_owner_id, int32_t db_id)
std::unordered_set< Grantee * > grantees_
DBObjectMap directPrivileges_
void getPrivileges(DBObject &object, bool only_direct)
std::vector< std::string > getRoles(bool only_direct=true) const
virtual void renameDbObject(const DBObject &object)
virtual bool checkPrivileges(const DBObject &objectRequested) const