OmniSciDB  7bf56492aa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 "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 =
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 };
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
190 };
191 
192 class DBObject {
193  public:
194  DBObject(const std::string& name, const DBObjectType& objectAndPermissionType);
195  DBObject(const int32_t id, const DBObjectType& objectAndPermissionType);
196  DBObject(DBObjectKey key, AccessPrivileges privs, int32_t owner)
197  : objectName_("")
199  , objectKey_(key)
200  , objectPrivs_(privs)
201  , ownerId_(owner){};
202  DBObject(const DBObject& object);
204 
205  void setObjectType(const DBObjectType& objectType);
206  void setName(std::string name) { objectName_ = name; }
207  std::string getName() const { return objectName_; }
208  DBObjectType getType() const { return objectType_; }
210  CHECK(-1 != objectKey_.dbId);
211  return objectKey_;
212  }
213  void setObjectKey(const DBObjectKey& objectKey) { objectKey_ = objectKey; }
214  const AccessPrivileges& getPrivileges() const { return objectPrivs_; }
215  void setPrivileges(const AccessPrivileges& privs) { objectPrivs_ = privs; }
217  void copyPrivileges(const DBObject& object);
218  void updatePrivileges(const DBObject& object);
219  void grantPrivileges(const DBObject& object) { updatePrivileges(object); }
220  void revokePrivileges(const DBObject& object);
221  void setPermissionType(const DBObjectType& permissionType);
222  int32_t getOwner() const { return ownerId_; }
223  void setOwner(int32_t userId) { ownerId_ = userId; }
224  std::vector<std::string> toString() const;
225  void loadKey();
226  void loadKey(const Catalog_Namespace::Catalog& catalog);
227 
228  private:
229  std::string objectName_;
233  int32_t ownerId_; // 0 - if not owned by user
234 };
235 
236 #endif /* DBOBJECT_H */
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:184
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:152
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:171
static const int32_t DROP_VIEW
Definition: DBObject.h:115
static const AccessPrivileges DROP_SERVER
Definition: DBObject.h:189
DBObjectKey getObjectKey() const
Definition: DBObject.h:209
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:86
static const int32_t UPDATE_IN_VIEW
Definition: DBObject.h:118
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:151
void copyPrivileges(const DBObject &object)
Definition: DBObject.cpp:135
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:165
DBObjectType
Definition: DBObject.h:42
static const int32_t CREATE_VIEW
Definition: DBObject.h:114
void updatePrivileges(const DBObject &object)
Definition: DBObject.cpp:139
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:79
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:161
void revokePrivileges(const DBObject &object)
Definition: DBObject.cpp:143
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:170
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:213
bool hasAny() const
Definition: DBObject.h:140
~DBObject()
Definition: DBObject.h:203
int32_t objectId
Definition: DBObject.h:57
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:182
static const int32_t ALTER_TABLE
Definition: DBObject.h:95
void setName(std::string name)
Definition: DBObject.h:206
void setPrivileges(const AccessPrivileges &privs)
Definition: DBObject.h:215
int32_t ownerId_
Definition: DBObject.h:233
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:160
DBObject(const std::string &name, const DBObjectType &objectAndPermissionType)
Definition: DBObject.cpp:113
bool operator<(const DBObjectKey &key) const
Definition: DBObject.h:61
AccessPrivileges objectPrivs_
Definition: DBObject.h:232
static const size_t N_COLUMNS
Definition: DBObject.h:59
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:177
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:97
DBObjectKey objectKey_
Definition: DBObject.h:231
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:122
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:257
CHECK(cgen_state)
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:147
static const int32_t DROP_DATABASE
Definition: DBObject.h:81
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:159
std::vector< std::string > toString() const
Definition: DBObject.cpp:154
void resetPrivileges()
Definition: DBObject.h:216
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:219
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:168
static const int32_t TRUNCATE_TABLE
Definition: DBObject.h:94
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:223
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:163
std::string getName() const
Definition: DBObject.h:207
static const AccessPrivileges NONE
Definition: DBObject.h:148
std::string objectName_
Definition: DBObject.h:229
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:158
static const int32_t INSERT_INTO_TABLE
Definition: DBObject.h:91
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:183
DBObjectType getType() const
Definition: DBObject.h:208
static const int32_t CREATE_SERVER
Definition: DBObject.h:128
void loadKey()
Definition: DBObject.cpp:177
const AccessPrivileges & getPrivileges() const
Definition: DBObject.h:214
void setObjectType(const DBObjectType &objectType)
Definition: DBObject.cpp:150
static const int32_t ALL_MIGRATE
Definition: DBObject.h:108
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: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:196
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:105
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:157
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:179
static const int32_t ALL
Definition: DBObject.h:102
static const int32_t DELETE_FROM_VIEW
Definition: DBObject.h:119
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:178
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:173
static const int32_t SELECT_FROM_TABLE
Definition: DBObject.h:90
std::string DBObjectTypeToString(DBObjectType type)
int64_t privileges
Definition: DBObject.h:133
int32_t getOwner() const
Definition: DBObject.h:222
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:67
static const int32_t UPDATE_IN_TABLE
Definition: DBObject.h:92
static const int32_t TRUNCATE_VIEW
Definition: DBObject.h:120
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:129
DBObjectType objectType_
Definition: DBObject.h:230