Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UpdateUser.cs
1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
5using Waher.Content;
8using Waher.Script;
11
13{
17 public class UpdateUser : CommandRegEx
18 {
22 public UpdateUser()
23 : base(@"^update\s+user\s+(?'Name'[^\s]+)\s+(?'Roles'[^\s\{\}]+(\s+[^\s\{\}]+)*)(\s+(?'MetaData'\{[^\}]*\})(\s+(?'Password'[^\s]+))?)?$")
24 {
25 }
26
30 public override string Name => "Update";
31
40 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
41 ResponseCallbackHandler ResponseCallback)
42 {
43 string Name = Details.Groups["Name"].Value;
44 string[] RoleIds = Details.Groups["Roles"].Value.Split(CommonTypes.WhiteSpace, System.StringSplitOptions.RemoveEmptyEntries);
45 string MetaDataStr = Details.Groups["MetaData"].Value;
46 string Password = Details.Groups["Password"].Value;
47
48 IUser User0 = await Waher.Security.Users.Users.GetUser(Name, false)
49 ?? throw new Exception("User not found.");
50
51 User User = User0 as User
52 ?? throw new Exception("User not editable.");
53
54 foreach (string RoleId in RoleIds)
55 {
56 Role _ = await Waher.Security.Users.Roles.GetRole(RoleId, false)
57 ?? throw new Exception("Role `" + RoleId + "` does not exist.");
58 }
59
60 if (!string.IsNullOrEmpty(Password) && Password.Length < 16)
61 throw new Exception("Password too short.");
62
63 List<UserMetaData> MetaData = null;
64
65 if (!string.IsNullOrEmpty(MetaDataStr))
66 {
67 MetaData = new List<UserMetaData>();
68
69 Expression Exp = new Expression(MetaDataStr);
70 object Result = await Exp.EvaluateAsync(HttpServer.CreateVariables());
71 if (Result is IDictionary<string, object> Obj)
72 {
73 foreach (KeyValuePair<string, object> P in Obj)
74 {
75 MetaData.Add(new UserMetaData()
76 {
77 Name = P.Key,
78 Value = P.Value?.ToString() ?? string.Empty
79 });
80 }
81 };
82 }
83
84 User.RoleIds = RoleIds;
85
86 if (!(MetaData is null))
87 User.MetaData = MetaData.ToArray();
88
89 if (!string.IsNullOrEmpty(Password))
90 User.PasswordHash = Convert.ToBase64String(Waher.Security.Users.Users.ComputeHash(Name, Password));
91
92 await Persistence.Database.Update(User);
93
95
96 await ResponseCallback("User updated: `" + Name + "`", string.Empty);
97 }
98
103 public override HelpItem[] GetHelp()
104 {
105 return new HelpItem[]
106 {
107 new HelpItem("update user NAME ROLES[ METADATA[ PASSWORD]]", "Updates an existing user with the user name given by `NAME`. `ROLES` is one or more roles separated by whitespace. `METADATA` is an object-exnihilo definition of meta-data associated with the user. If not provided, existing meta-data is kept. `PASSWORD` is an optional password string to assign to the user account. If not provided, the existing password is kept.")
108 };
109 }
110 }
111}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static readonly char[] WhiteSpace
Contains white-space characters.
Definition: CommonTypes.cs:33
Implements an HTTP server.
Definition: HttpServer.cs:36
static Variables CreateVariables()
Creates a new collection of variables, that contains access to the global set of variables.
Definition: HttpServer.cs:1604
Class managing a script expression.
Definition: Expression.cs:39
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4275
Corresponds to a role in the system.
Definition: Role.cs:15
Maintains the collection of all roles in the system.
Definition: Roles.cs:14
static Task< Role > GetRole(string RoleId)
Gets the Role object corresponding to a Role ID.
Definition: Roles.cs:23
Corresponds to a user in the system.
Definition: User.cs:21
Contains a piece of meta-data information about a user.
Definition: UserMetaData.cs:11
Maintains the collection of all users in the system.
Definition: Users.cs:24
static byte[] ComputeHash(string UserName, string Password)
Computes a hash of a password.
Definition: Users.cs:157
static async Task< User > GetUser(string UserName, bool CreateIfNew)
Gets the User object corresponding to a User Name.
Definition: Users.cs:65
static void ClearCache()
Clears internal caches.
Definition: Users.cs:202
An administrative command whose syntax is validated with a regular expression.
Definition: CommandRegEx.cs:12
Contains an item of information about a command.
Definition: HelpItem.cs:9
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
Definition: UpdateUser.cs:40
override HelpItem[] GetHelp()
Gets help about the command.
Definition: UpdateUser.cs:103
Basic interface for a user.
Definition: IUser.cs:7
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.
Definition: App.xaml.cs:4