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 Paiwise;
2using System;
3using System.Collections.Generic;
4using System.Threading.Tasks;
5using Waher.Content;
18
20{
25 {
26 private static PeerReviewConfiguration instance = null;
27
28 private int nrReviewersToApprove = 2;
29 private int nrPhotosRequired = 1;
30 private bool allowPeerReview = false;
31 private bool requireFirstName = false;
32 private bool requireMiddleName = false;
33 private bool requireLastName = false;
34 private bool requirePersonalNumber = false;
35 private bool requireCountry = false;
36 private bool requireRegion = false;
37 private bool requireCity = false;
38 private bool requireArea = false;
39 private bool requirePostalCode = false;
40 private bool requireAddress = false;
41 private bool requireIso3166Compliance = false;
42 private bool requireNationality = false;
43 private bool requireGender = false;
44 private bool requireBirthDate = false;
45
46 private HttpResource peerReview = null;
47
48 public int NrReviewersToApprove
49 {
50 get => this.nrReviewersToApprove;
51 set => this.nrReviewersToApprove = value;
52 }
53
54 public int NrPhotosRequired
55 {
56 get => this.nrPhotosRequired;
57 set => this.nrPhotosRequired = value;
58 }
59
60 [DefaultValue(false)]
61 public bool AllowPeerReview
62 {
63 get => this.allowPeerReview;
64 set => this.allowPeerReview = value;
65 }
66
67 [DefaultValue(false)]
68 public bool RequireFirstName
69 {
70 get => this.requireFirstName;
71 set => this.requireFirstName = value;
72 }
73
74 [DefaultValue(false)]
75 public bool RequireMiddleName
76 {
77 get => this.requireMiddleName;
78 set => this.requireMiddleName = value;
79 }
80
81 [DefaultValue(false)]
82 public bool RequireLastName
83 {
84 get => this.requireLastName;
85 set => this.requireLastName = value;
86 }
87
88 [DefaultValue(false)]
89 public bool RequirePersonalNumber
90 {
91 get => this.requirePersonalNumber;
92 set => this.requirePersonalNumber = value;
93 }
94
95 [DefaultValue(false)]
96 public bool RequireCountry
97 {
98 get => this.requireCountry;
99 set => this.requireCountry = value;
100 }
101
102 [DefaultValue(false)]
103 public bool RequireRegion
104 {
105 get => this.requireRegion;
106 set => this.requireRegion = value;
107 }
108
109 [DefaultValue(false)]
110 public bool RequireCity
111 {
112 get => this.requireCity;
113 set => this.requireCity = value;
114 }
115
116 [DefaultValue(false)]
117 public bool RequireArea
118 {
119 get => this.requireArea;
120 set => this.requireArea = value;
121 }
122
123 [DefaultValue(false)]
124 public bool RequirePostalCode
125 {
126 get => this.requirePostalCode;
127 set => this.requirePostalCode = value;
128 }
129
130 [DefaultValue(false)]
131 public bool RequireAddress
132 {
133 get => this.requireAddress;
134 set => this.requireAddress = value;
135 }
136
137 [DefaultValue(false)]
138 public bool RequireIso3166Compliance
139 {
140 get => this.requireIso3166Compliance;
141 set => this.requireIso3166Compliance = value;
142 }
143
144 [DefaultValue(false)]
145 public bool RequireNationality
146 {
147 get => this.requireNationality;
148 set => this.requireNationality = value;
149 }
150
151 [DefaultValue(false)]
152 public bool RequireGender
153 {
154 get => this.requireGender;
155 set => this.requireGender = value;
156 }
157
158 [DefaultValue(false)]
159 public bool RequireBirthDate
160 {
161 get => this.requireBirthDate;
162 set => this.requireBirthDate = value;
163 }
164
168 public static PeerReviewConfiguration Instance => instance;
169
173 public override string Resource => "/Settings/PeerReview.md";
174
178 public override int Priority => 380;
179
185 public override Task<string> Title(Language Language)
186 {
187 return Language.GetStringAsync(typeof(XmppServerModule), 39, "Peer Review");
188 }
189
193 public override Task ConfigureSystem()
194 {
195 return Task.CompletedTask;
196 }
197
202 public override void SetStaticInstance(ISystemConfiguration Configuration)
203 {
204 instance = Configuration as PeerReviewConfiguration;
205 }
206
211 public override Task InitSetup(HttpServer WebServer)
212 {
213 this.peerReview = WebServer.Register("/Settings/PeerReview", null, this.PeerReview, true, false, true);
214
215 return base.InitSetup(WebServer);
216 }
217
222 public override Task UnregisterSetup(HttpServer WebServer)
223 {
224 WebServer.Unregister(this.peerReview);
225
226 return base.UnregisterSetup(WebServer);
227 }
228
232 protected override string ConfigPrivilege => "Admin.Notarius.PeerReview";
233
234 private async Task PeerReview(HttpRequest Request, HttpResponse Response)
235 {
236 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
237
238 if (!Request.HasData)
239 throw new BadRequestException();
240
241 object Obj = await Request.DecodeDataAsync();
242 if (!(Obj is Dictionary<string, object> Form) ||
243 !Form.TryGetValue("allowPeerReview", out Obj) || !(Obj is bool AllowPeerReview) ||
244 !Form.TryGetValue("nrReviewersToApprove", out Obj) || !(Obj is string NrReviewersToApproveStr) ||
245 !int.TryParse(NrReviewersToApproveStr, out int NrReviewersToApprove) || NrReviewersToApprove <= 0 ||
246 !Form.TryGetValue("nrPhotosRequired", out Obj) || !(Obj is string NrPhotosToApproveStr) ||
247 !int.TryParse(NrPhotosToApproveStr, out int NrPhotosToApprove) || NrPhotosToApprove <= 0 ||
248 !Form.TryGetValue("requireFirstName", out Obj) || !(Obj is bool RequireFirstName) ||
249 !Form.TryGetValue("requireMiddleName", out Obj) || !(Obj is bool RequireMiddleName) ||
250 !Form.TryGetValue("requireLastName", out Obj) || !(Obj is bool RequireLastName) ||
251 !Form.TryGetValue("requirePersonalNumber", out Obj) || !(Obj is bool RequirePersonalNumber) ||
252 !Form.TryGetValue("requireCountry", out Obj) || !(Obj is bool RequireCountry) ||
253 !Form.TryGetValue("requireRegion", out Obj) || !(Obj is bool RequireRegion) ||
254 !Form.TryGetValue("requireCity", out Obj) || !(Obj is bool RequireCity) ||
255 !Form.TryGetValue("requireArea", out Obj) || !(Obj is bool RequireArea) ||
256 !Form.TryGetValue("requirePostalCode", out Obj) || !(Obj is bool RequirePostalCode) ||
257 !Form.TryGetValue("requireAddress", out Obj) || !(Obj is bool RequireAddress) ||
258 !Form.TryGetValue("requireIso3166Compliance", out Obj) || !(Obj is bool RequireIso3166Compliance) ||
259 !Form.TryGetValue("requireNationality", out Obj) || !(Obj is bool RequireNationality) ||
260 !Form.TryGetValue("requireGender", out Obj) || !(Obj is bool RequireGender) ||
261 !Form.TryGetValue("requireBirthDate", out Obj) || !(Obj is bool RequireBirthDate))
262 {
263 throw new BadRequestException();
264 }
265
266 this.allowPeerReview = AllowPeerReview;
267 this.nrReviewersToApprove = NrReviewersToApprove;
268 this.nrPhotosRequired = NrPhotosToApprove;
269 this.requireFirstName = RequireFirstName;
270 this.requireMiddleName = RequireMiddleName;
271 this.requireLastName = RequireLastName;
272 this.requirePersonalNumber = RequirePersonalNumber;
273 this.requireCountry = RequireCountry;
274 this.requireRegion = RequireRegion;
275 this.requireCity = RequireCity;
276 this.requireArea = RequireArea;
277 this.requirePostalCode = RequirePostalCode;
278 this.requireAddress = RequireAddress;
279 this.requireIso3166Compliance = RequireIso3166Compliance;
280 this.requireNationality = RequireNationality;
281 this.requireGender = RequireGender;
282 this.requireBirthDate = RequireBirthDate;
283
284 await Database.Update(this);
285
286 Response.StatusCode = 200;
287 await Response.SendResponse();
288 }
289
294 public override Task<bool> SimplifiedConfiguration()
295 {
296 this.nrReviewersToApprove = 2;
297 this.nrPhotosRequired = 1;
298 this.allowPeerReview = true;
299 this.requireFirstName = true;
300 this.requireMiddleName = false;
301 this.requireLastName = true;
302 this.requirePersonalNumber = true;
303 this.requireCountry = true;
304 this.requireRegion = false;
305 this.requireCity = true;
306 this.requireArea = false;
307 this.requirePostalCode = true;
308 this.requireAddress = true;
309 this.requireIso3166Compliance = true;
310 this.requireNationality = false;
311 this.requireGender = false;
312 this.requireBirthDate = false;
313
314 return Task.FromResult(true);
315 }
316
321 public const string BROKER_REVIEW_USE = nameof(BROKER_REVIEW_USE);
322
326 public const string BROKER_REVIEW_NRPEERS = nameof(BROKER_REVIEW_NRPEERS);
327
331 public const string BROKER_REVIEW_NRPHOTOS = nameof(BROKER_REVIEW_NRPHOTOS);
332
336 public const string BROKER_REVIEW_FIRST = nameof(BROKER_REVIEW_FIRST);
337
341 public const string BROKER_REVIEW_MIDDLE = nameof(BROKER_REVIEW_MIDDLE);
342
346 public const string BROKER_REVIEW_LAST = nameof(BROKER_REVIEW_LAST);
347
351 public const string BROKER_REVIEW_PNR = nameof(BROKER_REVIEW_PNR);
352
356 public const string BROKER_REVIEW_COUNTRY = nameof(BROKER_REVIEW_COUNTRY);
357
361 public const string BROKER_REVIEW_REGION = nameof(BROKER_REVIEW_REGION);
362
366 public const string BROKER_REVIEW_CITY = nameof(BROKER_REVIEW_CITY);
367
371 public const string BROKER_REVIEW_AREA = nameof(BROKER_REVIEW_AREA);
372
376 public const string BROKER_REVIEW_ZIP = nameof(BROKER_REVIEW_ZIP);
377
381 public const string BROKER_REVIEW_ADDR = nameof(BROKER_REVIEW_ADDR);
382
386 public const string BROKER_REVIEW_ISO3166 = nameof(BROKER_REVIEW_ISO3166);
387
392
396 public const string BROKER_REVIEW_GENDER = nameof(BROKER_REVIEW_GENDER);
397
401 public const string BROKER_REVIEW_BDATE = nameof(BROKER_REVIEW_BDATE);
402
407 public override Task<bool> EnvironmentConfiguration()
408 {
409 if (!this.TryGetEnvironmentVariable(BROKER_REVIEW_USE, false, out this.allowPeerReview))
410 return Task.FromResult(false);
411
412 if (!this.allowPeerReview)
413 return Task.FromResult(true);
414
415 if (!this.TryGetEnvironmentVariable(BROKER_REVIEW_NRPEERS, 1, int.MaxValue, true, ref this.nrReviewersToApprove) ||
416 !this.TryGetEnvironmentVariable(BROKER_REVIEW_NRPHOTOS, 0, int.MaxValue, true, ref this.nrPhotosRequired) ||
417 !this.TryGetEnvironmentVariable(BROKER_REVIEW_FIRST, true, out this.requireFirstName) ||
418 !this.TryGetEnvironmentVariable(BROKER_REVIEW_MIDDLE, true, out this.requireMiddleName) ||
419 !this.TryGetEnvironmentVariable(BROKER_REVIEW_LAST, true, out this.requireLastName) ||
420 !this.TryGetEnvironmentVariable(BROKER_REVIEW_PNR, true, out this.requirePersonalNumber) ||
421 !this.TryGetEnvironmentVariable(BROKER_REVIEW_COUNTRY, true, out this.requireCountry) ||
422 !this.TryGetEnvironmentVariable(BROKER_REVIEW_REGION, true, out this.requireRegion) ||
423 !this.TryGetEnvironmentVariable(BROKER_REVIEW_CITY, true, out this.requireCity) ||
424 !this.TryGetEnvironmentVariable(BROKER_REVIEW_AREA, true, out this.requireArea) ||
425 !this.TryGetEnvironmentVariable(BROKER_REVIEW_ZIP, true, out this.requirePostalCode) ||
426 !this.TryGetEnvironmentVariable(BROKER_REVIEW_ADDR, true, out this.requireAddress) ||
427 !this.TryGetEnvironmentVariable(BROKER_REVIEW_ISO3166, true, out this.requireIso3166Compliance) ||
428 !this.TryGetEnvironmentVariable(BROKER_REVIEW_NATIONALITY, true, out this.requireNationality) ||
429 !this.TryGetEnvironmentVariable(BROKER_REVIEW_GENDER, true, out this.requireGender) ||
430 !this.TryGetEnvironmentVariable(BROKER_REVIEW_BDATE, true, out this.requireBirthDate))
431 {
432 return Task.FromResult(false);
433 }
434
435 return Task.FromResult(true);
436 }
437
438 }
439}
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
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:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
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:36
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1287
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1438
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 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).
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
const string BROKER_REVIEW_LAST
If last-name is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_FIRST
If first-name is required in a peer review (true or 1), or not(false or 0).
override Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
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).
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.
override string Resource
Resource to be redirected to, to perform the configuration.
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.
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).
override Task< string > Title(Language Language)
Gets a title for the system configuration.
const string BROKER_REVIEW_COUNTRY
If country is required in a peer review(true or 1), or not(false or 0).
const string BROKER_REVIEW_AREA
If area is required in a peer review(true or 1), or not(false or 0).
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...