11 namespace Deveel.Data.Security {
21 #region Group Management
24 if (!context.UserCanManageGroups())
25 throw new InvalidOperationException(String.Format(
"User '{0}' has not enough privileges to create a group.", context.UserName()));
27 context.ForSystemUser().UserManager().CreateUserGroup(groupName);
32 #region User Management
35 if (context.UserName().Equals(userName, StringComparison.OrdinalIgnoreCase))
36 return new User(context, userName);
38 if (!context.UserCanAccessUsers())
40 String.Format(
"The user '{0}' has not enough rights to access other users information.", context.UserName()));
42 if (!context.ForSystemUser().UserManager().UserExists(userName))
45 return new User(context, userName);
49 if (!queryContext.UserCanManageUsers())
51 String.Format(
"User '{0}' cannot change the status of user '{1}'", queryContext.UserName(), username));
53 queryContext.ForSystemUser().UserManager().SetUserStatus(username, status);
57 if (!queryContext.UserName().Equals(userName) &&
58 !queryContext.UserCanAccessUsers())
60 String.Format(
"The user '{0}' has not enough rights to access other users information.", queryContext.UserName()));
62 return queryContext.ForSystemUser().UserManager().GetUserStatus(userName);
66 if (!context.UserCanManageUsers())
68 String.Format(
"The user '{0}' has not enough rights to modify other users information.", context.UserName()));
72 var userGroups = context.ForSystemUser().UserManager().GetUserGroups(userName);
73 foreach (var userGroup
in userGroups) {
74 context.ForSystemUser().UserManager().RemoveUserFromGroup(userName, userGroup);
77 foreach (var userGroup
in groups) {
78 context.ForSystemUser().UserManager().AddUserToGroup(userName, userGroup,
false);
83 return context.ForSystemUser().UserManager().UserExists(userName);
87 if (!context.User().IsSystem)
88 throw new InvalidOperationException(
"The @PUBLIC user can be created only by the SYSTEM");
92 var userInfo =
new UserInfo(userName, userId);
94 context.ForSystemUser().UserManager().CreateUser(userInfo,
"####");
98 if (String.IsNullOrEmpty(userName))
99 throw new ArgumentNullException(
"userName");
100 if (String.IsNullOrEmpty(password))
101 throw new ArgumentNullException(
"password");
103 if (!context.UserCanCreateUsers())
105 String.Format(
"User '{0}' cannot create users.", context.UserName()));
107 if (String.Equals(userName,
User.
PublicName, StringComparison.OrdinalIgnoreCase))
108 throw new ArgumentException(
109 String.Format(
"User name '{0}' is reserved and cannot be registered.",
User.
PublicName),
"userName");
111 if (userName.Length <= 1)
112 throw new ArgumentException(
"User name must be at least one character.");
113 if (password.Length <= 1)
114 throw new ArgumentException(
"The password must be at least one character.");
117 if (c ==
'#' || c ==
'@' || c ==
'$' || c ==
'&')
118 throw new ArgumentException(
119 String.Format(
"User name '{0}' is invalid: cannot start with '{1}' character.", userName, c),
"userName");
122 var userInfo =
new UserInfo(userName, userId);
124 context.ForSystemUser().UserManager().CreateUser(userInfo, password);
126 return new User(context, userName);
130 if (!queryContext.UserCanAlterUser(username))
134 var userInfo =
new UserInfo(username, userId);
136 queryContext.ForSystemUser().UserManager().AlterUser(userInfo, password);
140 if (String.IsNullOrEmpty(userName))
141 throw new ArgumentNullException(
"userName");
143 if (!context.UserCanDropUser(userName))
146 return context.ForSystemUser().UserManager().DropUser(userName);
153 var c1 = table.GetResolvedColumnName(0);
175 if (String.IsNullOrEmpty(username))
176 throw new ArgumentNullException(
"username");
177 if (String.IsNullOrEmpty(password))
178 throw new ArgumentNullException(
"password");
180 var userInfo = queryContext.ForSystemUser().UserManager().GetUser(username);
182 if (userInfo == null)
185 var userId = userInfo.Identification;
187 if (userId.Method !=
"plain")
188 throw new NotImplementedException();
190 if (!queryContext.ForSystemUser().UserManager().CheckIdentifier(username, password))
194 return new User(queryContext, username);
197 }
catch (Exception ex) {
202 #region User Grants Management
205 if (String.IsNullOrEmpty(@group))
206 throw new ArgumentNullException(
"group");
207 if (String.IsNullOrEmpty(username))
208 throw new ArgumentNullException(
"username");
210 if (!queryContext.UserCanAddToGroup(group))
213 queryContext.ForSystemUser().UserManager().AddUserToGroup(username, group, asAdmin);
217 var obj = context.FindObject(objectName);
221 context.GrantToUserOn(obj.ObjectType, obj.FullName, grantee, privileges, withOption);
228 if (!context.ObjectExists(objectType, objectName))
231 if (!context.UserHasGrantOption(objectType, objectName, privileges))
234 var granter = context.UserName();
235 var grant =
new Grant(privileges, objectName, objectType, granter, withOption);
236 context.ForSystemUser().PrivilegeManager().GrantToUser(grantee, grant);
245 throw new InvalidOperationException(
"Cannot grant to a system group.");
247 if (!context.UserCanManageGroups())
250 if (!context.ObjectExists(objectType, objectName))
253 var granter = context.UserName();
254 var grant =
new Grant(privileges, objectName, objectType, granter, withOption);
255 context.ForSystemUser().PrivilegeManager().GrantToGroup(groupName, grant);
259 if (context.ForSystemUser().UserManager().UserGroupExists(groupOrUserName)) {
263 context.GrantToGroupOn(objectType, objectName, groupOrUserName, privileges);
264 }
else if (context.ForSystemUser().UserManager().UserExists(groupOrUserName)) {
265 context.GrantToUserOn(objectType, objectName, groupOrUserName, privileges, withOption);
267 throw new SecurityException(String.Format(
"User or group '{0}' was not found.", groupOrUserName));
272 RevokeAllGrantsOn(context,
DbObjectType.Table, objectName);
276 context.RevokeAllGrantsOn(
DbObjectType.View, objectName);
282 var objectTypeColumn = grantTable.GetResolvedColumnName(1);
283 var objectNameColumn = grantTable.GetResolvedColumnName(2);
285 var t1 = grantTable.SimpleSelect(context, objectTypeColumn,
SqlExpressionType.Equal,
292 grantTable.Delete(t1);
296 context.GrantToUserOn(
DbObjectType.Table, tableName, grantee, privileges);
303 #region User Grants Query
306 return queryContext.ForSystemUser().UserManager().GetUserGroups(username);
310 return UserBelongsToGroup(queryContext, queryContext.UserName(), group);
314 return context.ForSystemUser().UserManager().IsUserInGroup(username, groupName);
318 return context.User().IsSystem || context.UserHasSecureAccess();
322 if (context.User().IsSystem)
325 return context.UserBelongsToSecureGroup();
333 var user = context.User();
337 if (context.UserBelongsToSecureGroup())
340 var grant = context.ForSystemUser().PrivilegeManager().GetUserPrivileges(user.Name, objectType, objectName,
true);
341 return (grant & privileges) != 0;
345 var user = context.User();
349 if (context.UserBelongsToSecureGroup())
352 var userName = user.
Name;
353 var grant = context.ForSystemUser().PrivilegeManager().GetUserPrivileges(userName, objectType, objectName,
false);
354 return (grant & privileges) != 0;
358 return context.UserHasSecureAccess() ||
363 return context.UserHasSecureAccess() ||
365 context.UserName().Equals(userToDrop, StringComparison.OrdinalIgnoreCase);
369 if (context.UserName().Equals(userName))
372 if (userName.Equals(
User.
PublicName, StringComparison.OrdinalIgnoreCase))
375 return context.UserHasSecureAccess();
387 return context.UserHasPrivilege(
DbObjectType.Table, tableName, privileges);
394 return context.UserHasSecureAccess();
398 return context.UserHasSecureAccess();
402 return context.UserHasSchemaPrivilege(schemaName,
Privileges.Create);
406 var schema = tableName.
Parent;
408 return context.UserHasSecureAccess();
410 return context.UserCanCreateInSchema(schema.FullName);
414 if (context.UserHasSchemaPrivilege(schemaName,
Privileges.Alter))
417 return context.UserHasSecureAccess();
421 var schema = tableName.
Parent;
425 return context.UserCanAlterInSchema(schema.FullName);
429 return UserCanSelectFromTable(context, tableName,
new string[0]);
433 return context.UserHasTablePrivilege(tableName,
Privileges.References);
437 var selectedTables = queryPlan.DiscoverTableNames();
438 return selectedTables.All(context.UserCanSelectFromTable);
443 return context.UserHasTablePrivilege(tableName,
Privileges.Select);
448 return context.UserHasTablePrivilege(tableName,
Privileges.Update);
453 return context.UserHasTablePrivilege(tableName,
Privileges.Insert);
458 context.IsSystemFunction(invoke)) {
462 if (context.UserHasSecureAccess())
469 return context.UserCanExecute(
RoutineType.Function, invoke);
473 return context.UserCanExecute(
RoutineType.Procedure, invoke);
477 return context.UserHasPrivilege(objectType, objectName,
Privileges.Create);
481 return context.UserHasPrivilege(objectType, objectName,
Privileges.Drop);
485 return context.UserHasPrivilege(objectType, objectName,
Privileges.Alter);
489 return context.UserHasPrivilege(objectType, objectName,
Privileges.Select);
493 return context.UserHasTablePrivilege(tableName,
Privileges.Delete);
497 if (context.User().IsSystem)
500 if (context.UserBelongsToSecureGroup() ||
504 return context.ForSystemUser().UserManager().IsUserGroupAdmin(context.UserName(), groupName);
static bool UserCanDeleteFromTable(this IQueryContext context, ObjectName tableName)
static bool UserCanDropObject(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static void RevokeAllGrantsOnTable(this IQueryContext context, ObjectName objectName)
static bool UserBelongsToGroup(this IQueryContext context, string username, string groupName)
static DataObject Integer(int value)
static bool UserCanExecuteProcedure(this IQueryContext context, Invoke invoke)
static bool IsSystemGroup(string groupName)
static void GrantToGroupOn(this IQueryContext context, DbObjectType objectType, ObjectName objectName, string groupName, Privileges privileges, bool withOption=false)
static bool UserHasTablePrivilege(this IQueryContext context, ObjectName tableName, Privileges privileges)
static UserIdentification PlainText
static void RevokeAllGrantsOnView(this IQueryContext context, ObjectName objectName)
static bool UserCanSelectFromPlan(this IQueryContext context, IQueryPlanNode queryPlan)
static bool UserCanCreateSchema(this IQueryContext context)
static User CreateUser(this IQueryContext context, string userName, string password)
static bool UserExists(this IQueryContext context, string userName)
const string PublicName
The name of the PUBLIC special user.
static bool UserCanCreateTable(this IQueryContext context, ObjectName tableName)
static void AlterUserPassword(this IQueryContext queryContext, string username, string password)
static void SetUserGroups(this IQueryContext context, string userName, string[] groups)
static bool UserCanAccessObject(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static User GetUser(this IQueryContext context, string userName)
static void GrantToUserOnSchema(this IQueryContext context, string schemaName, string grantee, Privileges privileges, bool withOption=false)
static void RemoveUserFromAllGroups(this IQueryContext context, string username)
static void AddUserToGroup(this IQueryContext queryContext, string username, string group, bool asAdmin=false)
static bool UserCanManageUsers(this IQueryContext context)
static IUserManager UserManager(this IQueryContext context)
static User Authenticate(this IQueryContext queryContext, string username, string password)
Authenticates the specified user using the provided credentials.
Provides a context for executing queries, accessing the system resources and evaluation context...
Describes the name of an object within a database.
static DataObject String(string s)
static bool UserCanAlterUser(this IQueryContext context, string userName)
static void GrantToUserOnTable(this IQueryContext context, ObjectName tableName, string grantee, Privileges privileges)
SqlExpressionType
All the possible type of SqlExpression supported
A node element of a query plan tree. /summary>
static UserStatus GetUserStatus(this IQueryContext queryContext, string userName)
static bool UserCanUpdateTable(this IQueryContext context, ObjectName tableName, params string[] columnNames)
static bool UserCanExecuteFunction(this IQueryContext context, Invoke invoke)
static bool UserCanDropUser(this IQueryContext context, string userToDrop)
RoutineType
The type of routine program.
static IPrivilegeManager PrivilegeManager(this IQueryContext context)
static bool DeleteUser(this IQueryContext context, string userName)
static bool UserHasGrantOption(this IQueryContext context, DbObjectType objectType, ObjectName objectName, Privileges privileges)
static readonly ObjectName UserGroupTableName
const string UserManagerGroup
The name of the user manager group.
static void GrantTo(this IQueryContext context, string groupOrUserName, DbObjectType objectType, ObjectName objectName, Privileges privileges, bool withOption=false)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
static bool UserCanAlterObject(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static void CreatePublicUser(this IQueryContext context)
static string[] GetGroupsUserBelongsTo(this IQueryContext queryContext, string username)
The information about the invocation of a routine, including the full name and arguments (as SqlExpre...
static bool UserCanAccessUsers(this IQueryContext context)
static bool UserBelongsToGroup(this IQueryContext queryContext, string group)
static bool UserCanAlterInSchema(this IQueryContext context, string schemaName)
static bool UserCanAlterTable(this IQueryContext context, ObjectName tableName)
Provides utilities and properties for handling the SYSTEN schema of a database.
string FullName
Gets the full reference name formatted.
static void RevokeAllGrantsOn(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static bool UserHasPrivilege(this IQueryContext context, DbObjectType objectType, ObjectName objectName, Privileges privileges)
static bool UserCanAddToGroup(this IQueryContext context, string groupName)
static void CreateUserGroup(this IQueryContext context, string groupName)
static bool UserCanSelectFromTable(this IQueryContext context, ObjectName tableName)
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
static void SetUserStatus(this IQueryContext queryContext, string username, UserStatus status)
static bool UserCanCreateInSchema(this IQueryContext context, string schemaName)
static bool UserCanCreateUsers(this IQueryContext context)
static bool UserCanInsertIntoTable(this IQueryContext context, ObjectName tableName, params string[] columnNames)
static bool UserBelongsToSecureGroup(this IQueryContext context)
static readonly ObjectName UserGrantsTableName
ObjectName RoutineName
Gets the fully qualified name of the routine to invoke.
static void GrantToUserOn(this IQueryContext context, ObjectName objectName, string grantee, Privileges privileges, bool withOption=false)
const string SecureGroup
THe name of the secure access group.
static bool UserHasSecureAccess(this IQueryContext context)
static bool UserCanCreateObject(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static bool UserHasSchemaPrivilege(this IQueryContext context, string schemaName, Privileges privileges)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
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
static bool UserCanExecute(this IQueryContext context, RoutineType routineType, Invoke invoke)
static bool UserCanSelectFromTable(this IQueryContext context, ObjectName tableName, params string[] columnNames)
static bool UserCanManageGroups(this IQueryContext context)
static void GrantToUserOn(this IQueryContext context, DbObjectType objectType, ObjectName objectName, string grantee, Privileges privileges, bool withOption=false)
The entity that holds the access control granted to an user or a group to a specific object in a data...
static bool UserCanReferenceTable(this IQueryContext context, ObjectName tableName)
const string SystemName
The name of the SYSTEM special user.