OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 "Logger/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 =
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 =
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 =
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  static const int32_t SERVER_USAGE = 1 << 3;
132 };
133 
135  int64_t privileges;
136 
138 
139  AccessPrivileges(int64_t priv) : privileges(priv) {}
140 
141  void reset() { privileges = 0L; }
142  bool hasAny() const { return 0L != privileges; }
143  bool hasPermission(int permission) const {
144  return permission == (privileges & permission);
145  }
146 
147  void add(AccessPrivileges newprivs) { privileges |= newprivs.privileges; }
148  void remove(AccessPrivileges newprivs) { privileges &= ~(newprivs.privileges); }
149 
150  static const AccessPrivileges NONE;
151 
152  // database permissions
155  static const AccessPrivileges ACCESS;
156 
157  // table permissions
168 
169  // dashboard permissions
176 
177  // view permissions
187 
188  // server permissions
194 };
195 
196 class DBObject {
197  public:
198  DBObject(const std::string& name, const DBObjectType& objectAndPermissionType);
199  DBObject(const int32_t id, const DBObjectType& objectAndPermissionType);
200  DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
201  : objectName_("")
203  , objectKey_(key)
204  , objectPrivs_(privs)
205  , ownerId_(owner){};
206  DBObject(const DBObject& object);
207  DBObject(const std::string& name,
209  DBObjectKey key,
210  AccessPrivileges privs,
211  int32_t owner)
212  : objectName_(name)
213  , objectType_(type)
214  , objectKey_(key)
215  , objectPrivs_(privs)
216  , ownerId_(owner){};
218 
219  void setObjectType(const DBObjectType& objectType);
220  void setName(std::string name) { objectName_ = name; }
221  std::string getName() const { return objectName_; }
222  DBObjectType getType() const { return objectType_; }
224  CHECK(-1 != objectKey_.dbId);
225  return objectKey_;
226  }
227  void setObjectKey(const DBObjectKey& objectKey) { objectKey_ = objectKey; }
228  const AccessPrivileges& getPrivileges() const { return objectPrivs_; }
229  void setPrivileges(const AccessPrivileges& privs) { objectPrivs_ = privs; }
231  void copyPrivileges(const DBObject& object);
232  void updatePrivileges(const DBObject& object);
233  void grantPrivileges(const DBObject& object) { updatePrivileges(object); }
234  void revokePrivileges(const DBObject& object);
235  void setPermissionType(const DBObjectType& permissionType);
236  int32_t getOwner() const { return ownerId_; }
237  void setOwner(int32_t userId) { ownerId_ = userId; }
238  std::vector<std::string> toString() const;
239  void loadKey();
240  void loadKey(const Catalog_Namespace::Catalog& catalog);
241 
242  bool valid() const {
244  objectKey_.dbId != -1);
245  }
246 
247  private:
248  std::string objectName_;
252  int32_t ownerId_; // 0 - if not owned by user
253 };
254 
255 #endif /* DBOBJECT_H */
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:186
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:154
static const int32_t SERVER_USAGE
Definition: DBObject.h:131
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:173
static const int32_t DROP_VIEW
Definition: DBObject.h:115
static const int32_t ALTER_SERVER
Definition: DBObject.h:130
static const AccessPrivileges DROP_SERVER
Definition: DBObject.h:191
DBObjectKey getObjectKey() const
Definition: DBObject.h:223
static const int32_t SELECT_FROM_VIEW
Definition: DBObject.h:116
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:111
static const int32_t UPDATE_IN_VIEW
Definition: DBObject.h:118
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:153
void copyPrivileges(const DBObject &object)
Definition: DBObject.cpp:139
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:167
DBObjectType
Definition: DBObject.h:42
static const int32_t CREATE_VIEW
Definition: DBObject.h:114
void updatePrivileges(const DBObject &object)
Definition: DBObject.cpp:143
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:166
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:158
static const int32_t ALL
Definition: DBObject.h:79
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:163
void revokePrivileges(const DBObject &object)
Definition: DBObject.cpp:147
string name
Definition: setup.in.py:72
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:172
static const AccessPrivileges SERVER_USAGE
Definition: DBObject.h:193
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:227
bool hasAny() const
Definition: DBObject.h:142
~DBObject()
Definition: DBObject.h:217
int32_t objectId
Definition: DBObject.h:57
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:184
static const int32_t ALTER_TABLE
Definition: DBObject.h:95
void setName(std::string name)
Definition: DBObject.h:220
void setPrivileges(const AccessPrivileges &privs)
Definition: DBObject.h:229
int32_t ownerId_
Definition: DBObject.h:252
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:162
DBObject(const std::string &name, const DBObjectType &objectAndPermissionType)
Definition: DBObject.cpp:117
bool operator<(const DBObjectKey &key) const
Definition: DBObject.h:61
AccessPrivileges objectPrivs_
Definition: DBObject.h:251
static const size_t N_COLUMNS
Definition: DBObject.h:59
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:179
DBObject(const std::string &name, DBObjectType type, DBObjectKey key, AccessPrivileges privs, int32_t owner)
Definition: DBObject.h:207
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:101
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:192
DBObjectKey objectKey_
Definition: DBObject.h:250
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:183
void add(AccessPrivileges newprivs)
Definition: DBObject.h:147
static const int32_t ALL_MIGRATE
Definition: DBObject.h:122
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:261
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:151
static const int32_t DROP_DATABASE
Definition: DBObject.h:81
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:161
std::vector< std::string > toString() const
Definition: DBObject.cpp:158
void resetPrivileges()
Definition: DBObject.h:230
static const int32_t ALL_MIGRATE
Definition: DBObject.h:97
static const int32_t DELETE_FROM_TABLE
Definition: DBObject.h:93
void grantPrivileges(const DBObject &object)
Definition: DBObject.h:233
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:170
static const int32_t TRUNCATE_TABLE
Definition: DBObject.h:94
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:189
static const AccessPrivileges CREATE_SERVER
Definition: DBObject.h:190
void setOwner(int32_t userId)
Definition: DBObject.h:237
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:165
std::string getName() const
Definition: DBObject.h:221
static const AccessPrivileges NONE
Definition: DBObject.h:150
std::string objectName_
Definition: DBObject.h:248
static const int32_t ALL
Definition: DBObject.h:113
static const int32_t EDIT_DASHBOARD
Definition: DBObject.h:106
static const int32_t DELETE_DASHBOARD
Definition: DBObject.h:104
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:160
static const int32_t INSERT_INTO_TABLE
Definition: DBObject.h:91
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:185
std::string DBObjectTypeToString(DBObjectType type)
Definition: DBObject.cpp:83
DBObjectType getType() const
Definition: DBObject.h:222
static const int32_t CREATE_SERVER
Definition: DBObject.h:128
void loadKey()
Definition: DBObject.cpp:181
const AccessPrivileges & getPrivileges() const
Definition: DBObject.h:228
void setObjectType(const DBObjectType &objectType)
Definition: DBObject.cpp:154
static const int32_t ALL_MIGRATE
Definition: DBObject.h:108
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:178
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:182
static const int32_t ACCESS
Definition: DBObject.h:83
static const int32_t ALL
Definition: DBObject.h:127
int32_t dbId
Definition: DBObject.h:56
static const int32_t CREATE_DATABASE
Definition: DBObject.h:80
static const int32_t ALL
Definition: DBObject.h:87
DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
Definition: DBObject.h:200
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:171
static const AccessPrivileges ACCESS
Definition: DBObject.h:155
static const int32_t VIEW_DASHBOARD
Definition: DBObject.h:105
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:159
static const int32_t VIEW_SQL_EDITOR
Definition: DBObject.h:82
static const int32_t DROP_TABLE
Definition: DBObject.h:89
static const int32_t INSERT_INTO_VIEW
Definition: DBObject.h:117
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:181
static const int32_t ALL
Definition: DBObject.h:102
#define CHECK(condition)
Definition: Logger.h:209
static const int32_t DELETE_FROM_VIEW
Definition: DBObject.h:119
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:180
static const int32_t CREATE_TABLE
Definition: DBObject.h:88
static const int32_t CREATE_DASHBOARD
Definition: DBObject.h:103
int32_t permissionType
Definition: DBObject.h:55
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:175
static const int32_t SELECT_FROM_TABLE
Definition: DBObject.h:90
int64_t privileges
Definition: DBObject.h:135
int32_t getOwner() const
Definition: DBObject.h:236
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:174
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:164
bool operator==(const DBObjectKey &key) const
Definition: DBObject.h:67
static const int32_t UPDATE_IN_TABLE
Definition: DBObject.h:92
bool valid() const
Definition: DBObject.h:242
static const int32_t TRUNCATE_VIEW
Definition: DBObject.h:120
bool hasPermission(int permission) const
Definition: DBObject.h:143
AccessPrivileges(int64_t priv)
Definition: DBObject.h:139
static const int32_t DROP_SERVER
Definition: DBObject.h:129
DBObjectType objectType_
Definition: DBObject.h:249