DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Protected Member Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Security.PrivilegeManager Class Reference
Inheritance diagram for Deveel.Data.Security.PrivilegeManager:
Deveel.Data.Security.IPrivilegeManager

Classes

class  GrantCacheKey
 

Public Member Functions

 PrivilegeManager (IQuery queryContext)
 
void Dispose ()
 
void GrantToUser (string userName, Grant grant)
 
Privileges GetUserPrivileges (string userName, DbObjectType objectType, ObjectName objectName, bool withOption)
 
void RevokeFromUser (string userName, Grant grant)
 
void GrantToGroup (string groupName, Grant grant)
 
void RevokeFromGroup (string groupName, Grant grant)
 
Privileges GetGroupPrivileges (string groupName, DbObjectType objectType, ObjectName objectName)
 

Protected Member Functions

virtual void Dispose (bool disposing)
 

Properties

IQuery QueryContext [get, private set]
 

Private Member Functions

 ~PrivilegeManager ()
 
void UpdateUserGrants (DbObjectType objectType, ObjectName objectName, string granter, string grantee, Privileges privileges, bool withOption)
 
void ClearUserGrantsCache (string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
 
void ClearUserGrantsCache (string userName)
 
bool TryGetPrivilegesFromCache (string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, out Privileges privileges)
 
void SetPrivilegesInCache (string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, Privileges privileges)
 
void RevokeAllGrantsFromUser (DbObjectType objectType, ObjectName objectName, string revoker, string user, bool withOption=false)
 
Privileges QueryUserPrivileges (string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
 
Privileges QueryGroupPrivileges (string groupName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
 

Static Private Member Functions

static void UpdateGrants (IQuery queryContext, IMutableTable grantTable, DbObjectType objectType, ObjectName objectName, string granter, string grantee, Privileges privileges, bool withOption)
 
static void RevokeAllGrants (IQuery queryContext, IMutableTable grantTable, DbObjectType objectType, ObjectName objectName, string revoker, string user, bool withOption=false)
 
static Privileges QueryPrivileges (IQuery queryContext, ITable grantTable, string grantee, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
 

Private Attributes

Dictionary< GrantCacheKey, PrivilegesgrantsCache
 
Dictionary< string, PrivilegesgroupsPrivilegesCache
 

Detailed Description

Definition at line 27 of file PrivilegeManager.cs.

Constructor & Destructor Documentation

Deveel.Data.Security.PrivilegeManager.PrivilegeManager ( IQuery  queryContext)
inline

Definition at line 31 of file PrivilegeManager.cs.

31  {
32  QueryContext = queryContext;
33  }
Deveel.Data.Security.PrivilegeManager.~PrivilegeManager ( )
inlineprivate

Definition at line 35 of file PrivilegeManager.cs.

35  {
36  Dispose(false);
37  }

Member Function Documentation

void Deveel.Data.Security.PrivilegeManager.ClearUserGrantsCache ( string  userName,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption,
bool  withPublic 
)
inlineprivate

Definition at line 133 of file PrivilegeManager.cs.

133  {
134  if (grantsCache == null)
135  return;
136 
137  var key = new GrantCacheKey(userName, objectType, objectName.FullName, withOption, withPublic);
138  grantsCache.Remove(key);
139  }
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
Dictionary< GrantCacheKey, Privileges > grantsCache
void Deveel.Data.Security.PrivilegeManager.ClearUserGrantsCache ( string  userName)
inlineprivate

Definition at line 141 of file PrivilegeManager.cs.

141  {
142  if (grantsCache == null)
143  return;
144 
145  var keys = grantsCache.Keys.Where(x => x.userName.Equals(userName, StringComparison.OrdinalIgnoreCase));
146  foreach (var key in keys) {
147  grantsCache.Remove(key);
148  }
149  }
Dictionary< GrantCacheKey, Privileges > grantsCache
void Deveel.Data.Security.PrivilegeManager.Dispose ( )
inline

Definition at line 41 of file PrivilegeManager.cs.

41  {
42  Dispose(true);
43  GC.SuppressFinalize(this);
44  }
virtual void Deveel.Data.Security.PrivilegeManager.Dispose ( bool  disposing)
inlineprotectedvirtual

Definition at line 54 of file PrivilegeManager.cs.

54  {
55  QueryContext = null;
56  }
Privileges Deveel.Data.Security.PrivilegeManager.GetGroupPrivileges ( string  groupName,
DbObjectType  objectType,
ObjectName  objectName 
)
inline

Implements Deveel.Data.Security.IPrivilegeManager.

Definition at line 331 of file PrivilegeManager.cs.

331  {
332  Privileges privileges;
333  if (!TryGetPrivilegesFromCache(groupName, objectType, objectName, false, false, out privileges)) {
334  privileges = QueryGroupPrivileges(groupName, objectType, objectName, false, false);
335  SetPrivilegesInCache(groupName, objectType, objectName, false, false, privileges);
336  }
337 
338  return privileges;
339  }
Privileges QueryGroupPrivileges(string groupName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
bool TryGetPrivilegesFromCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, out Privileges privileges)
void SetPrivilegesInCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, Privileges privileges)
Privileges Deveel.Data.Security.PrivilegeManager.GetUserPrivileges ( string  userName,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption 
)
inline

Implements Deveel.Data.Security.IPrivilegeManager.

Definition at line 302 of file PrivilegeManager.cs.

302  {
303  Privileges privs;
304  if (!TryGetPrivilegesFromCache(userName, objectType, objectName, withOption, true, out privs)) {
305  privs = QueryUserPrivileges(userName, objectType, objectName, withOption, true);
306  SetPrivilegesInCache(userName, objectType, objectName, withOption, true, privs);
307  }
308 
309  return privs;
310  }
Privileges QueryUserPrivileges(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
bool TryGetPrivilegesFromCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, out Privileges privileges)
void SetPrivilegesInCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, Privileges privileges)
void Deveel.Data.Security.PrivilegeManager.GrantToGroup ( string  groupName,
Grant  grant 
)
inline

Implements Deveel.Data.Security.IPrivilegeManager.

Definition at line 323 of file PrivilegeManager.cs.

323  {
324  throw new NotImplementedException();
325  }
void Deveel.Data.Security.PrivilegeManager.GrantToUser ( string  userName,
Grant  grant 
)
inline

Implements Deveel.Data.Security.IPrivilegeManager.

Definition at line 151 of file PrivilegeManager.cs.

151  {
152  if (String.IsNullOrEmpty(userName))
153  throw new ArgumentNullException("userName");
154  if (grant == null)
155  throw new ArgumentNullException("grant");
156 
157  var objectType = grant.ObjectType;
158  var objectName = grant.ObjectName;
159  var privileges = grant.Privileges;
160 
161  Privileges oldPrivs = GetUserPrivileges(userName, objectType, objectName, grant.WithOption);
162  privileges |= oldPrivs;
163 
164  if (!oldPrivs.Equals(privileges))
165  UpdateUserGrants(objectType, objectName, grant.GranterName, userName, privileges, grant.WithOption);
166  }
A long string in the system.
void UpdateUserGrants(DbObjectType objectType, ObjectName objectName, string granter, string grantee, Privileges privileges, bool withOption)
Privileges GetUserPrivileges(string userName, DbObjectType objectType, ObjectName objectName, bool withOption)
Privileges Deveel.Data.Security.PrivilegeManager.QueryGroupPrivileges ( string  groupName,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption,
bool  withPublic 
)
inlineprivate

Definition at line 296 of file PrivilegeManager.cs.

297  {
298  var grantTable = QueryContext.GetTable(SystemSchema.GroupGrantsTable);
299  return QueryPrivileges(QueryContext, grantTable, groupName, objectType, objectName, withOption, withPublic);
300  }
static Privileges QueryPrivileges(IQuery queryContext, ITable grantTable, string grantee, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
static Privileges Deveel.Data.Security.PrivilegeManager.QueryPrivileges ( IQuery  queryContext,
ITable  grantTable,
string  grantee,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption,
bool  withPublic 
)
inlinestaticprivate

Definition at line 237 of file PrivilegeManager.cs.

238  {
239  var objectCol = grantTable.GetResolvedColumnName(1);
240  var paramCol = grantTable.GetResolvedColumnName(2);
241  var granteeCol = grantTable.GetResolvedColumnName(3);
242  var grantOptionCol = grantTable.GetResolvedColumnName(4);
243  var granterCol = grantTable.GetResolvedColumnName(5);
244 
245  ITable t1 = grantTable;
246 
247  // All that match the given object parameter
248  // It's most likely this will reduce the search by the most so we do
249  // it first.
250  t1 = t1.SimpleSelect(queryContext, paramCol, SqlExpressionType.Equal, SqlExpression.Constant(DataObject.String(objectName.FullName)));
251 
252  // The next is a single exhaustive select through the remaining records.
253  // It finds all grants that match either public or the grantee is the
254  // username, and that match the object type.
255 
256  // Expression: ("grantee_col" = username OR "grantee_col" = 'public')
257  var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol), SqlExpression.Constant(DataObject.String(grantee)));
258  if (withPublic) {
259  userCheck = SqlExpression.Or(userCheck, SqlExpression.Equal(SqlExpression.Reference(granteeCol),
260  SqlExpression.Constant(DataObject.String(User.PublicName))));
261  }
262 
263  // Expression: ("object_col" = object AND
264  // ("grantee_col" = username OR "grantee_col" = 'public'))
265  // All that match the given username or public and given object
267  SqlExpression.Constant(DataObject.BigInt((int)objectType))), userCheck);
268 
269  // Are we only searching for grant options?
270  if (withOption) {
271  var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
272  SqlExpression.Constant(DataObject.BooleanTrue));
273  expr = SqlExpression.And(expr, grantOptionCheck);
274  }
275 
276  t1 = t1.ExhaustiveSelect(queryContext, expr);
277 
278  // For each grant, merge with the resultant priv object
279  Privileges privs = Privileges.None;
280 
281  foreach (var row in t1) {
282  var priv = (int)row.GetValue(0).AsBigInt();
283  privs |= (Privileges)priv;
284  }
285 
286  return privs;
287  }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static SqlBinaryExpression And(SqlExpression left, SqlExpression right)
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
SqlExpressionType
All the possible type of SqlExpression supported
static SqlBinaryExpression Or(SqlExpression left, SqlExpression right)
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
Privileges Deveel.Data.Security.PrivilegeManager.QueryUserPrivileges ( string  userName,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption,
bool  withPublic 
)
inlineprivate

Definition at line 289 of file PrivilegeManager.cs.

290  {
291  // The system grants table.
292  var grantTable = QueryContext.GetTable(SystemSchema.UserGrantsTableName);
293  return QueryPrivileges(QueryContext, grantTable, userName, objectType, objectName, withOption, withPublic);
294  }
static Privileges QueryPrivileges(IQuery queryContext, ITable grantTable, string grantee, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
static void Deveel.Data.Security.PrivilegeManager.RevokeAllGrants ( IQuery  queryContext,
IMutableTable  grantTable,
DbObjectType  objectType,
ObjectName  objectName,
string  revoker,
string  user,
bool  withOption = false 
)
inlinestaticprivate

Definition at line 75 of file PrivilegeManager.cs.

75  {
76  var objectCol = grantTable.GetResolvedColumnName(1);
77  var paramCol = grantTable.GetResolvedColumnName(2);
78  var granteeCol = grantTable.GetResolvedColumnName(3);
79  var grantOptionCol = grantTable.GetResolvedColumnName(4);
80  var granterCol = grantTable.GetResolvedColumnName(5);
81 
82  ITable t1 = grantTable;
83 
84  // All that match the given object parameter
85  // It's most likely this will reduce the search by the most so we do
86  // it first.
87  t1 = t1.SimpleSelect(queryContext, paramCol, SqlExpressionType.Equal,
88  SqlExpression.Constant(DataObject.String(objectName.FullName)));
89 
90  // The next is a single exhaustive select through the remaining records.
91  // It finds all grants that match either public or the grantee is the
92  // username, and that match the object type.
93 
94  // Expression: ("grantee_col" = username)
95  var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol),
96  SqlExpression.Constant(DataObject.String(user)));
97 
98  // Expression: ("object_col" = object AND
99  // "grantee_col" = username)
100  // All that match the given username or public and given object
101  var expr =
104  SqlExpression.Constant(DataObject.BigInt((int)objectType))), userCheck);
105 
106  // Are we only searching for grant options?
107  var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
108  SqlExpression.Constant(DataObject.Boolean(withOption)));
109  expr = SqlExpression.And(expr, grantOptionCheck);
110 
111  // Make sure the granter matches up also
112  var granterCheck = SqlExpression.Equal(SqlExpression.Reference(granterCol),
113  SqlExpression.Constant(DataObject.String(revoker)));
114  expr = SqlExpression.And(expr, granterCheck);
115 
116  t1 = t1.ExhaustiveSelect(queryContext, expr);
117 
118  // Remove these rows from the table
119  grantTable.Delete(t1);
120  }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static SqlBinaryExpression And(SqlExpression left, SqlExpression right)
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
SqlExpressionType
All the possible type of SqlExpression supported
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
void Deveel.Data.Security.PrivilegeManager.RevokeAllGrantsFromUser ( DbObjectType  objectType,
ObjectName  objectName,
string  revoker,
string  user,
bool  withOption = false 
)
inlineprivate

Definition at line 188 of file PrivilegeManager.cs.

188  {
189  var grantTable = QueryContext.GetMutableTable(SystemSchema.UserGrantsTableName);
190 
191  var objectCol = grantTable.GetResolvedColumnName(1);
192  var paramCol = grantTable.GetResolvedColumnName(2);
193  var granteeCol = grantTable.GetResolvedColumnName(3);
194  var grantOptionCol = grantTable.GetResolvedColumnName(4);
195  var granterCol = grantTable.GetResolvedColumnName(5);
196 
197  ITable t1 = grantTable;
198 
199  // All that match the given object parameter
200  // It's most likely this will reduce the search by the most so we do
201  // it first.
202  t1 = t1.SimpleSelect(QueryContext, paramCol, SqlExpressionType.Equal,
203  SqlExpression.Constant(DataObject.String(objectName.FullName)));
204 
205  // The next is a single exhaustive select through the remaining records.
206  // It finds all grants that match either public or the grantee is the
207  // username, and that match the object type.
208 
209  // Expression: ("grantee_col" = username)
210  var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol),
211  SqlExpression.Constant(DataObject.String(user)));
212 
213  // Expression: ("object_col" = object AND
214  // "grantee_col" = username)
215  // All that match the given username or public and given object
216  var expr =
219  SqlExpression.Constant(DataObject.BigInt((int)objectType))), userCheck);
220 
221  // Are we only searching for grant options?
222  var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
223  SqlExpression.Constant(DataObject.Boolean(withOption)));
224  expr = SqlExpression.And(expr, grantOptionCheck);
225 
226  // Make sure the granter matches up also
227  var granterCheck = SqlExpression.Equal(SqlExpression.Reference(granterCol),
228  SqlExpression.Constant(DataObject.String(revoker)));
229  expr = SqlExpression.And(expr, granterCheck);
230 
231  t1 = t1.ExhaustiveSelect(QueryContext, expr);
232 
233  // Remove these rows from the table
234  grantTable.Delete(t1);
235  }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static SqlBinaryExpression And(SqlExpression left, SqlExpression right)
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
SqlExpressionType
All the possible type of SqlExpression supported
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
void Deveel.Data.Security.PrivilegeManager.RevokeFromGroup ( string  groupName,
Grant  grant 
)
inline

Implements Deveel.Data.Security.IPrivilegeManager.

Definition at line 327 of file PrivilegeManager.cs.

327  {
328  throw new NotImplementedException();
329  }
void Deveel.Data.Security.PrivilegeManager.RevokeFromUser ( string  userName,
Grant  grant 
)
inline

Implements Deveel.Data.Security.IPrivilegeManager.

Definition at line 312 of file PrivilegeManager.cs.

312  {
313  if (String.IsNullOrEmpty(userName))
314  throw new ArgumentNullException("userName");
315 
316  try {
317  RevokeAllGrantsFromUser(grant.ObjectType, grant.ObjectName, grant.GranterName, userName, grant.WithOption);
318  } finally {
319  ClearUserGrantsCache(userName, grant.ObjectType, grant.ObjectName, grant.WithOption, false);
320  }
321  }
A long string in the system.
void RevokeAllGrantsFromUser(DbObjectType objectType, ObjectName objectName, string revoker, string user, bool withOption=false)
void ClearUserGrantsCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
void Deveel.Data.Security.PrivilegeManager.SetPrivilegesInCache ( string  userName,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption,
bool  withPublic,
Privileges  privileges 
)
inlineprivate

Definition at line 179 of file PrivilegeManager.cs.

180  {
181  var key = new GrantCacheKey(userName, objectType, objectName.FullName, withOption, withPublic);
182  if (grantsCache == null)
183  grantsCache = new Dictionary<GrantCacheKey, Privileges>();
184 
185  grantsCache[key] = privileges;
186  }
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
Dictionary< GrantCacheKey, Privileges > grantsCache
bool Deveel.Data.Security.PrivilegeManager.TryGetPrivilegesFromCache ( string  userName,
DbObjectType  objectType,
ObjectName  objectName,
bool  withOption,
bool  withPublic,
out Privileges  privileges 
)
inlineprivate

Definition at line 168 of file PrivilegeManager.cs.

169  {
170  if (grantsCache == null) {
171  privileges = Privileges.None;
172  return false;
173  }
174 
175  var key = new GrantCacheKey(userName, objectType, objectName.FullName, withOption, withPublic);
176  return grantsCache.TryGetValue(key, out privileges);
177  }
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
Dictionary< GrantCacheKey, Privileges > grantsCache
static void Deveel.Data.Security.PrivilegeManager.UpdateGrants ( IQuery  queryContext,
IMutableTable  grantTable,
DbObjectType  objectType,
ObjectName  objectName,
string  granter,
string  grantee,
Privileges  privileges,
bool  withOption 
)
inlinestaticprivate

Definition at line 58 of file PrivilegeManager.cs.

59  {
60  RevokeAllGrants(queryContext, grantTable, objectType, objectName, granter, grantee, withOption);
61 
62  if (privileges != Privileges.None) {
63  // Add the grant to the grants table.
64  var row = grantTable.NewRow();
65  row.SetValue(0, (int)privileges);
66  row.SetValue(1, (int)objectType);
67  row.SetValue(2, objectName.FullName);
68  row.SetValue(3, grantee);
69  row.SetValue(4, withOption);
70  row.SetValue(5, granter);
71  grantTable.AddRow(row);
72  }
73  }
static void RevokeAllGrants(IQuery queryContext, IMutableTable grantTable, DbObjectType objectType, ObjectName objectName, string revoker, string user, bool withOption=false)
RowId AddRow(Row row)
Persists a new row to the table.
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
void Deveel.Data.Security.PrivilegeManager.UpdateUserGrants ( DbObjectType  objectType,
ObjectName  objectName,
string  granter,
string  grantee,
Privileges  privileges,
bool  withOption 
)
inlineprivate

Definition at line 123 of file PrivilegeManager.cs.

123  {
124  var grantTable = QueryContext.GetMutableTable(SystemSchema.UserGrantsTableName);
125 
126  try {
127  UpdateGrants(QueryContext, grantTable, objectType, objectName, granter, grantee, privileges, withOption);
128  } finally {
129  ClearUserGrantsCache(grantee, objectType, objectName, withOption, true);
130  }
131  }
static void UpdateGrants(IQuery queryContext, IMutableTable grantTable, DbObjectType objectType, ObjectName objectName, string granter, string grantee, Privileges privileges, bool withOption)
void ClearUserGrantsCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)

Member Data Documentation

Dictionary<GrantCacheKey, Privileges> Deveel.Data.Security.PrivilegeManager.grantsCache
private

Definition at line 28 of file PrivilegeManager.cs.

Dictionary<string, Privileges> Deveel.Data.Security.PrivilegeManager.groupsPrivilegesCache
private

Definition at line 29 of file PrivilegeManager.cs.

Property Documentation

IQuery Deveel.Data.Security.PrivilegeManager.QueryContext
getprivate set

Definition at line 39 of file PrivilegeManager.cs.


The documentation for this class was generated from the following file: