OmniSciDB  0b528656ed
DBObject.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 /*
18  * File: DBObject.h
19  * Author: norair
20  * @brief Class specification and related data structures for DBObject class.
21  *
22  * To support access privileges of DB users to DB entities (tables, columns, views, etc),
23  * the users are granted roles and included in the corresponding object of the Role class,
24  * and DB entities are being described as objects of DBObjects class
25  *
26  * Created on May 16, 2017, 03:30 PM
27  */
28 
29 #ifndef DBOBJECT_H
30 #define DBOBJECT_H
31 
32 #include <string>
33 #include <unordered_set>
34 #include "Shared/Logger.h"
35 
36 namespace Catalog_Namespace {
37 class Catalog;
38 }
39 
40 // DB objects for which privileges are currently supported, only ever add enums, never
41 // remove as the nums are persisted in the catalog DB
49 };
50 
52 DBObjectType DBObjectTypeFromString(const std::string& type);
53 
54 struct DBObjectKey {
55  int32_t permissionType = -1;
56  int32_t dbId = -1;
57  int32_t objectId = -1;
58 
59  static const size_t N_COLUMNS = 3;
60 
61  bool operator<(const DBObjectKey& key) const {
62  int32_t ids_a[N_COLUMNS] = {permissionType, dbId, objectId};
63  int32_t ids_b[N_COLUMNS] = {key.permissionType, key.dbId, key.objectId};
64  return memcmp(ids_a, ids_b, N_COLUMNS * sizeof(int32_t)) < 0;
65  }
66 
67  bool operator==(const DBObjectKey& key) const {
68  return permissionType == key.permissionType && dbId == key.dbId &&
69  objectId == key.objectId;
70  }
71 
72  static DBObjectKey fromString(const std::vector<std::string>& key,
73  const DBObjectType& type);
74 };
75 
76 // Access privileges currently supported
77 
79  static const int32_t ALL = -1;
80  static const int32_t CREATE_DATABASE = 1 << 0;
81  static const int32_t DROP_DATABASE = 1 << 1;
82  static const int32_t VIEW_SQL_EDITOR = 1 << 2;
83  static const int32_t ACCESS = 1 << 3;
84 };
85 
87  static const int32_t ALL = -1;
88  static const int32_t CREATE_TABLE = 1 << 0;
89  static const int32_t DROP_TABLE = 1 << 1;
90  static const int32_t SELECT_FROM_TABLE = 1 << 2;
91  static const int32_t INSERT_INTO_TABLE = 1 << 3;
92  static const int32_t UPDATE_IN_TABLE = 1 << 4;
93  static const int32_t DELETE_FROM_TABLE = 1 << 5;
94  static const int32_t TRUNCATE_TABLE = 1 << 6;
95  static const int32_t ALTER_TABLE = 1 << 7;
96 
97  static const int32_t ALL_MIGRATE =
98  CREATE_TABLE | DROP_TABLE | SELECT_FROM_TABLE | INSERT_INTO_TABLE;
99 };
100 
102  static const int32_t ALL = -1;
103  static const int32_t CREATE_DASHBOARD = 1 << 0;
104  static const int32_t DELETE_DASHBOARD = 1 << 1;
105  static const int32_t VIEW_DASHBOARD = 1 << 2;
106  static const int32_t EDIT_DASHBOARD = 1 << 3;
107 
108  static const int32_t ALL_MIGRATE =
109  CREATE_DASHBOARD | DELETE_DASHBOARD | VIEW_DASHBOARD | EDIT_DASHBOARD;
110 };
111 
113  static const int32_t ALL = -1;
114  static const int32_t CREATE_VIEW = 1 << 0;
115  static const int32_t DROP_VIEW = 1 << 1;
116  static const int32_t SELECT_FROM_VIEW = 1 << 2;
117  static const int32_t INSERT_INTO_VIEW = 1 << 3;
118  static const int32_t UPDATE_IN_VIEW = 1 << 4;
119  static const int32_t DELETE_FROM_VIEW = 1 << 5;
120  static const int32_t TRUNCATE_VIEW = 1 << 6;
121 
122  static const int32_t ALL_MIGRATE =
123  CREATE_VIEW | DROP_VIEW | SELECT_FROM_VIEW | INSERT_INTO_VIEW;
124 };
125 
127  static const int32_t ALL = -1;
128  static const int32_t CREATE_SERVER = 1 << 0;
129  static const int32_t DROP_SERVER = 1 << 1;
130  static const int32_t ALTER_SERVER = 1 << 2;
131 };
132 
134  int64_t privileges;
135 
136  AccessPrivileges() : privileges(0) {}
137 
138  AccessPrivileges(int64_t priv) : privileges(priv) {}
139 
140  void reset() { privileges = 0L; }
141  bool hasAny() const { return 0L != privileges; }
142  bool hasPermission(int permission) const {
143  return permission == (privileges & permission);
144  }
145 
146  void add(AccessPrivileges newprivs) { privileges |= newprivs.privileges; }
147  void remove(AccessPrivileges newprivs) { privileges &= ~(newprivs.privileges); }
148 
149  static const AccessPrivileges NONE;
150 
151  // database permissions
154  static const AccessPrivileges ACCESS;
155 
156  // table permissions
167 
168  // dashboard permissions
175 
176  // view permissions
186 
187  // server permissions
192 };
193 
194 class DBObject {
195  public:
196  DBObject(const std::string& name, const DBObjectType& objectAndPermissionType);
197  DBObject(const int32_t id, const DBObjectType& objectAndPermissionType);
198  DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
199  : objectName_("")
200  , objectType_(AbstractDBObjectType)
201  , objectKey_(key)
202  , objectPrivs_(privs)
203  , ownerId_(owner){};
204  DBObject(const DBObject& object);
206 
207  void setObjectType(const DBObjectType& objectType);
208  void setName(std::string name) { objectName_ = name; }
209  std::string getName() const { return objectName_; }
210  DBObjectType getType() const { return objectType_; }
212  CHECK(-1 != objectKey_.dbId);
213  return objectKey_;
214  }
215  void setObjectKey(const DBObjectKey& objectKey) { objectKey_ = objectKey; }
216  const AccessPrivileges& getPrivileges() const { return objectPrivs_; }
217  void setPrivileges(const AccessPrivileges& privs) { objectPrivs_ = privs; }
218  void resetPrivileges() { objectPrivs_.reset(); }
219  void copyPrivileges(const DBObject& object);
220  void updatePrivileges(const DBObject& object);
221  void grantPrivileges(const DBObject& object) { updatePrivileges(object); }
222  void revokePrivileges(const DBObject& object);
223  void setPermissionType(const DBObjectType& permissionType);
224  int32_t getOwner() const { return ownerId_; }
225  void setOwner(int32_t userId) { ownerId_ = userId; }
226  std::vector<std::string> toString() const;
227  void loadKey();
228  void loadKey(const Catalog_Namespace::Catalog& catalog);
229 
230  private:
231  std::string objectName_;
235  int32_t ownerId_; // 0 - if not owned by user
236 };
237 
238 #endif /* DBOBJECT_H */
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:185
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:153
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:172
static const AccessPrivileges DROP_SERVER
Definition: DBObject.h:190
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:86
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:152
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:166
DBObjectType
Definition: DBObject.h:42
bool operator<(const DBObjectKey &key) const
Definition: DBObject.h:61
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:165
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:157
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:99
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:162
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:171
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:215
~DBObject()
Definition: DBObject.h:205
int32_t objectId
Definition: DBObject.h:57
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:183
void setName(std::string name)
Definition: DBObject.h:208
DBObjectType getType() const
Definition: DBObject.h:210
void setPrivileges(const AccessPrivileges &privs)
Definition: DBObject.h:217
int32_t ownerId_
Definition: DBObject.h:235
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:161
AccessPrivileges objectPrivs_
Definition: DBObject.h:234
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:178
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:191
DBObjectKey objectKey_
Definition: DBObject.h:233
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:182
void add(AccessPrivileges newprivs)
Definition: DBObject.h:146
DBObjectKey getObjectKey() const
Definition: DBObject.h:211
bool hasAny() const
Definition: DBObject.h:141
std::string getName() const
Definition: DBObject.h:209
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:160
void resetPrivileges()
Definition: DBObject.h:218
int32_t getOwner() const
Definition: DBObject.h:224
void grantPrivileges(const DBObject &object)
Definition: DBObject.h:221
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:169
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:188
static const AccessPrivileges CREATE_SERVER
Definition: DBObject.h:189
void setOwner(int32_t userId)
Definition: DBObject.h:225
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:164
static const AccessPrivileges NONE
Definition: DBObject.h:149
std::string objectName_
Definition: DBObject.h:231
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:159
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:184
const AccessPrivileges & getPrivileges() const
Definition: DBObject.h:216
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:177
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:181
int32_t dbId
Definition: DBObject.h:56
DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
Definition: DBObject.h:198
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:170
static const AccessPrivileges ACCESS
Definition: DBObject.h:154
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:158
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:180
#define CHECK(condition)
Definition: Logger.h:197
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:179
bool operator==(const DBObjectKey &key) const
Definition: DBObject.h:67
int32_t permissionType
Definition: DBObject.h:55
bool hasPermission(int permission) const
Definition: DBObject.h:142
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:174
std::string DBObjectTypeToString(DBObjectType type)
int64_t privileges
Definition: DBObject.h:134
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:173
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:163
AccessPrivileges(int64_t priv)
Definition: DBObject.h:138
DBObjectType objectType_
Definition: DBObject.h:232