Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Account.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Text.RegularExpressions;
5using System.Threading.Tasks;
6using Waher.Events;
13
15{
19 [CollectionName("BrokerAccounts")]
20 [TypeName(TypeNameSerialization.None)]
21 [ObsoleteMethod(nameof(ObsoleteProperties))]
22 [ArchivingTime]
23 [Index("UserName")]
24 [Index("ApiKey", "UserName")]
25 [Index("EMail", "UserName")]
26 [Index("PhoneNr", "UserName")]
27 public class Account : IAccount
28 {
29 internal static readonly Regex FromSaveUnsaved = new Regex(@"Waher[.]Persistence[.]Files[.]ObjectBTreeFile[.+]<SaveUnsaved>\w*[.]\w*",
30 RegexOptions.Compiled | RegexOptions.Singleline);
31 internal static readonly Regex FromUpdateObject = new Regex(@"Waher[.]Persistence[.]Files[.]ObjectBTreeFile[.+]<UpdateObject>\w*[.]\w*",
32 RegexOptions.Compiled | RegexOptions.Singleline);
33 private static readonly object[] approvedSources = new object[]
34 {
35 typeof(XmppServerModule).Assembly,
36 typeof(XmppServer).Assembly,
37 typeof(SmtpServer).Assembly,
38 typeof(Persistence.NeuroLedger.NeuroLedgerProvider),
39 typeof(Networking.SASL.AuthenticationMechanism).Assembly,
40 typeof(Content.Markdown.Web.MarkdownToHtmlConverter),
41 FromSaveUnsaved,
42 FromUpdateObject
43 };
44
45 private string objectId = null;
46 private string apiKey = string.Empty;
48 private string password = string.Empty;
51 private CaseInsensitiveString latestIdentity = CaseInsensitiveString.Empty;
55 private CaseInsensitiveString personalNumber = CaseInsensitiveString.Empty;
62 private bool enabled = true;
63 private bool canRelayMessages = false;
64 private DateTime created = DateTime.MinValue;
65 private DateTime? eMailVerified = null;
66 private DateTime? phoneNrVerified = null;
67 private AccountLogin accountLogin = null;
68
72 public Account()
73 {
74 }
75
79 [ObjectId]
80 public string ObjectId
81 {
82 get => this.objectId;
83 set => this.objectId = value;
84 }
85
89 public string ApiKey
90 {
91 get => this.apiKey;
92 set => this.apiKey = value;
93 }
94
99 {
100 get => this.userName;
101 set => this.userName = value;
102 }
103
107 [DefaultValueStringEmpty]
108 public string Password
109 {
110 get
111 {
112 Assert.CallFromSource(approvedSources);
113 return this.password;
114 }
115
116 set
117 {
118 if (!string.IsNullOrEmpty(this.password))
119 Assert.CallFromSource(approvedSources);
120
121 this.password = value;
122 }
123 }
124
129 {
130 get => this.eMail;
131 set => this.eMail = value;
132 }
133
138 {
139 get => this.phoneNr;
140 set => this.phoneNr = value;
141 }
142
147 [DefaultValueStringEmpty]
149 {
150 get => this.latestIdentity;
151 set => this.latestIdentity = value;
152 }
153
157 [DefaultValueStringEmpty]
159 {
160 get => this.firstName;
161 set => this.firstName = value;
162 }
163
167 [DefaultValueStringEmpty]
169 {
170 get => this.middleNames;
171 set => this.middleNames = value;
172 }
173
177 [DefaultValueStringEmpty]
179 {
180 get => this.lastNames;
181 set => this.lastNames = value;
182 }
183
187 [DefaultValueStringEmpty]
189 {
190 get => this.personalNumber;
191 set => this.personalNumber = value;
192 }
193
197 [DefaultValueStringEmpty]
199 {
200 get => this.country;
201 set => this.country = value;
202 }
203
207 [DefaultValueStringEmpty]
209 {
210 get => this.orgNumber;
211 set => this.orgNumber = value;
212 }
213
217 [DefaultValueStringEmpty]
219 {
220 get => this.orgName;
221 set => this.orgName = value;
222 }
223
227 [DefaultValueStringEmpty]
229 {
230 get => this.orgRole;
231 set => this.orgRole = value;
232 }
233
237 [DefaultValueStringEmpty]
239 {
240 get => this.orgDepartment;
241 set => this.orgDepartment = value;
242 }
243
247 [DefaultValueStringEmpty]
249 {
250 get => this.orgCountry;
251 set => this.orgCountry = value;
252 }
253
257 [DefaultValueDateTimeMinValue]
258 public DateTime Created
259 {
260 get => this.created;
261 set => this.created = value;
262 }
263
267 public DateTime? EMailVerified
268 {
269 get => this.eMailVerified;
270 set => this.eMailVerified = value;
271 }
272
276 public DateTime? PhoneNrVerified
277 {
278 get => this.phoneNrVerified;
279 set => this.phoneNrVerified = value;
280 }
281
285 [DefaultValue(true)]
286 public bool Enabled
287 {
288 get => this.enabled;
289 set => this.enabled = value;
290 }
291
295 [DefaultValue(false)]
297 {
298 get => this.canRelayMessages;
299 set => this.canRelayMessages = value;
300 }
301
307 public bool HasPrivilege(string PrivilegeID)
308 {
309 switch (PrivilegeID)
310 {
311 case SmtpServer.SmtpRelayPrivilegeID: return this.canRelayMessages;
312 default: return false;
313 }
314 }
315
320 public async void ObsoleteProperties(Dictionary<string, object> Properties)
321 {
322 try
323 {
324 AccountLogin Login = await this.GetAccountLogin();
325 bool Updated = false;
326
327 if (string.IsNullOrEmpty(Login.RemoteEndpoint) &&
328 Properties.TryGetValue("RemoteEndpoint", out object Obj) &&
329 Obj is string RemoteEndpoint)
330 {
331 Login.RemoteEndpoint = RemoteEndpoint;
332 Updated = true;
333 }
334
335 if (Login.LastLogin == DateTime.MinValue &&
336 Properties.TryGetValue("LastLogin", out Obj) &&
337 Obj is DateTime LastLogin)
338 {
339 Login.LastLogin = LastLogin;
340 Updated = true;
341 }
342
343 if (Updated)
344 await Database.UpdateLazy(Login);
345 }
346 catch (Exception ex)
347 {
348 Log.Exception(ex);
349 }
350 }
351
356 public async Task LoggedIn(string RemoteEndpoint)
357 {
358 AccountLogin Login = await this.GetAccountLogin();
359
360 Login.LastLogin = DateTime.Now;
361 Login.RemoteEndpoint = RemoteEndpoint;
362
363 await Database.UpdateLazy(Login);
364 }
365
370 public async Task<AccountLogin> GetAccountLogin()
371 {
372 if (!(this.accountLogin is null))
373 return this.accountLogin;
374
375 this.accountLogin = await Database.FindFirstDeleteRest<AccountLogin>(new FilterFieldEqualTo("UserName", this.userName));
376 if (!(this.accountLogin is null))
377 return this.accountLogin;
378
379 this.accountLogin = new AccountLogin()
380 {
381 UserName = this.userName,
382 RemoteEndpoint = string.Empty,
383 LastLogin = DateTime.MinValue
384 };
385
386 await Database.InsertLazy(this.accountLogin);
387
388 return this.accountLogin;
389 }
390
394 public string FullName => Concat(" ", this.firstName, this.middleNames, this.lastNames);
395
399 public string PersonalInformation => Concat(", ", this.FullName, this.personalNumber, this.country);
400
404 public string OrganizationInformation => Concat(", ", this.orgRole, this.orgDepartment, this.orgName, this.orgNumber, this.orgCountry);
405
406 private static string Concat(string Delimiter, params CaseInsensitiveString[] Segments)
407 {
408 StringBuilder sb = new StringBuilder();
409 bool First = true;
410
411 foreach (string Segment in Segments)
412 Append(sb, ref First, Segment, Delimiter);
413
414 return sb.ToString();
415 }
416
417 private static void Append(StringBuilder sb, ref bool First, CaseInsensitiveString Segment, string Delimiter)
418 {
419 if (!string.IsNullOrEmpty(Segment))
420 {
421 if (First)
422 First = false;
423 else
424 sb.Append(Delimiter);
425
426 sb.Append(Segment.Value);
427 }
428 }
429
430 }
431}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Implements a simple SMTP Server, as defined in:
Definition: SmtpServer.cs:44
const string SmtpRelayPrivilegeID
SmtpRelay
Definition: SmtpServer.cs:68
Represents a case-insensitive string.
string Value
String-representation of the case-insensitive string. (Representation is case sensitive....
static readonly CaseInsensitiveString Empty
Empty case-insensitive 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 InsertLazy(object Object)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
Definition: Database.cs:156
static async Task UpdateLazy(object Object)
Updates an object in the database, if unlocked. If locked, object will be updated at next opportunity...
Definition: Database.cs:687
This filter selects objects that have a named field equal to a given value.
Static class containing methods that can be used to make sure calls are made from appropriate locatio...
Definition: Assert.cs:15
static void CallFromSource(params string[] Sources)
Makes sure the call is made from one of the listed sources.
Definition: Assert.cs:39
Contains information about a broker account.
Definition: Account.cs:28
DateTime? PhoneNrVerified
When Phone Number was verified.
Definition: Account.cs:277
CaseInsensitiveString PersonalNumber
Phone Number associated with account holder.
Definition: Account.cs:189
CaseInsensitiveString EMail
E-mail address associated with account.
Definition: Account.cs:129
CaseInsensitiveString UserName
User Name of account
Definition: Account.cs:99
bool Enabled
If account is enabled
Definition: Account.cs:287
CaseInsensitiveString OrgRole
Organization role associated with account holder.
Definition: Account.cs:229
async void ObsoleteProperties(Dictionary< string, object > Properties)
Method called when loading the object and properties were found that are no longer recognized.
Definition: Account.cs:320
CaseInsensitiveString FirstName
First name associated with account holder.
Definition: Account.cs:159
string PersonalInformation
Personal information of account holder.
Definition: Account.cs:399
async Task< AccountLogin > GetAccountLogin()
Gets the object with the associated account login status information.
Definition: Account.cs:370
DateTime? EMailVerified
When e-Mail was verified.
Definition: Account.cs:268
string Password
Password of account
Definition: Account.cs:109
CaseInsensitiveString Country
Country code associated with account holder.
Definition: Account.cs:199
bool HasPrivilege(string PrivilegeID)
If account has a certain privilege.
Definition: Account.cs:307
string OrganizationInformation
Organizational information of account holder.
Definition: Account.cs:404
CaseInsensitiveString LastNames
Last name(s) associated with account holder.
Definition: Account.cs:179
CaseInsensitiveString PhoneNr
Phone number associated with account.
Definition: Account.cs:138
Account()
Contains information about a broker account.
Definition: Account.cs:72
CaseInsensitiveString OrgDepartment
Organization department associated with account holder.
Definition: Account.cs:239
bool CanRelayMessages
If account is allowed to relay e-mail messages
Definition: Account.cs:297
CaseInsensitiveString OrgNumber
Organization number associated with account holder.
Definition: Account.cs:209
string FullName
Full name of account holder.
Definition: Account.cs:394
async Task LoggedIn(string RemoteEndpoint)
Registers a log-in event on the account.
Definition: Account.cs:356
DateTime Created
When account was created associated with account holder.
Definition: Account.cs:259
CaseInsensitiveString OrgName
Organization name associated with account holder.
Definition: Account.cs:219
CaseInsensitiveString OrgCountry
Country code of organization associated with account holder.
Definition: Account.cs:249
CaseInsensitiveString MiddleNames
Middle name(s) associated with account holder.
Definition: Account.cs:169
CaseInsensitiveString LatestIdentity
Last Legal Identity approved for account. Note: Identity object might not be approved any longer.
Definition: Account.cs:149
Service Module hosting the XMPP broker and its components.
Interface for XMPP user accounts.
Definition: IAccount.cs:9
TypeNameSerialization
How the type name should be serialized.