Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UpdateAccount.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
3using Waher.Content;
8
10{
12 {
13 public UpdateAccount()
14 : base("/UpdateAccount")
15 {
16 }
17
18 public override bool HandlesSubPaths => false;
19 public override bool UserSessions => true;
20 public bool AllowsPOST => true;
21
22 public async Task POST(HttpRequest Request, HttpResponse Response)
23 {
24 Gateway.AssertUserAuthenticated(Request, "Admin.Broker.Accounts");
25
26 if (!Request.HasData)
27 throw new BadRequestException();
28
29 object Obj = await Request.DecodeDataAsync();
30 if (!(Obj is Dictionary<string, string> Form))
31 throw new BadRequestException();
32
33 CaseInsensitiveString UserName;
34 string Password;
37 bool Enabled;
38 bool CanRelayMessages;
39
40 if (!Form.ContainsKey("UserName") || CaseInsensitiveString.IsNullOrEmpty(UserName = Form["UserName"]))
41 throw new BadRequestException();
42
43 if (!Form.ContainsKey("Password") || string.IsNullOrEmpty(Password = Form["Password"]))
44 throw new BadRequestException();
45
46 if (!Form.ContainsKey("EMail") || CaseInsensitiveString.IsNullOrEmpty(EMail = Form["EMail"]))
47 throw new BadRequestException();
48
49 if (!Form.ContainsKey("PhoneNr"))
50 throw new BadRequestException();
51
52 if (!Form.ContainsKey("Enabled"))
53 Enabled = false;
54 else if (!CommonTypes.TryParse(Form["Enabled"], out Enabled))
55 throw new BadRequestException();
56
57 if (!Form.ContainsKey("CanRelayMessages"))
58 CanRelayMessages = false;
59 else if (!CommonTypes.TryParse(Form["CanRelayMessages"], out CanRelayMessages))
60 throw new BadRequestException();
61
62 PhoneNr = Form["PhoneNr"];
63
64 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
65 Account Account = await PersistenceLayer.GetAccountEx(UserName)
66 ?? throw new NotFoundException();
67
68 Account.Password = Password;
69
70 if (Account.EMail != EMail)
71 {
72 Account.EMail = EMail;
73 Account.EMailVerified = null;
74 }
75
76 if (Account.PhoneNr != PhoneNr)
77 {
78 Account.PhoneNr = PhoneNr;
79 Account.PhoneNrVerified = null;
80 }
81
82 Account.Enabled = Enabled;
83 Account.CanRelayMessages = CanRelayMessages;
84
85 await Database.Update(Account);
86 await PersistenceLayer.AccountUpdated(UserName);
87
88 if (!(Request.Header.Referer is null))
89 throw new SeeOtherException(Request.Header.Referer.Value); // PRG pattern.
90 }
91 }
92}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3041
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
HttpFieldReferer Referer
Referer HTTP Field header. (RFC 2616, §14.36)
Represents an HTTP request.
Definition: HttpRequest.cs:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
The response to the request can be found under a different URI and SHOULD be retrieved using a GET me...
Represents a case-insensitive string.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
Contains information about a broker account.
Definition: Account.cs:28
CaseInsensitiveString EMail
E-mail address associated with account.
Definition: Account.cs:129
CaseInsensitiveString PhoneNr
Phone number associated with account.
Definition: Account.cs:138
Task AccountUpdated(CaseInsensitiveString UserName)
Called when account has been updated.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
bool AllowsPOST
If the POST method is allowed.
POST Interface for HTTP resources.