DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
AlterUserStatement.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
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 using System;
18 using System.IO;
19 using System.Linq;
20 
21 using Deveel.Data.Security;
24 
25 namespace Deveel.Data.Sql.Statements {
26  [Serializable]
27  public sealed class AlterUserStatement : SqlStatement, IPreparable {
28  public AlterUserStatement(string userName, IAlterUserAction alterAction) {
29  if (alterAction == null)
30  throw new ArgumentNullException("alterAction");
31  if (String.IsNullOrEmpty(userName))
32  throw new ArgumentNullException("userName");
33 
34  UserName = userName;
35  AlterAction = alterAction;
36  }
37 
39  UserName = data.GetString("UserName");
40  AlterAction = data.GetValue<IAlterUserAction>("Action");
41  }
42 
43  public string UserName { get; private set; }
44 
45  public IAlterUserAction AlterAction { get; private set; }
46 
48  var action = AlterAction;
49  if (action is IPreparable)
50  action = ((IPreparable)action).Prepare(preparer) as IAlterUserAction;
51 
52  return new AlterUserStatement(UserName, action);
53  }
54 
55  protected override void GetData(SerializeData data) {
56  data.SetValue("UserName", UserName);
57  data.SetValue("Action", AlterAction);
58  }
59 
60  protected override void ExecuteStatement(ExecutionContext context) {
61  if (AlterAction.ActionType == AlterUserActionType.SetPassword) {
62  var password = ((SqlConstantExpression)((SetPasswordAction)AlterAction).PasswordExpression).Value.ToString();
63  context.Request.Query.AlterUserPassword(UserName, password);
64  } else if (AlterAction.ActionType == AlterUserActionType.SetGroups) {
65  var groupNames = ((SetUserGroupsAction)AlterAction).Groups
66  .Cast<SqlConstantExpression>()
67  .Select(x => x.Value.Value.ToString())
68  .ToArray();
69 
70  context.Request.Query.SetUserGroups(UserName, groupNames);
71  } else if (AlterAction.ActionType == AlterUserActionType.SetAccountStatus) {
72  context.Request.Query.SetUserStatus(UserName, ((SetAccountStatusAction)AlterAction).Status);
73  }
74  }
75  }
76 }
AlterUserStatement(string userName, IAlterUserAction alterAction)
void SetValue(string key, Type type, object value)
override void ExecuteStatement(ExecutionContext context)
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
An interface used to prepare a SqlExpression object.
override void GetData(SerializeData data)
An expression that holds a constant value.
object Prepare(IExpressionPreparer preparer)
Converts the underlying value of this instance into an object that can be evaluated by an expression...
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...
Definition: IPreparable.cs:30