OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Grantee.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GRANTEE_H
18 #define GRANTEE_H
19 
20 #include "DBObject.h"
21 
22 #include <boost/algorithm/string.hpp>
23 #include <boost/make_unique.hpp>
24 #include <map>
25 #include <string>
26 #include <unordered_set>
27 #include "Logger/Logger.h"
28 
29 class User;
30 class Role;
31 
32 class Grantee {
33  using DBObjectMap = std::map<DBObjectKey, std::unique_ptr<DBObject>>;
34 
35  public:
36  Grantee(const std::string& name);
37  virtual ~Grantee();
38  virtual bool isUser() const = 0;
39  virtual void grantPrivileges(const DBObject& object);
40  virtual DBObject* revokePrivileges(const DBObject& object);
41  virtual void grantRole(Role* role);
42  virtual void revokeRole(Role* role);
43  virtual bool hasAnyPrivileges(const DBObject& objectRequested, bool only_direct) const;
44  virtual bool checkPrivileges(const DBObject& objectRequested) const;
45  virtual void updatePrivileges();
46  virtual void updatePrivileges(Role* role);
47  virtual void revokeAllOnDatabase(int32_t dbId);
48  virtual void renameDbObject(const DBObject& object);
49  void getPrivileges(DBObject& object, bool only_direct);
50  DBObject* findDbObject(const DBObjectKey& objectKey, bool only_direct) const;
51  bool hasAnyPrivilegesOnDb(int32_t dbId, bool only_direct) const;
52  const std::string& getName() const { return name_; }
53  void setName(const std::string& name) { name_ = name; }
54  std::vector<std::string> getRoles(bool only_direct = true) const;
55  bool hasRole(Role* role, bool only_direct) const;
56  const DBObjectMap* getDbObjects(bool only_direct) const {
57  return only_direct ? &directPrivileges_ : &effectivePrivileges_;
58  }
59  void checkCycles(Role* newRole);
60 
61  void reassignObjectOwners(const std::set<int32_t>& old_owner_ids,
62  int32_t new_owner_id,
63  int32_t db_id);
64  void reassignObjectOwner(DBObjectKey& object_key, int32_t new_owner_id);
65 
66  protected:
67  std::string name_;
68  std::unordered_set<Role*> roles_;
69  // tracks all privileges, including privileges from granted roles recursively
71  // tracks only privileges granted directly to this grantee
73 };
74 
75 class User : public Grantee {
76  public:
77  User(const std::string& name) : Grantee(name) {}
78  bool isUser() const override { return true; }
79 };
80 
81 class Role : public Grantee {
82  public:
83  Role(const std::string& name) : Grantee(name) {}
84  ~Role() override;
85 
86  bool isUser() const override { return false; }
87  void updatePrivileges() override;
88  void renameDbObject(const DBObject& object) override;
89 
90  // NOTE(max): To be used only from Grantee
91  virtual void addGrantee(Grantee* grantee);
92  virtual void removeGrantee(Grantee* grantee);
93 
94  void revokeAllOnDatabase(int32_t dbId) override;
95  std::vector<Grantee*> getGrantees() const;
96 
97  private:
98  std::unordered_set<Grantee*> grantees_;
99 };
100 
101 #endif /* GRANTEE_H */
DBObjectMap effectivePrivileges_
Definition: Grantee.h:70
bool isUser() const override
Definition: Grantee.h:78
User(const std::string &name)
Definition: Grantee.h:77
bool hasAnyPrivilegesOnDb(int32_t dbId, bool only_direct) const
Definition: Grantee.cpp:95
void revokeAllOnDatabase(int32_t dbId) override
Definition: Grantee.cpp:397
void renameDbObject(const DBObject &object) override
Definition: Grantee.cpp:412
virtual void updatePrivileges()
Definition: Grantee.cpp:268
void reassignObjectOwner(DBObjectKey &object_key, int32_t new_owner_id)
Definition: Grantee.cpp:348
virtual void grantPrivileges(const DBObject &object)
Definition: Grantee.cpp:105
virtual DBObject * revokePrivileges(const DBObject &object)
Definition: Grantee.cpp:136
virtual void addGrantee(Grantee *grantee)
Definition: Grantee.cpp:371
Definition: Grantee.h:75
Definition: Grantee.h:81
const std::string & getName() const
Definition: Grantee.h:52
virtual ~Grantee()
Definition: Grantee.cpp:28
DBObject * findDbObject(const DBObjectKey &objectKey, bool only_direct) const
Definition: Grantee.cpp:85
void updatePrivileges() override
Definition: Grantee.cpp:405
virtual bool hasAnyPrivileges(const DBObject &objectRequested, bool only_direct) const
Definition: Grantee.cpp:207
virtual void revokeAllOnDatabase(int32_t dbId)
Definition: Grantee.cpp:296
Grantee(const std::string &name)
Definition: Grantee.cpp:26
Class specification and related data structures for DBObject class.
virtual void revokeRole(Role *role)
Definition: Grantee.cpp:181
std::vector< Grantee * > getGrantees() const
Definition: Grantee.cpp:389
std::map< DBObjectKey, std::unique_ptr< DBObject >> DBObjectMap
Definition: Grantee.h:33
const DBObjectMap * getDbObjects(bool only_direct) const
Definition: Grantee.h:56
virtual bool isUser() const =0
virtual void removeGrantee(Grantee *grantee)
Definition: Grantee.cpp:380
std::unordered_set< Role * > roles_
Definition: Grantee.h:68
void checkCycles(Role *newRole)
Definition: Grantee.cpp:310
bool isUser() const override
Definition: Grantee.h:86
Role(const std::string &name)
Definition: Grantee.h:83
void setName(const std::string &name)
Definition: Grantee.h:53
virtual void grantRole(Role *role)
Definition: Grantee.cpp:163
std::string name_
Definition: Grantee.h:67
bool hasRole(Role *role, bool only_direct) const
Definition: Grantee.cpp:55
void reassignObjectOwners(const std::set< int32_t > &old_owner_ids, int32_t new_owner_id, int32_t db_id)
Definition: Grantee.cpp:330
~Role() override
Definition: Grantee.cpp:362
std::unordered_set< Grantee * > grantees_
Definition: Grantee.h:98
DBObjectMap directPrivileges_
Definition: Grantee.h:72
std::vector< std::string > getRoles(bool only_direct=true) const
Definition: Grantee.cpp:37
void getPrivileges(DBObject &object, bool only_direct)
Definition: Grantee.cpp:76
string name
Definition: setup.in.py:72
virtual void renameDbObject(const DBObject &object)
Definition: Grantee.cpp:121
virtual bool checkPrivileges(const DBObject &objectRequested) const
Definition: Grantee.cpp:231