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;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Content;
10
12{
14 {
15 public UpdateAccount()
16 : base("/UpdateAccount")
17 {
18 }
19
20 public override bool HandlesSubPaths => false;
21 public override bool UserSessions => true;
22 public bool AllowsPOST => true;
23
24 public async Task POST(HttpRequest Request, HttpResponse Response)
25 {
26 Gateway.AssertUserAuthenticated(Request, "Admin.Broker.Accounts");
27
28 if (!Request.HasData)
29 {
30 await Response.SendResponse(new BadRequestException());
31 return;
32 }
33
34 ContentResponse Content = await Request.DecodeDataAsync();
35 if (Content.HasError || !(Content.Decoded is Dictionary<string, string> Form))
36 {
37 await Response.SendResponse(new BadRequestException());
38 return;
39 }
40
41 CaseInsensitiveString UserName;
42 string Password;
45 string[] RoleIds;
46 string FtpRootFolder;
47 bool Enabled;
48 bool CanRelayMessages;
49 bool FtpCanRead;
50 bool FtpCanWrite;
51
52 if (!Form.ContainsKey("UserName") || CaseInsensitiveString.IsNullOrEmpty(UserName = Form["UserName"]))
53 {
54 await Response.SendResponse(new BadRequestException("Missing user name."));
55 return;
56 }
57
58 if (!Form.ContainsKey("Password"))
59 Password = null;
60 else
61 Password = Form["Password"];
62
63 if (!Form.ContainsKey("EMail") || CaseInsensitiveString.IsNullOrEmpty(EMail = Form["EMail"]))
64 {
65 await Response.SendResponse(new BadRequestException("Missing e-Mail address."));
66 return;
67 }
68
69 if (!Form.ContainsKey("PhoneNr"))
70 {
71 await Response.SendResponse(new BadRequestException("Missing phone number field."));
72 return;
73 }
74
75 PhoneNr = Form["PhoneNr"];
76
77 if (!Form.ContainsKey("Enabled"))
78 Enabled = false;
79 else if (!CommonTypes.TryParse(Form["Enabled"], out Enabled))
80 {
81 await Response.SendResponse(new BadRequestException("Invalid value."));
82 return;
83 }
84
85 if (!Form.ContainsKey("CanRelayMessages"))
86 CanRelayMessages = false;
87 else if (!CommonTypes.TryParse(Form["CanRelayMessages"], out CanRelayMessages))
88 {
89 await Response.SendResponse(new BadRequestException("Invalid value."));
90 return;
91 }
92
93 if (!Form.ContainsKey("FtpRootFolder"))
94 {
95 await Response.SendResponse(new BadRequestException("Missing FTP Root folder field."));
96 return;
97 }
98
99 FtpRootFolder = Form["FtpRootFolder"];
100 if (!string.IsNullOrEmpty(FtpRootFolder) && !Directory.Exists(FtpRootFolder))
101 {
102 await Response.SendResponse(new BadRequestException("FTP Root folder does not exist or is not reachable."));
103 return;
104 }
105
106 long? FtpMaxStorage;
107
108 if (!Form.ContainsKey("FtpMaxStorage"))
109 FtpMaxStorage = null;
110 else
111 {
112 string s = Form["FtpMaxStorage"];
113
114 if (string.IsNullOrEmpty(s))
115 FtpMaxStorage = null;
116 else if (!long.TryParse(Form["FtpMaxStorage"], out long l) || l < 0)
117 {
118 await Response.SendResponse(new BadRequestException("Invalid value."));
119 return;
120 }
121 else
122 FtpMaxStorage = l;
123 }
124
125 if (!Form.ContainsKey("FtpCanRead"))
126 FtpCanRead = false;
127 else if (!CommonTypes.TryParse(Form["FtpCanRead"], out FtpCanRead))
128 {
129 await Response.SendResponse(new BadRequestException("Invalid value."));
130 return;
131 }
132
133 if (!Form.ContainsKey("FtpCanWrite"))
134 FtpCanWrite = false;
135 else if (!CommonTypes.TryParse(Form["FtpCanWrite"], out FtpCanWrite))
136 {
137 await Response.SendResponse(new BadRequestException("Invalid value."));
138 return;
139 }
140
141 if ((FtpCanRead || FtpCanWrite) && string.IsNullOrEmpty(FtpRootFolder))
142 {
143 await Response.SendResponse(new BadRequestException("FTP Root folder not provided."));
144 return;
145 }
146
147 if (!Form.TryGetValue("Roles", out string Roles))
148 Roles = string.Empty;
149
150 RoleIds = Roles.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries);
151
152 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
153 Account Account = await PersistenceLayer.GetAccountEx(UserName);
154 if (Account is null)
155 {
156 await Response.SendResponse(new NotFoundException());
157 return;
158 }
159
160 if (!string.IsNullOrEmpty(Password))
161 Account.Password = Password;
162
163 if (Account.EMail != EMail)
164 {
165 Account.EMail = EMail;
166 Account.EMailVerified = null;
167 }
168
169 if (Account.PhoneNr != PhoneNr)
170 {
171 Account.PhoneNr = PhoneNr;
172 Account.PhoneNrVerified = null;
173 }
174
175 Account.Enabled = Enabled;
176 Account.CanRelayMessages = CanRelayMessages;
177 Account.FtpRootFolder = FtpRootFolder;
178 Account.FtpMaxStorage = FtpMaxStorage;
179 Account.FtpCanRead = FtpCanRead;
180 Account.FtpCanWrite = FtpCanWrite;
181 Account.RoleIds = RoleIds;
182 Account.Updated = DateTime.UtcNow;
183
184 await Database.Update(Account);
185
186 if (!(Request.Header.Referer is null))
187 await Response.SendResponse(new SeeOtherException(Request.Header.Referer.Value)); // PRG pattern.
188 }
189 }
190}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static readonly char[] CRLF
Contains the CR LF character sequence.
Definition: CommonTypes.cs:19
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
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:3868
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:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
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:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
Contains information about a broker account.
Definition: Account.cs:41
CaseInsensitiveString EMail
E-mail address associated with account.
Definition: Account.cs:176
CaseInsensitiveString PhoneNr
Phone number associated with account.
Definition: Account.cs:185
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.