2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
23 : base(
@"^create\s+user\s+(?'Name'[^\s]+)\s+(?'Roles'[^\s\{\}]+(\s+[^\s\{\}]+)*)(\s+(?'MetaData'\{[^\}]*\})(\s+(?'Password'[^\s]+))?)?$")
30 public override string Name =>
"Create";
40 public override async Task
Execute(
ChatState State,
string[] Arguments,
string OrgMessage, Match Details,
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;
50 throw new Exception(
"User already exists.");
52 foreach (
string RoleId
in RoleIds)
55 ??
throw new Exception(
"Role `" + RoleId +
"` does not exist.");
58 List<UserMetaData> MetaData =
new List<UserMetaData>();
60 if (!
string.IsNullOrEmpty(MetaDataStr))
64 if (Result is IDictionary<string, object> Obj)
66 foreach (KeyValuePair<string, object> P
in Obj)
71 Value = P.Value?.ToString() ??
string.Empty
77 if (
string.IsNullOrEmpty(Password))
80 await ResponseCallback(
"Generating password: `" + Password +
"`",
string.Empty);
82 else if (Password.Length < 16)
83 throw new Exception(
"Password too short.");
89 MetaData = MetaData.ToArray(),
93 await Persistence.Database.Insert(NewUser);
97 await ResponseCallback(
"User created: `" +
Name +
"`",
string.Empty);
108 new HelpItem(
"create user NAME ROLES[ METADATA[ PASSWORD]]",
"Creates a new user with the user name given by `NAME` and privileges defined by `ROLES`. `ROLES` is one or more roles separated by whitespace. `METADATA` is an object-exnihilo definition of meta-data associated with the user. `PASSWORD` is an optional password string to assign to the user account. If not provided, a random password is provided.")
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Helps with parsing of commong data types.
static readonly char[] WhiteSpace
Contains white-space characters.
Static class managing the runtime environment of the IoT Gateway.
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Implements an HTTP server.
static Variables CreateVariables()
Creates a new collection of variables, that contains access to the global set of variables.
Class managing a script expression.
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Corresponds to a role in the system.
Maintains the collection of all roles in the system.
static Task< Role > GetRole(string RoleId)
Gets the Role object corresponding to a Role ID.
Corresponds to a user in the system.
Maintains the collection of all users in the system.
static byte[] ComputeHash(string UserName, string Password)
Computes a hash of a password.
static async Task< User > GetUser(string UserName, bool CreateIfNew)
Gets the User object corresponding to a User Name.
static void ClearCache()
Clears internal caches.
An administrative command whose syntax is validated with a regular expression.
Contains an item of information about a command.
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
CreateUser()
Creates an account.
override HelpItem[] GetHelp()
Gets help about the command.
override string Name
Command name
Basic interface for a user.
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.