OmniSciDB  c07336695a
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
48 };
49 
51 DBObjectType DBObjectTypeFromString(const std::string& type);
52 
53 struct DBObjectKey {
54  int32_t permissionType = -1;
55  int32_t dbId = -1;
56  int32_t objectId = -1;
57 
58  static const size_t N_COLUMNS = 3;
59 
60  bool operator<(const DBObjectKey& key) const {
61  int32_t ids_a[N_COLUMNS] = {permissionType, dbId, objectId};
62  int32_t ids_b[N_COLUMNS] = {key.permissionType, key.dbId, key.objectId};
63  return memcmp(ids_a, ids_b, N_COLUMNS * sizeof(int32_t)) < 0;
64  }
65 
66  bool operator==(const DBObjectKey& key) const {
67  return permissionType == key.permissionType && dbId == key.dbId &&
68  objectId == key.objectId;
69  }
70 
71  static DBObjectKey fromString(const std::vector<std::string>& key,
72  const DBObjectType& type);
73 };
74 
75 // Access privileges currently supported
76 
78  static const int32_t ALL = -1;
79  static const int32_t CREATE_DATABASE = 1 << 0;
80  static const int32_t DROP_DATABASE = 1 << 1;
81  static const int32_t VIEW_SQL_EDITOR = 1 << 2;
82  static const int32_t ACCESS = 1 << 3;
83 };
84 
86  static const int32_t ALL = -1;
87  static const int32_t CREATE_TABLE = 1 << 0;
88  static const int32_t DROP_TABLE = 1 << 1;
89  static const int32_t SELECT_FROM_TABLE = 1 << 2;
90  static const int32_t INSERT_INTO_TABLE = 1 << 3;
91  static const int32_t UPDATE_IN_TABLE = 1 << 4;
92  static const int32_t DELETE_FROM_TABLE = 1 << 5;
93  static const int32_t TRUNCATE_TABLE = 1 << 6;
94  static const int32_t ALTER_TABLE = 1 << 7;
95 
96  static const int32_t ALL_MIGRATE =
97  CREATE_TABLE | DROP_TABLE | SELECT_FROM_TABLE | INSERT_INTO_TABLE;
98 };
99 
101  static const int32_t ALL = -1;
102  static const int32_t CREATE_DASHBOARD = 1 << 0;
103  static const int32_t DELETE_DASHBOARD = 1 << 1;
104  static const int32_t VIEW_DASHBOARD = 1 << 2;
105  static const int32_t EDIT_DASHBOARD = 1 << 3;
106 
107  static const int32_t ALL_MIGRATE =
108  CREATE_DASHBOARD | DELETE_DASHBOARD | VIEW_DASHBOARD | EDIT_DASHBOARD;
109 };
110 
112  static const int32_t ALL = -1;
113  static const int32_t CREATE_VIEW = 1 << 0;
114  static const int32_t DROP_VIEW = 1 << 1;
115  static const int32_t SELECT_FROM_VIEW = 1 << 2;
116  static const int32_t INSERT_INTO_VIEW = 1 << 3;
117  static const int32_t UPDATE_IN_VIEW = 1 << 4;
118  static const int32_t DELETE_FROM_VIEW = 1 << 5;
119  static const int32_t TRUNCATE_VIEW = 1 << 6;
120 
121  static const int32_t ALL_MIGRATE =
122  CREATE_VIEW | DROP_VIEW | SELECT_FROM_VIEW | INSERT_INTO_VIEW;
123 };
124 
126  int64_t privileges;
127 
128  AccessPrivileges() : privileges(0) {}
129 
130  AccessPrivileges(int64_t priv) : privileges(priv) {}
131 
132  void reset() { privileges = 0L; }
133  bool hasAny() const { return 0L != privileges; }
134  bool hasPermission(int permission) const {
135  return permission == (privileges & permission);
136  }
137 
138  void add(AccessPrivileges newprivs) { privileges |= newprivs.privileges; }
139  void remove(AccessPrivileges newprivs) { privileges &= ~(newprivs.privileges); }
140 
141  static const AccessPrivileges NONE;
142 
143  // database permissions
146  static const AccessPrivileges ACCESS;
147 
148  // table permissions
159 
160  // dashboard permissions
167 
168  // view permissions
178 };
179 
180 class DBObject {
181  public:
182  DBObject(const std::string& name, const DBObjectType& objectAndPermissionType);
183  DBObject(const int32_t id, const DBObjectType& objectAndPermissionType);
184  DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
185  : objectName_("")
186  , objectType_(AbstractDBObjectType)
187  , objectKey_(key)
188  , objectPrivs_(privs)
189  , ownerId_(owner){};
190  DBObject(const DBObject& object);
192 
193  void setObjectType(const DBObjectType& objectType);
194  void setName(std::string name) { objectName_ = name; }
195  std::string getName() const { return objectName_; }
197  CHECK(-1 != objectKey_.dbId);
198  return objectKey_;
199  }
200  void setObjectKey(const DBObjectKey& objectKey) { objectKey_ = objectKey; }
201  const AccessPrivileges& getPrivileges() const { return objectPrivs_; }
202  void setPrivileges(const AccessPrivileges& privs) { objectPrivs_ = privs; }
203  void resetPrivileges() { objectPrivs_.reset(); }
204  void copyPrivileges(const DBObject& object);
205  void updatePrivileges(const DBObject& object);
206  void grantPrivileges(const DBObject& object) { updatePrivileges(object); }
207  void revokePrivileges(const DBObject& object);
208  void setPermissionType(const DBObjectType& permissionType);
209  int32_t getOwner() const { return ownerId_; }
210  void setOwner(int32_t userId) { ownerId_ = userId; }
211  std::vector<std::string> toString() const;
212  void loadKey();
213  void loadKey(const Catalog_Namespace::Catalog& catalog);
214 
215  private:
216  std::string objectName_;
220  int32_t ownerId_; // 0 - if not owned by user
221 };
222 
223 #endif /* DBOBJECT_H */
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:177
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:145
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:164
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:144
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:158
DBObjectType
Definition: DBObject.h:42
bool operator<(const DBObjectKey &key) const
Definition: DBObject.h:60
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:157
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:149
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:88
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:154
const std::vector< std::shared_ptr< TestColumnDescriptor > > ALL
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:163
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:200
~DBObject()
Definition: DBObject.h:191
int32_t objectId
Definition: DBObject.h:56
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:175
void setName(std::string name)
Definition: DBObject.h:194
void setPrivileges(const AccessPrivileges &privs)
Definition: DBObject.h:202
int32_t ownerId_
Definition: DBObject.h:220
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:153
AccessPrivileges objectPrivs_
Definition: DBObject.h:219
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:170
DBObjectKey objectKey_
Definition: DBObject.h:218
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:174
void add(AccessPrivileges newprivs)
Definition: DBObject.h:138
DBObjectKey getObjectKey() const
Definition: DBObject.h:196
bool hasAny() const
Definition: DBObject.h:133
std::string getName() const
Definition: DBObject.h:195
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:152
void resetPrivileges()
Definition: DBObject.h:203
int32_t getOwner() const
Definition: DBObject.h:209
void grantPrivileges(const DBObject &object)
Definition: DBObject.h:206
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:161
void setOwner(int32_t userId)
Definition: DBObject.h:210
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:156
static const AccessPrivileges NONE
Definition: DBObject.h:141
std::string objectName_
Definition: DBObject.h:216
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:151
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:176
const AccessPrivileges & getPrivileges() const
Definition: DBObject.h:201
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:169
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:173
int32_t dbId
Definition: DBObject.h:55
DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
Definition: DBObject.h:184
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:162
static const AccessPrivileges ACCESS
Definition: DBObject.h:146
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:150
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:172
#define CHECK(condition)
Definition: Logger.h:187
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:171
bool operator==(const DBObjectKey &key) const
Definition: DBObject.h:66
int32_t permissionType
Definition: DBObject.h:54
bool hasPermission(int permission) const
Definition: DBObject.h:134
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:166
std::string DBObjectTypeToString(DBObjectType type)
int64_t privileges
Definition: DBObject.h:126
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:165
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:155
AccessPrivileges(int64_t priv)
Definition: DBObject.h:130
DBObjectType objectType_
Definition: DBObject.h:217