18 using System.Collections.Generic;
26 namespace Deveel.Data.Security {
43 GC.SuppressFinalize(
this);
54 protected virtual void Dispose(
bool disposing) {
59 string granter,
string grantee,
Privileges privileges,
bool withOption) {
60 RevokeAllGrants(queryContext, grantTable, objectType, objectName, granter, grantee, withOption);
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);
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);
116 t1 = t1.ExhaustiveSelect(queryContext, expr);
119 grantTable.Delete(t1);
127 UpdateGrants(
QueryContext, grantTable, objectType, objectName, granter, grantee, privileges, withOption);
129 ClearUserGrantsCache(grantee, objectType, objectName, withOption,
true);
134 if (grantsCache == null)
138 grantsCache.Remove(key);
142 if (grantsCache == null)
145 var keys = grantsCache.Keys.Where(x => x.userName.Equals(userName, StringComparison.OrdinalIgnoreCase));
146 foreach (var key
in keys) {
147 grantsCache.Remove(key);
152 if (String.IsNullOrEmpty(userName))
153 throw new ArgumentNullException(
"userName");
155 throw new ArgumentNullException(
"grant");
162 privileges |= oldPrivs;
164 if (!oldPrivs.Equals(privileges))
170 if (grantsCache == null) {
176 return grantsCache.TryGetValue(key, out privileges);
182 if (grantsCache == null)
183 grantsCache =
new Dictionary<GrantCacheKey, Privileges>();
185 grantsCache[key] = privileges;
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);
234 grantTable.Delete(t1);
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);
276 t1 = t1.ExhaustiveSelect(queryContext, expr);
281 foreach (var row
in t1) {
290 bool withOption,
bool withPublic) {
293 return QueryPrivileges(
QueryContext, grantTable, userName, objectType, objectName, withOption, withPublic);
297 bool withOption,
bool withPublic) {
299 return QueryPrivileges(
QueryContext, grantTable, groupName, objectType, objectName, withOption, withPublic);
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);
313 if (String.IsNullOrEmpty(userName))
314 throw new ArgumentNullException(
"userName");
324 throw new NotImplementedException();
328 throw new NotImplementedException();
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);
341 #region GrantCacheKey
350 this.userName = userName;
351 this.objectType = objectType;
352 this.objectName = objectName;
361 public override bool Equals(
object obj) {
363 return Equals(other);
367 return unchecked(((userName.GetHashCode() * objectName.GetHashCode()) ^ (
int)objectType) + options);
374 if (!String.Equals(userName, other.
userName, StringComparison.OrdinalIgnoreCase))
380 if (!String.Equals(objectName, other.
objectName, StringComparison.OrdinalIgnoreCase))
Defines the contract to access the data contained into a table of a database.
void GrantToUser(string userName, Grant grant)
GrantCacheKey(string userName, DbObjectType objectType, string objectName, bool withOption, bool withPublic)
Privileges QueryGroupPrivileges(string groupName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
static SqlBinaryExpression And(SqlExpression left, SqlExpression right)
Privileges Privileges
Gets the access privileges granted to the user.
const string PublicName
The name of the PUBLIC special user.
bool Equals(GrantCacheKey other)
static readonly ObjectName GroupGrantsTable
Privileges QueryUserPrivileges(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
Privileges GetGroupPrivileges(string groupName, DbObjectType objectType, ObjectName objectName)
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
Describes the name of an object within a database.
static readonly DataObject BooleanTrue
The representation of a BOOLEAN true as DataObject
static void RevokeAllGrants(IQuery queryContext, IMutableTable grantTable, DbObjectType objectType, ObjectName objectName, string revoker, string user, bool withOption=false)
void GrantToGroup(string groupName, Grant grant)
static DataObject String(string s)
static DataObject Boolean(SqlBoolean value)
DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
SqlExpressionType
All the possible type of SqlExpression supported
readonly DbObjectType objectType
bool TryGetPrivilegesFromCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic, out Privileges privileges)
override int GetHashCode()
string GranterName
Gets the name of the user that provided this grant.
void RevokeFromUser(string userName, Grant grant)
PrivilegeManager(IQuery queryContext)
static SqlBinaryExpression Or(SqlExpression left, SqlExpression right)
override bool Equals(object obj)
ObjectName ObjectName
Gets the fully qualified name of the object on which this grant provides access privileges to the use...
RowId AddRow(Row row)
Persists a new row to the table.
void UpdateUserGrants(DbObjectType objectType, ObjectName objectName, string granter, string grantee, Privileges privileges, bool withOption)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
void ClearUserGrantsCache(string userName)
Provides utilities and properties for handling the SYSTEN schema of a database.
string FullName
Gets the full reference name formatted.
virtual void Dispose(bool disposing)
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)
Dictionary< GrantCacheKey, Privileges > grantsCache
static DataObject BigInt(long value)
readonly string objectName
static readonly ObjectName UserGrantsTableName
static SqlReferenceExpression Reference(ObjectName objectName)
void RevokeFromGroup(string groupName, Grant grant)
Dictionary< string, Privileges > groupsPrivilegesCache
Defines the base class for instances that represent SQL expression tree nodes.
static Privileges QueryPrivileges(IQuery queryContext, ITable grantTable, string grantee, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
static SqlConstantExpression Constant(object value)
Privileges GetUserPrivileges(string userName, DbObjectType objectType, ObjectName objectName, bool withOption)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Provides the information for a user in a database system
bool WithOption
Gets a value indicating whether the grants include an option to grant to other users.
static void UpdateGrants(IQuery queryContext, IMutableTable grantTable, DbObjectType objectType, ObjectName objectName, string granter, string grantee, Privileges privileges, bool withOption)
DbObjectType ObjectType
Gets the type of the object on which to provide access privileges to the user.
An interface that defines contracts to alter the contents of a table.
The entity that holds the access control granted to an user or a group to a specific object in a data...
void ClearUserGrantsCache(string userName, DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)