27 namespace Deveel.Data.Security {
38 if (!query.UserCanManageGroups())
39 throw new InvalidOperationException(String.Format(
"User '{0}' has not enough privileges to create a group.", query.UserName()));
41 query.Direct().UserManager().CreateUserGroup(groupName);
44 #region User Management
47 if (query.UserName().Equals(userName, StringComparison.OrdinalIgnoreCase))
48 return new User(userName);
50 if (!query.UserCanAccessUsers())
52 String.Format(
"The user '{0}' has not enough rights to access other users information.", query.UserName()));
54 if (!query.Direct().UserManager().UserExists(userName))
57 return new User(userName);
61 if (!queryContext.UserCanManageUsers())
63 String.Format(
"User '{0}' cannot change the status of user '{1}'", queryContext.UserName(), username));
65 queryContext.Direct().UserManager().SetUserStatus(username, status);
69 if (!queryContext.UserName().Equals(userName) &&
70 !queryContext.UserCanAccessUsers())
72 String.Format(
"The user '{0}' has not enough rights to access other users information.", queryContext.UserName()));
74 return queryContext.Direct().UserManager().GetUserStatus(userName);
78 if (!query.UserCanManageUsers())
80 String.Format(
"The user '{0}' has not enough rights to modify other users information.", query.UserName()));
84 var userGroups = query.Direct().UserManager().GetUserGroups(userName);
85 foreach (var userGroup
in userGroups) {
86 query.Direct().UserManager().RemoveUserFromGroup(userName, userGroup);
89 foreach (var userGroup
in groups) {
90 query.Direct().UserManager().AddUserToGroup(userName, userGroup,
false);
95 return query.Direct().UserManager().UserExists(userName);
99 if (!query.User().IsSystem)
100 throw new InvalidOperationException(
"The @PUBLIC user can be created only by the SYSTEM");
104 var userInfo =
new UserInfo(userName, userId);
106 query.Direct().UserManager().CreateUser(userInfo,
"####");
110 if (String.IsNullOrEmpty(userName))
111 throw new ArgumentNullException(
"userName");
112 if (String.IsNullOrEmpty(password))
113 throw new ArgumentNullException(
"password");
115 if (!query.UserCanCreateUsers())
117 String.Format(
"User '{0}' cannot create users.", query.UserName()));
119 if (String.Equals(userName,
User.
PublicName, StringComparison.OrdinalIgnoreCase))
120 throw new ArgumentException(
121 String.Format(
"User name '{0}' is reserved and cannot be registered.",
User.
PublicName),
"userName");
123 if (userName.Length <= 1)
124 throw new ArgumentException(
"User name must be at least one character.");
125 if (password.Length <= 1)
126 throw new ArgumentException(
"The password must be at least one character.");
129 if (c ==
'#' || c ==
'@' || c ==
'$' || c ==
'&')
130 throw new ArgumentException(
131 String.Format(
"User name '{0}' is invalid: cannot start with '{1}' character.", userName, c),
"userName");
134 var userInfo =
new UserInfo(userName, userId);
136 query.Direct().UserManager().CreateUser(userInfo, password);
138 return new User(userName);
142 if (!queryContext.UserCanAlterUser(username))
146 var userInfo =
new UserInfo(username, userId);
148 queryContext.Direct().UserManager().AlterUser(userInfo, password);
152 if (String.IsNullOrEmpty(userName))
153 throw new ArgumentNullException(
"userName");
155 if (!query.UserCanDropUser(userName))
158 return query.Direct().UserManager().DropUser(userName);
178 if (String.IsNullOrEmpty(username))
179 throw new ArgumentNullException(
"username");
180 if (String.IsNullOrEmpty(password))
181 throw new ArgumentNullException(
"password");
183 var userInfo = queryContext.Direct().UserManager().GetUser(username);
185 if (userInfo == null)
188 var userId = userInfo.Identification;
190 if (userId.Method !=
"plain")
191 throw new NotImplementedException();
193 if (!queryContext.Direct().UserManager().CheckIdentifier(username, password))
197 return new User(username);
200 }
catch (Exception ex) {
207 #region User Grants Management
209 public static void AddUserToGroup(
this IQuery queryContext,
string username,
string group,
bool asAdmin =
false) {
210 if (String.IsNullOrEmpty(@group))
211 throw new ArgumentNullException(
"group");
212 if (String.IsNullOrEmpty(username))
213 throw new ArgumentNullException(
"username");
215 if (!queryContext.UserCanAddToGroup(group))
218 queryContext.Direct().UserManager().AddUserToGroup(username, group, asAdmin);
222 var obj = query.FindObject(objectName);
226 query.GrantToUserOn(obj.ObjectType, obj.FullName, grantee, privileges, withOption);
233 if (!query.ObjectExists(objectType, objectName))
236 if (!query.UserHasGrantOption(objectType, objectName, privileges))
239 var granter = query.UserName();
240 var grant =
new Grant(privileges, objectName, objectType, granter, withOption);
241 query.Direct().PrivilegeManager().GrantToUser(grantee, grant);
250 throw new InvalidOperationException(
"Cannot grant to a system group.");
252 if (!query.UserCanManageGroups())
255 if (!query.ObjectExists(objectType, objectName))
258 var granter = query.UserName();
259 var grant =
new Grant(privileges, objectName, objectType, granter, withOption);
260 query.Direct().PrivilegeManager().GrantToGroup(groupName, grant);
264 if (query.Direct().UserManager().UserGroupExists(groupOrUserName)) {
268 query.GrantToGroupOn(objectType, objectName, groupOrUserName, privileges);
269 }
else if (query.Direct().UserManager().UserExists(groupOrUserName)) {
270 query.GrantToUserOn(objectType, objectName, groupOrUserName, privileges, withOption);
272 throw new SecurityException(String.Format(
"User or group '{0}' was not found.", groupOrUserName));
277 RevokeAllGrantsOn(query,
DbObjectType.Table, objectName);
287 var objectTypeColumn = grantTable.GetResolvedColumnName(1);
288 var objectNameColumn = grantTable.GetResolvedColumnName(2);
290 var t1 = grantTable.SimpleSelect(query, objectTypeColumn,
SqlExpressionType.Equal,
297 grantTable.Delete(t1);
301 query.GrantToUserOn(
DbObjectType.Table, tableName, grantee, privileges);
306 #region User Grants Query
309 return queryContext.Direct().UserManager().GetUserGroups(username);
313 return UserBelongsToGroup(queryContext, queryContext.UserName(), group);
317 return query.Direct().UserManager().IsUserInGroup(username, groupName);
321 return query.User().IsSystem || query.UserHasSecureAccess();
325 if (query.User().IsSystem)
328 return query.UserBelongsToSecureGroup();
336 var user = query.User();
340 if (query.UserBelongsToSecureGroup())
343 var grant = query.Direct().PrivilegeManager().GetUserPrivileges(user.Name, objectType, objectName,
true);
344 return (grant & privileges) != 0;
348 var user = query.User();
352 if (query.UserBelongsToSecureGroup())
355 var userName = user.Name;
356 var grant = query.Direct().PrivilegeManager().GetUserPrivileges(userName, objectType, objectName,
false);
357 return (grant & privileges) != 0;
361 return query.UserHasSecureAccess() ||
366 return query.UserHasSecureAccess() ||
368 query.UserName().Equals(userToDrop, StringComparison.OrdinalIgnoreCase);
372 if (query.UserName().Equals(userName))
375 if (userName.Equals(
User.
PublicName, StringComparison.OrdinalIgnoreCase))
378 return query.UserHasSecureAccess();
390 return query.UserHasPrivilege(
DbObjectType.Table, tableName, privileges);
397 return query.UserHasSecureAccess();
401 return query.UserHasSecureAccess();
405 return query.UserHasSchemaPrivilege(schemaName,
Privileges.Create);
409 var schema = tableName.
Parent;
411 return query.UserHasSecureAccess();
413 return query.UserCanCreateInSchema(schema.FullName);
417 if (query.UserHasSchemaPrivilege(schemaName,
Privileges.Alter))
420 return query.UserHasSecureAccess();
424 var schema = tableName.
Parent;
428 return query.UserCanAlterInSchema(schema.FullName);
432 return UserCanSelectFromTable(query, tableName,
new string[0]);
436 return query.UserHasTablePrivilege(tableName,
Privileges.References);
440 var selectedTables = queryPlan.DiscoverTableNames();
441 return selectedTables.All(query.UserCanSelectFromTable);
446 return query.UserHasTablePrivilege(tableName,
Privileges.Select);
451 return query.UserHasTablePrivilege(tableName,
Privileges.Update);
456 return query.UserHasTablePrivilege(tableName,
Privileges.Insert);
461 query.IsSystemFunction(invoke)) {
465 if (query.UserHasSecureAccess())
472 return query.UserCanExecute(
RoutineType.Function, invoke);
476 return query.UserCanExecute(
RoutineType.Procedure, invoke);
480 return query.UserHasPrivilege(objectType, objectName,
Privileges.Create);
484 return query.UserHasPrivilege(objectType, objectName,
Privileges.Drop);
488 return query.UserHasPrivilege(objectType, objectName,
Privileges.Alter);
492 return query.UserHasPrivilege(objectType, objectName,
Privileges.Select);
496 return query.UserHasTablePrivilege(tableName,
Privileges.Delete);
500 if (query.User().IsSystem)
503 if (query.UserBelongsToSecureGroup() ||
507 return query.Direct().UserManager().IsUserGroupAdmin(query.UserName(), groupName);
static bool UserCanAccessUsers(this IQuery query)
static void GrantToUserOnTable(this IQuery query, ObjectName tableName, string grantee, Privileges privileges)
static IUserManager UserManager(this IQuery query)
static User CreateUser(this IQuery query, string userName, string password)
static DataObject Integer(int value)
static bool IsSystemGroup(string groupName)
static bool UserCanSelectFromPlan(this IQuery query, IQueryPlanNode queryPlan)
static UserIdentification PlainText
static User Authenticate(this IQuery queryContext, string username, string password)
Authenticates the specified user using the provided credentials.
static void AddUserToGroup(this IQuery queryContext, string username, string group, bool asAdmin=false)
static bool UserHasPrivilege(this IQuery query, DbObjectType objectType, ObjectName objectName, Privileges privileges)
static bool UserCanAccessObject(this IQuery query, DbObjectType objectType, ObjectName objectName)
static bool UserCanDropObject(this IQuery query, DbObjectType objectType, ObjectName objectName)
static bool UserCanManageGroups(this IQuery query)
static void AlterUserPassword(this IQuery queryContext, string username, string password)
const string PublicName
The name of the PUBLIC special user.
static bool UserCanSelectFromTable(this IQuery query, ObjectName tableName)
static bool UserCanCreateTable(this IQuery query, ObjectName tableName)
static IPrivilegeManager PrivilegeManager(this IQuery query)
static string[] GetGroupsUserBelongsTo(this IQuery queryContext, string username)
static bool UserCanExecute(this IQuery query, RoutineType routineType, Invoke invoke)
static void RevokeAllGrantsOn(this IQuery query, DbObjectType objectType, ObjectName objectName)
static void GrantToGroupOn(this IQuery query, DbObjectType objectType, ObjectName objectName, string groupName, Privileges privileges, bool withOption=false)
static void CreateUserGroup(this IQuery query, string groupName)
Describes the name of an object within a database.
static void GrantToUserOn(this IQuery query, ObjectName objectName, string grantee, Privileges privileges, bool withOption=false)
static void GrantToUserOnSchema(this IQuery query, string schemaName, string grantee, Privileges privileges, bool withOption=false)
static bool UserCanCreateSchema(this IQuery query)
static void RevokeAllGrantsOnTable(this IQuery query, ObjectName objectName)
static DataObject String(string s)
SqlExpressionType
All the possible type of SqlExpression supported
static bool UserCanManageUsers(this IQuery query)
A node element of a query plan tree. /summary>
static bool UserCanCreateInSchema(this IQuery query, string schemaName)
RoutineType
The type of routine program.
static bool UserHasGrantOption(this IQuery query, DbObjectType objectType, ObjectName objectName, Privileges privileges)
static bool UserHasSecureAccess(this IQuery query)
static void SetUserGroups(this IQuery query, string userName, string[] groups)
static bool UserCanInsertIntoTable(this IQuery query, ObjectName tableName, params string[] columnNames)
const string UserManagerGroup
The name of the user manager group.
static bool UserCanAlterTable(this IQuery query, ObjectName tableName)
static bool UserCanDeleteFromTable(this IQuery query, ObjectName tableName)
static bool UserCanCreateObject(this IQuery query, DbObjectType objectType, ObjectName objectName)
static bool UserCanAddToGroup(this IQuery query, string groupName)
static bool UserCanCreateUsers(this IQuery query)
static void GrantToUserOn(this IQuery query, DbObjectType objectType, ObjectName objectName, string grantee, Privileges privileges, bool withOption=false)
static void GrantTo(this IQuery query, 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 UserBelongsToSecureGroup(this IQuery query)
The information about the invocation of a routine, including the full name and arguments (as SqlExpre...
static bool UserCanExecuteFunction(this IQuery query, Invoke invoke)
Provides utilities and properties for handling the SYSTEN schema of a database.
static bool UserHasTablePrivilege(this IQuery query, ObjectName tableName, Privileges privileges)
static User GetUser(this IQuery query, string userName)
string FullName
Gets the full reference name formatted.
static bool UserExists(this IQuery query, string userName)
static bool UserCanDropUser(this IQuery query, string userToDrop)
static bool UserCanAlterInSchema(this IQuery query, string schemaName)
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
static readonly ObjectName UserGrantsTableName
ObjectName RoutineName
Gets the fully qualified name of the routine to invoke.
static void RevokeAllGrantsOnView(this IQuery query, ObjectName objectName)
static bool UserCanUpdateTable(this IQuery query, ObjectName tableName, params string[] columnNames)
static bool UserBelongsToGroup(this IQuery queryContext, string group)
const string SecureGroup
THe name of the secure access group.
static bool UserCanSelectFromTable(this IQuery query, ObjectName tableName, params string[] columnNames)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static bool UserBelongsToGroup(this IQuery query, string username, string groupName)
DbObjectType
The kind of objects that can be handled by a database system and its managers
static bool DeleteUser(this IQuery query, string userName)
Provides the information for a user in a database system
static bool UserCanExecuteProcedure(this IQuery query, Invoke invoke)
static bool UserCanReferenceTable(this IQuery query, ObjectName tableName)
static bool UserCanAlterUser(this IQuery query, string userName)
static bool UserCanAlterObject(this IQuery query, DbObjectType objectType, ObjectName objectName)
static void SetUserStatus(this IQuery queryContext, string username, UserStatus status)
static bool UserHasSchemaPrivilege(this IQuery query, string schemaName, Privileges privileges)
new IQueryContext Context
static UserStatus GetUserStatus(this IQuery queryContext, string userName)
The entity that holds the access control granted to an user or a group to a specific object in a data...
const string SystemName
The name of the SYSTEM special user.
static void CreatePublicUser(this IQuery query)