OmniSciDB  c0231cc57d
 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 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 
27 #ifndef DBOBJECT_H
28 #define DBOBJECT_H
29 
30 #include <string>
31 #include <unordered_set>
32 #include "Logger/Logger.h"
33 
34 namespace Catalog_Namespace {
35 class Catalog;
36 }
37 
38 // DB objects for which privileges are currently supported, only ever add enums, never
39 // remove as the nums are persisted in the catalog DB
47 };
48 
50 DBObjectType DBObjectTypeFromString(const std::string& type);
51 
52 struct DBObjectKey {
53  int32_t permissionType = -1;
54  int32_t dbId = -1;
55  int32_t objectId = -1;
56 
57  static const size_t N_COLUMNS = 3;
58 
59  bool operator<(const DBObjectKey& key) const {
60  int32_t ids_a[N_COLUMNS] = {permissionType, dbId, objectId};
61  int32_t ids_b[N_COLUMNS] = {key.permissionType, key.dbId, key.objectId};
62  return memcmp(ids_a, ids_b, N_COLUMNS * sizeof(int32_t)) < 0;
63  }
64 
65  bool operator==(const DBObjectKey& key) const {
66  return permissionType == key.permissionType && dbId == key.dbId &&
67  objectId == key.objectId;
68  }
69 
70  static DBObjectKey fromString(const std::vector<std::string>& key,
71  const DBObjectType& type);
72 };
73 
74 // Access privileges currently supported
75 
77  static const int32_t ALL = -1;
78  static const int32_t CREATE_DATABASE = 1 << 0;
79  static const int32_t DROP_DATABASE = 1 << 1;
80  static const int32_t VIEW_SQL_EDITOR = 1 << 2;
81  static const int32_t ACCESS = 1 << 3;
82 };
83 
85  static const int32_t ALL = -1;
86  static const int32_t CREATE_TABLE = 1 << 0;
87  static const int32_t DROP_TABLE = 1 << 1;
88  static const int32_t SELECT_FROM_TABLE = 1 << 2;
89  static const int32_t INSERT_INTO_TABLE = 1 << 3;
90  static const int32_t UPDATE_IN_TABLE = 1 << 4;
91  static const int32_t DELETE_FROM_TABLE = 1 << 5;
92  static const int32_t TRUNCATE_TABLE = 1 << 6;
93  static const int32_t ALTER_TABLE = 1 << 7;
94 
95  static const int32_t ALL_MIGRATE =
97 };
98 
100  static const int32_t ALL = -1;
101  static const int32_t CREATE_DASHBOARD = 1 << 0;
102  static const int32_t DELETE_DASHBOARD = 1 << 1;
103  static const int32_t VIEW_DASHBOARD = 1 << 2;
104  static const int32_t EDIT_DASHBOARD = 1 << 3;
105 
106  static const int32_t ALL_MIGRATE =
108 };
109 
111  static const int32_t ALL = -1;
112  static const int32_t CREATE_VIEW = 1 << 0;
113  static const int32_t DROP_VIEW = 1 << 1;
114  static const int32_t SELECT_FROM_VIEW = 1 << 2;
115  static const int32_t INSERT_INTO_VIEW = 1 << 3;
116  static const int32_t UPDATE_IN_VIEW = 1 << 4;
117  static const int32_t DELETE_FROM_VIEW = 1 << 5;
118  static const int32_t TRUNCATE_VIEW = 1 << 6;
119 
120  static const int32_t ALL_MIGRATE =
122 };
123 
125  static const int32_t ALL = -1;
126  static const int32_t CREATE_SERVER = 1 << 0;
127  static const int32_t DROP_SERVER = 1 << 1;
128  static const int32_t ALTER_SERVER = 1 << 2;
129  static const int32_t SERVER_USAGE = 1 << 3;
130 };
131 
133  int64_t privileges;
134 
136 
137  AccessPrivileges(int64_t priv) : privileges(priv) {}
138 
139  void reset() { privileges = 0L; }
140  bool hasAny() const { return 0L != privileges; }
141  bool hasPermission(int permission) const {
142  return permission == (privileges & permission);
143  }
144 
145  void add(AccessPrivileges newprivs) { privileges |= newprivs.privileges; }
146  void remove(AccessPrivileges newprivs) { privileges &= ~(newprivs.privileges); }
147 
148  static const AccessPrivileges NONE;
149 
150  // database permissions
153  static const AccessPrivileges ACCESS;
154 
155  // table permissions
166 
167  // dashboard permissions
174 
175  // view permissions
185 
186  // 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_("")
201  , objectKey_(key)
202  , objectPrivs_(privs)
203  , ownerId_(owner){};
204  DBObject(const DBObject& object);
205  DBObject(const std::string& name,
207  DBObjectKey key,
208  AccessPrivileges privs,
209  int32_t owner)
210  : objectName_(name)
211  , objectType_(type)
212  , objectKey_(key)
213  , objectPrivs_(privs)
214  , ownerId_(owner){};
216 
217  void setObjectType(const DBObjectType& objectType);
218  void setName(std::string name) { objectName_ = name; }
219  std::string getName() const { return objectName_; }
220  DBObjectType getType() const { return objectType_; }
222  CHECK(-1 != objectKey_.dbId);
223  return objectKey_;
224  }
225  void setObjectKey(const DBObjectKey& objectKey) { objectKey_ = objectKey; }
226  const AccessPrivileges& getPrivileges() const { return objectPrivs_; }
227  void setPrivileges(const AccessPrivileges& privs) { objectPrivs_ = privs; }
229  void copyPrivileges(const DBObject& object);
230  void updatePrivileges(const DBObject& object);
231  void grantPrivileges(const DBObject& object) { updatePrivileges(object); }
232  void revokePrivileges(const DBObject& object);
233  void setPermissionType(const DBObjectType& permissionType);
234  int32_t getOwner() const { return ownerId_; }
235  void setOwner(int32_t userId) { ownerId_ = userId; }
236  std::vector<std::string> toString() const;
237  void loadKey();
238  void loadKey(const Catalog_Namespace::Catalog& catalog);
239 
240  bool valid() const {
242  objectKey_.dbId != -1);
243  }
244 
245  private:
246  std::string objectName_;
250  int32_t ownerId_; // 0 - if not owned by user
251 };
252 
253 #endif /* DBOBJECT_H */
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:184
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:152
static const int32_t SERVER_USAGE
Definition: DBObject.h:129
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:171
static const int32_t DROP_VIEW
Definition: DBObject.h:113
static const int32_t ALTER_SERVER
Definition: DBObject.h:128
static const AccessPrivileges DROP_SERVER
Definition: DBObject.h:189
DBObjectKey getObjectKey() const
Definition: DBObject.h:221
static const int32_t SELECT_FROM_VIEW
Definition: DBObject.h:114
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:132
static const int32_t UPDATE_IN_VIEW
Definition: DBObject.h:116
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:151
void copyPrivileges(const DBObject &object)
Definition: DBObject.cpp:148
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:165
DBObjectType
Definition: DBObject.h:40
static const int32_t CREATE_VIEW
Definition: DBObject.h:112
void updatePrivileges(const DBObject &object)
Definition: DBObject.cpp:152
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:164
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:156
static const int32_t ALL
Definition: DBObject.h:77
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:161
void revokePrivileges(const DBObject &object)
Definition: DBObject.cpp:156
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:170
static const AccessPrivileges SERVER_USAGE
Definition: DBObject.h:191
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:225
bool hasAny() const
Definition: DBObject.h:140
~DBObject()
Definition: DBObject.h:215
int32_t objectId
Definition: DBObject.h:55
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:182
static const int32_t ALTER_TABLE
Definition: DBObject.h:93
void setName(std::string name)
Definition: DBObject.h:218
void setPrivileges(const AccessPrivileges &privs)
Definition: DBObject.h:227
int32_t ownerId_
Definition: DBObject.h:250
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:160
DBObject(const std::string &name, const DBObjectType &objectAndPermissionType)
Definition: DBObject.cpp:126
bool operator<(const DBObjectKey &key) const
Definition: DBObject.h:59
AccessPrivileges objectPrivs_
Definition: DBObject.h:249
static const size_t N_COLUMNS
Definition: DBObject.h:57
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:177
DBObject(const std::string &name, DBObjectType type, DBObjectKey key, AccessPrivileges privs, int32_t owner)
Definition: DBObject.h:205
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:110
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:190
DBObjectKey objectKey_
Definition: DBObject.h:248
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:181
void add(AccessPrivileges newprivs)
Definition: DBObject.h:145
static const int32_t ALL_MIGRATE
Definition: DBObject.h:120
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:270
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:160
static const int32_t DROP_DATABASE
Definition: DBObject.h:79
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:159
std::vector< std::string > toString() const
Definition: DBObject.cpp:167
void resetPrivileges()
Definition: DBObject.h:228
static const int32_t ALL_MIGRATE
Definition: DBObject.h:95
static const int32_t DELETE_FROM_TABLE
Definition: DBObject.h:91
void grantPrivileges(const DBObject &object)
Definition: DBObject.h:231
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:168
static const int32_t TRUNCATE_TABLE
Definition: DBObject.h:92
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:187
static const AccessPrivileges CREATE_SERVER
Definition: DBObject.h:188
void setOwner(int32_t userId)
Definition: DBObject.h:235
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:163
std::string getName() const
Definition: DBObject.h:219
static const AccessPrivileges NONE
Definition: DBObject.h:148
std::string objectName_
Definition: DBObject.h:246
static const int32_t ALL
Definition: DBObject.h:111
static const int32_t EDIT_DASHBOARD
Definition: DBObject.h:104
static const int32_t DELETE_DASHBOARD
Definition: DBObject.h:102
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:158
static const int32_t INSERT_INTO_TABLE
Definition: DBObject.h:89
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:183
std::string DBObjectTypeToString(DBObjectType type)
Definition: DBObject.cpp:92
DBObjectType getType() const
Definition: DBObject.h:220
static const int32_t CREATE_SERVER
Definition: DBObject.h:126
void loadKey()
Definition: DBObject.cpp:190
const AccessPrivileges & getPrivileges() const
Definition: DBObject.h:226
void setObjectType(const DBObjectType &objectType)
Definition: DBObject.cpp:163
static const int32_t ALL_MIGRATE
Definition: DBObject.h:106
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:176
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:180
static const int32_t ACCESS
Definition: DBObject.h:81
static const int32_t ALL
Definition: DBObject.h:125
int32_t dbId
Definition: DBObject.h:54
static const int32_t CREATE_DATABASE
Definition: DBObject.h:78
static const int32_t ALL
Definition: DBObject.h:85
DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
Definition: DBObject.h:198
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:169
static const AccessPrivileges ACCESS
Definition: DBObject.h:153
static const int32_t VIEW_DASHBOARD
Definition: DBObject.h:103
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:157
static const int32_t VIEW_SQL_EDITOR
Definition: DBObject.h:80
static const int32_t DROP_TABLE
Definition: DBObject.h:87
static const int32_t INSERT_INTO_VIEW
Definition: DBObject.h:115
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:179
static const int32_t ALL
Definition: DBObject.h:100
#define CHECK(condition)
Definition: Logger.h:222
static const int32_t DELETE_FROM_VIEW
Definition: DBObject.h:117
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:178
static const int32_t CREATE_TABLE
Definition: DBObject.h:86
static const int32_t CREATE_DASHBOARD
Definition: DBObject.h:101
int32_t permissionType
Definition: DBObject.h:53
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:173
static const int32_t SELECT_FROM_TABLE
Definition: DBObject.h:88
int64_t privileges
Definition: DBObject.h:133
string name
Definition: setup.in.py:72
int32_t getOwner() const
Definition: DBObject.h:234
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:172
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:162
bool operator==(const DBObjectKey &key) const
Definition: DBObject.h:65
static const int32_t UPDATE_IN_TABLE
Definition: DBObject.h:90
bool valid() const
Definition: DBObject.h:240
static const int32_t TRUNCATE_VIEW
Definition: DBObject.h:118
bool hasPermission(int permission) const
Definition: DBObject.h:141
AccessPrivileges(int64_t priv)
Definition: DBObject.h:137
static const int32_t DROP_SERVER
Definition: DBObject.h:127
DBObjectType objectType_
Definition: DBObject.h:247