Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PeerReviewConfiguration.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
12
14{
19 {
20 private static PeerReviewConfiguration instance = null;
21
22 private string[] whiteList = Array.Empty<string>();
23 private int nrReviewersToApprove = 2;
24 private int nrPhotosRequired = 1;
25 private bool allowPeerReview = false;
26 private bool requireFirstName = false;
27 private bool requireMiddleName = false;
28 private bool requireLastName = false;
29 private bool requirePersonalNumber = false;
30 private bool requireCountry = false;
31 private bool requireRegion = false;
32 private bool requireCity = false;
33 private bool requireArea = false;
34 private bool requirePostalCode = false;
35 private bool requireAddress = false;
36 private bool requireIso3166Compliance = false;
37 private bool requireNationality = false;
38 private bool requireGender = false;
39 private bool requireBirthDate = false;
40
41 private HttpResource peerReview = null;
42
48 {
49 get => this.nrReviewersToApprove;
50 set => this.nrReviewersToApprove = value;
51 }
52
57 {
58 get => this.nrPhotosRequired;
59 set => this.nrPhotosRequired = value;
60 }
61
65 [DefaultValue(false)]
66 public bool AllowPeerReview
67 {
68 get => this.allowPeerReview;
69 set => this.allowPeerReview = value;
70 }
71
75 [DefaultValue(false)]
76 public bool RequireFirstName
77 {
78 get => this.requireFirstName;
79 set => this.requireFirstName = value;
80 }
81
85 [DefaultValue(false)]
87 {
88 get => this.requireMiddleName;
89 set => this.requireMiddleName = value;
90 }
91
95 [DefaultValue(false)]
96 public bool RequireLastName
97 {
98 get => this.requireLastName;
99 set => this.requireLastName = value;
100 }
101
105 [DefaultValue(false)]
107 {
108 get => this.requirePersonalNumber;
109 set => this.requirePersonalNumber = value;
110 }
111
115 [DefaultValue(false)]
116 public bool RequireCountry
117 {
118 get => this.requireCountry;
119 set => this.requireCountry = value;
120 }
121
125 [DefaultValue(false)]
126 public bool RequireRegion
127 {
128 get => this.requireRegion;
129 set => this.requireRegion = value;
130 }
131
135 [DefaultValue(false)]
136 public bool RequireCity
137 {
138 get => this.requireCity;
139 set => this.requireCity = value;
140 }
141
145 [DefaultValue(false)]
146 public bool RequireArea
147 {
148 get => this.requireArea;
149 set => this.requireArea = value;
150 }
151
155 [DefaultValue(false)]
157 {
158 get => this.requirePostalCode;
159 set => this.requirePostalCode = value;
160 }
161
165 [DefaultValue(false)]
166 public bool RequireAddress
167 {
168 get => this.requireAddress;
169 set => this.requireAddress = value;
170 }
171
176 [DefaultValue(false)]
178 {
179 get => this.requireIso3166Compliance;
180 set => this.requireIso3166Compliance = value;
181 }
182
186 [DefaultValue(false)]
188 {
189 get => this.requireNationality;
190 set => this.requireNationality = value;
191 }
192
196 [DefaultValue(false)]
197 public bool RequireGender
198 {
199 get => this.requireGender;
200 set => this.requireGender = value;
201 }
202
206 [DefaultValue(false)]
208 {
209 get => this.requireBirthDate;
210 set => this.requireBirthDate = value;
211 }
212
216 [DefaultValueNull]
217 public string[] WhiteList
218 {
219 get => this.whiteList ?? Array.Empty<string>();
220 set
221 {
223
224 if (!(value is null))
225 {
226 foreach (string s in value)
227 {
228 string s2 = s.Trim();
229 if (!string.IsNullOrEmpty(s2))
230 List.Add(s2);
231 }
232 }
233
234 this.whiteList = List.ToArray();
235 }
236 }
237
241 public bool UsesWhiteList => (this.whiteList?.Length ?? 0) > 0;
242
250 public bool IsReviewerAllowed(string Jid, string LegalId)
251 {
252 if (!this.UsesWhiteList)
253 return true;
254
255 foreach (string Item in this.WhiteList)
256 {
257 if (string.Compare(Jid, Item, true) == 0 ||
258 string.Compare(LegalId, Item, true) == 0)
259 {
260 return true;
261 }
262 }
263
264 return false;
265 }
266
270 public static PeerReviewConfiguration Instance => instance;
271
275 public override string Resource => "/Settings/PeerReview.md";
276
280 public override int Priority => 380;
281
287 public override Task<string> Title(Language Language)
288 {
289 return Language.GetStringAsync(typeof(XmppServerModule), 39, "Peer Review");
290 }
291
295 public override Task ConfigureSystem()
296 {
297 return Task.CompletedTask;
298 }
299
304 public override void SetStaticInstance(ISystemConfiguration Configuration)
305 {
306 instance = Configuration as PeerReviewConfiguration;
307 }
308
313 public override Task InitSetup(HttpServer WebServer)
314 {
315 this.peerReview = WebServer.Register("/Settings/PeerReview", null, this.PeerReview, true, false, true);
316
317 return base.InitSetup(WebServer);
318 }
319
324 public override Task UnregisterSetup(HttpServer WebServer)
325 {
326 WebServer.Unregister(this.peerReview);
327
328 return base.UnregisterSetup(WebServer);
329 }
330
334 protected override string ConfigPrivilege => "Admin.Notarius.PeerReview";
335
336 private async Task PeerReview(HttpRequest Request, HttpResponse Response)
337 {
338 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
339
340 if (!Request.HasData)
341 {
342 await Response.SendResponse(new BadRequestException());
343 return;
344 }
345
346 ContentResponse Content = await Request.DecodeDataAsync();
347 if (Content.HasError ||
348 !(Content.Decoded is Dictionary<string, object> Form) ||
349 !Form.TryGetValue("allowPeerReview", out object Obj) || !(Obj is bool AllowPeerReview) ||
350 !Form.TryGetValue("nrReviewersToApprove", out Obj) || !(Obj is string NrReviewersToApproveStr) ||
351 !int.TryParse(NrReviewersToApproveStr, out int NrReviewersToApprove) || NrReviewersToApprove <= 0 ||
352 !Form.TryGetValue("nrPhotosRequired", out Obj) || !(Obj is string NrPhotosToApproveStr) ||
353 !int.TryParse(NrPhotosToApproveStr, out int NrPhotosToApprove) || NrPhotosToApprove < 0 ||
354 !Form.TryGetValue("requireFirstName", out Obj) || !(Obj is bool RequireFirstName) ||
355 !Form.TryGetValue("requireMiddleName", out Obj) || !(Obj is bool RequireMiddleName) ||
356 !Form.TryGetValue("requireLastName", out Obj) || !(Obj is bool RequireLastName) ||
357 !Form.TryGetValue("requirePersonalNumber", out Obj) || !(Obj is bool RequirePersonalNumber) ||
358 !Form.TryGetValue("requireCountry", out Obj) || !(Obj is bool RequireCountry) ||
359 !Form.TryGetValue("requireRegion", out Obj) || !(Obj is bool RequireRegion) ||
360 !Form.TryGetValue("requireCity", out Obj) || !(Obj is bool RequireCity) ||
361 !Form.TryGetValue("requireArea", out Obj) || !(Obj is bool RequireArea) ||
362 !Form.TryGetValue("requirePostalCode", out Obj) || !(Obj is bool RequirePostalCode) ||
363 !Form.TryGetValue("requireAddress", out Obj) || !(Obj is bool RequireAddress) ||
364 !Form.TryGetValue("requireIso3166Compliance", out Obj) || !(Obj is bool RequireIso3166Compliance) ||
365 !Form.TryGetValue("requireNationality", out Obj) || !(Obj is bool RequireNationality) ||
366 !Form.TryGetValue("requireGender", out Obj) || !(Obj is bool RequireGender) ||
367 !Form.TryGetValue("requireBirthDate", out Obj) || !(Obj is bool RequireBirthDate) ||
368 !Form.TryGetValue("whiteList", out Obj) || !(Obj is string WhiteList))
369 {
370 await Response.SendResponse(new BadRequestException());
371 return;
372 }
373
374 this.allowPeerReview = AllowPeerReview;
375 this.nrReviewersToApprove = NrReviewersToApprove;
376 this.nrPhotosRequired = NrPhotosToApprove;
377 this.requireFirstName = RequireFirstName;
378 this.requireMiddleName = RequireMiddleName;
379 this.requireLastName = RequireLastName;
380 this.requirePersonalNumber = RequirePersonalNumber;
381 this.requireCountry = RequireCountry;
382 this.requireRegion = RequireRegion;
383 this.requireCity = RequireCity;
384 this.requireArea = RequireArea;
385 this.requirePostalCode = RequirePostalCode;
386 this.requireAddress = RequireAddress;
387 this.requireIso3166Compliance = RequireIso3166Compliance;
388 this.requireNationality = RequireNationality;
389 this.requireGender = RequireGender;
390 this.requireBirthDate = RequireBirthDate;
391 this.WhiteList = WhiteList.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries);
392
393 await Database.Update(this);
394
395 Response.StatusCode = 200;
396 Response.StatusMessage = "OK";
397
398 await Response.SendResponse();
399 }
400
405 public override Task<bool> SimplifiedConfiguration()
406 {
407 this.nrReviewersToApprove = 2;
408 this.nrPhotosRequired = 1;
409 this.allowPeerReview = true;
410 this.requireFirstName = true;
411 this.requireMiddleName = false;
412 this.requireLastName = true;
413 this.requirePersonalNumber = true;
414 this.requireCountry = true;
415 this.requireRegion = false;
416 this.requireCity = true;
417 this.requireArea = false;
418 this.requirePostalCode = true;
419 this.requireAddress = true;
420 this.requireIso3166Compliance = true;
421 this.requireNationality = false;
422 this.requireGender = false;
423 this.requireBirthDate = false;
424 this.whiteList = Array.Empty<string>();
425
426 return Task.FromResult(true);
427 }
428
433 public const string BROKER_REVIEW_USE = nameof(BROKER_REVIEW_USE);
434
438 public const string BROKER_REVIEW_NRPEERS = nameof(BROKER_REVIEW_NRPEERS);
439
443 public const string BROKER_REVIEW_NRPHOTOS = nameof(BROKER_REVIEW_NRPHOTOS);
444
448 public const string BROKER_REVIEW_FIRST = nameof(BROKER_REVIEW_FIRST);
449
453 public const string BROKER_REVIEW_MIDDLE = nameof(BROKER_REVIEW_MIDDLE);
454
458 public const string BROKER_REVIEW_LAST = nameof(BROKER_REVIEW_LAST);
459
463 public const string BROKER_REVIEW_PNR = nameof(BROKER_REVIEW_PNR);
464
468 public const string BROKER_REVIEW_COUNTRY = nameof(BROKER_REVIEW_COUNTRY);
469
473 public const string BROKER_REVIEW_REGION = nameof(BROKER_REVIEW_REGION);
474
478 public const string BROKER_REVIEW_CITY = nameof(BROKER_REVIEW_CITY);
479
483 public const string BROKER_REVIEW_AREA = nameof(BROKER_REVIEW_AREA);
484
488 public const string BROKER_REVIEW_ZIP = nameof(BROKER_REVIEW_ZIP);
489
493 public const string BROKER_REVIEW_ADDR = nameof(BROKER_REVIEW_ADDR);
494
498 public const string BROKER_REVIEW_ISO3166 = nameof(BROKER_REVIEW_ISO3166);
499
504
508 public const string BROKER_REVIEW_GENDER = nameof(BROKER_REVIEW_GENDER);
509
513 public const string BROKER_REVIEW_BDATE = nameof(BROKER_REVIEW_BDATE);
514
521
526 public override Task<bool> EnvironmentConfiguration()
527 {
528 if (!this.TryGetEnvironmentVariable(BROKER_REVIEW_USE, false, out this.allowPeerReview))
529 return Task.FromResult(false);
530
531 if (!this.allowPeerReview)
532 return Task.FromResult(true);
533
534 if (!this.TryGetEnvironmentVariable(BROKER_REVIEW_NRPEERS, 1, int.MaxValue, true, ref this.nrReviewersToApprove) ||
535 !this.TryGetEnvironmentVariable(BROKER_REVIEW_NRPHOTOS, 0, int.MaxValue, true, ref this.nrPhotosRequired) ||
536 !this.TryGetEnvironmentVariable(BROKER_REVIEW_FIRST, true, out this.requireFirstName) ||
537 !this.TryGetEnvironmentVariable(BROKER_REVIEW_MIDDLE, true, out this.requireMiddleName) ||
538 !this.TryGetEnvironmentVariable(BROKER_REVIEW_LAST, true, out this.requireLastName) ||
539 !this.TryGetEnvironmentVariable(BROKER_REVIEW_PNR, true, out this.requirePersonalNumber) ||
540 !this.TryGetEnvironmentVariable(BROKER_REVIEW_COUNTRY, true, out this.requireCountry) ||
541 !this.TryGetEnvironmentVariable(BROKER_REVIEW_REGION, true, out this.requireRegion) ||
542 !this.TryGetEnvironmentVariable(BROKER_REVIEW_CITY, true, out this.requireCity) ||
543 !this.TryGetEnvironmentVariable(BROKER_REVIEW_AREA, true, out this.requireArea) ||
544 !this.TryGetEnvironmentVariable(BROKER_REVIEW_ZIP, true, out this.requirePostalCode) ||
545 !this.TryGetEnvironmentVariable(BROKER_REVIEW_ADDR, true, out this.requireAddress) ||
546 !this.TryGetEnvironmentVariable(BROKER_REVIEW_ISO3166, true, out this.requireIso3166Compliance) ||
547 !this.TryGetEnvironmentVariable(BROKER_REVIEW_NATIONALITY, true, out this.requireNationality) ||
548 !this.TryGetEnvironmentVariable(BROKER_REVIEW_GENDER, true, out this.requireGender) ||
549 !this.TryGetEnvironmentVariable(BROKER_REVIEW_BDATE, true, out this.requireBirthDate) ||
550 !this.TryGetEnvironmentVariable(BROKER_REVIEW_WHITELIST, true, out string WhiteList))
551 {
552 return Task.FromResult(false);
553 }
554
555 this.WhiteList = WhiteList.Split(',', StringSplitOptions.RemoveEmptyEntries);
556
557 return Task.FromResult(true);
558 }
559
560 }
561}
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
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
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
Abstract base class for multi-step system configurations.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Base class for all HTTP resources.
Definition: HttpResource.cs:23
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...
Implements an HTTP server.
Definition: HttpServer.cs:41
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1568
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1743
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
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Provides the user configuration options regarding peer-review of new legal identities.
const string BROKER_REVIEW_MIDDLE
If middle-name is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_PNR
If personal number is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_NRPEERS
Number of peers required to review and approve a legal identity application before it can be approved...
const string BROKER_REVIEW_GENDER
If gender is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_NATIONALITY
If nationality is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_WHITELIST
White-listed JIDs and/or Legal IDs allowed to perform peer review, separated by commas....
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
bool AllowPeerReview
If peer-review is allowed on the broker.
const string BROKER_REVIEW_LAST
If last-name is required in a peer review(true or 1), or not(false or 0).
bool RequireFirstName
If first name is required in applications for them to be peer-reviewable.
const string BROKER_REVIEW_FIRST
If first-name is required in a peer review (true or 1), or not(false or 0).
int NrReviewersToApprove
Number of peers required to review and approve a legal identity application before it can be approved...
bool RequireBirthDate
If birth date is required in applications for them to be peer-reviewable.
int NrPhotosRequired
Number of photos required in an application for it to be peer-reviewable.
override Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
bool RequirePostalCode
If postal code is required in applications for them to be peer-reviewable.
const string BROKER_REVIEW_ISO3166
If country codes must comply with ISO 3166 in a peer review(true or 1), or not(false or 0).
bool IsReviewerAllowed(string Jid, string LegalId)
Checks if a reviewer is allowed to perform peer review, based on the white-list, if configured.
bool RequireCity
If city is required in applications for them to be peer-reviewable.
override Task< bool > SimplifiedConfiguration()
Simplified configuration by configuring simple default values.
override int Priority
Priority of the setting. Configurations are sorted in ascending order.
override Task ConfigureSystem()
Is called during startup to configure the system.
static PeerReviewConfiguration Instance
Current instance of configuration.
bool RequireNationality
If nationality is required in applications for them to be peer-reviewable.
bool RequireMiddleName
If middle name(s) is/are required in applications for them to be peer-reviewable.
bool RequireRegion
If region is required in applications for them to be peer-reviewable.
bool RequireIso3166Compliance
If country codes are required to be ISO 3166 compliant in applications for them to be peer-reviewable...
const string BROKER_REVIEW_USE
If peer review is allowed on the broker(true or 1), or not(false or 0). If enabled,...
const string BROKER_REVIEW_ZIP
If postal code is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_ADDR
If address is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_NRPHOTOS
Number of photos required in a legal identity application for a peer review to be accepted.
string[] WhiteList
Peer Review White List (JIDS and/or Legal IDs), allowed to perform peer review.
bool RequireCountry
If country is required in applications for them to be peer-reviewable.
const string BROKER_REVIEW_REGION
If region is required in a peer review(true or 1), or not(false or 0).
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
override Task InitSetup(HttpServer WebServer)
Initializes the setup object.
const string BROKER_REVIEW_BDATE
If birth date is required in a peer review(true or 1), or not(false or 0).
bool RequirePersonalNumber
If personal number is required in applications for them to be peer-reviewable.
override Task< string > Title(Language Language)
Gets a title for the system configuration.
bool RequireAddress
If address is required in applications for them to be peer-reviewable.
const string BROKER_REVIEW_COUNTRY
If country is required in a peer review(true or 1), or not(false or 0).
bool RequireLastName
If last names is/are is required in applications for them to be peer-reviewable.
bool RequireArea
If area is required in applications for them to be peer-reviewable.
const string BROKER_REVIEW_AREA
If area is required in a peer review(true or 1), or not(false or 0).
bool RequireGender
If gender is required in applications for them to be peer-reviewable.
const string BROKER_REVIEW_CITY
If city is required in a peer review(true or 1), or not(false or 0).
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
Service Module hosting the XMPP broker and its components.
Interface for system configurations. The gateway will scan all module for system configuration classe...