Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PersonalDataConfiguration.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
7using Waher.Events;
14
16{
21 {
22 private static PersonalDataConfiguration instance = null;
23 private static IProcessingActivity[] processingActivities = null;
24 private HttpResource consent = null;
25
26 private string informationHash = string.Empty;
27 private bool consented = false;
28 private DateTime consentedTimestamp = DateTime.MinValue;
29
33 public static PersonalDataConfiguration Instance => instance;
34
38 public static IProcessingActivity[] ProcessingActivities => processingActivities;
39
43 [DefaultValueStringEmpty]
44 public string InformationHash
45 {
46 get => this.informationHash;
47 set => this.informationHash = value;
48 }
49
53 [DefaultValue(false)]
54 public bool Consented
55 {
56 get => this.consented;
57 set => this.consented = value;
58 }
59
63 [DefaultValueDateTimeMinValue]
64 public DateTime ConsentedTimestamp
65 {
66 get => this.consentedTimestamp;
67 set => this.consentedTimestamp = value;
68 }
69
73 public override string Resource => "/Settings/PersonalData.md";
74
78 public override int Priority => 100;
79
85 public override Task<string> Title(Language Language)
86 {
87 return Language.GetStringAsync(typeof(Gateway), 4, "Personal Data");
88 }
89
93 public override Task ConfigureSystem()
94 {
95 return Task.CompletedTask;
96 }
97
102 public override void SetStaticInstance(ISystemConfiguration Configuration)
103 {
104 instance = Configuration as PersonalDataConfiguration;
105 }
106
111 public override async Task InitSetup(HttpServer WebServer)
112 {
113 await base.InitSetup(WebServer);
114
115 this.consent = WebServer.Register("/Settings/Consent", null, this.Consent, true, false, true);
116
117 List<IProcessingActivity> Activities = new List<IProcessingActivity>();
118
119 foreach (Type T in Types.GetTypesImplementingInterface(typeof(IProcessingActivity)))
120 {
121 if (T.IsAbstract || T.IsInterface || T.IsGenericTypeDefinition)
122 continue;
123
124 try
125 {
126 Activities.Add((IProcessingActivity)Types.Instantiate(T));
127 }
128 catch (Exception ex)
129 {
130 Log.Exception(ex, T.FullName);
131 }
132 }
133
134 Activities.Sort((A1, A2) =>
135 {
136 int i = A1.Priority - A2.Priority;
137 if (i != 0)
138 return i;
139
140 return A1.GetType().FullName.CompareTo(A2.GetType().FullName);
141 });
142
143 processingActivities = Activities.ToArray();
144
145 /* Removed since it might affect remote updates.
146
147 StringBuilder sb = new StringBuilder();
148
149 foreach (IProcessingActivity A in processingActivities)
150 {
151 string FileName = Path.Combine(Gateway.RootFolder, "Settings", "PersonalData", A.TransparentInformationMarkdownFileName);
152
153 try
154 {
155 string Markdown = await Resources.ReadAllTextAsync(FileName);
156 sb.AppendLine(Markdown);
157 }
158 catch (Exception ex)
159 {
160 Log.Exception(ex, A.TransparentInformationMarkdownFileName);
161 }
162 }
163
164 string Hash = Security.Hashes.ComputeSHA256HashString(Encoding.UTF8.GetBytes(sb.ToString()));
165
166 if (Hash != this.informationHash || (!this.consented && this.Complete))
167 {
168 this.InformationHash = Hash;
169 this.consented = false;
170 this.consentedTimestamp = DateTime.MinValue;
171 this.Complete = false;
172 this.Completed = DateTime.MinValue;
173 this.Updated = DateTime.Now;
174
175 await Database.Update(this);
176 }*/
177 }
178
183 public override Task UnregisterSetup(HttpServer WebServer)
184 {
185 WebServer.Unregister(this.consent);
186
187 return base.UnregisterSetup(WebServer);
188 }
189
193 protected override string ConfigPrivilege => "Admin.Legal.PersonalData";
194
195 private async Task Consent(HttpRequest Request, HttpResponse Response)
196 {
197 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
198
199 if (!Request.HasData)
200 throw new BadRequestException();
201
202 object Obj = await Request.DecodeDataAsync();
203 if (!(Obj is Dictionary<string, object> Parameters))
204 throw new BadRequestException();
205
206 if (!Parameters.TryGetValue("consent", out Obj) || !(Obj is bool Consent))
207 throw new BadRequestException();
208
209 string TabID = Request.Header["X-TabID"];
210 if (string.IsNullOrEmpty(TabID))
211 throw new BadRequestException();
212
213 if (this.consented != Consent)
214 {
215 this.consented = Consent;
216 this.consentedTimestamp = Consent ? DateTime.Now : DateTime.MinValue;
217
218 await Database.Update(this);
219
220 await ClientEvents.PushEvent(new string[] { TabID }, "ShowNext", JSON.Encode(new Dictionary<string, object>()
221 {
222 { "consent", Consent }
223 }, false), true, "User");
224 }
225
226 Response.StatusCode = 200;
227 }
228
232 public const string GATEWAY_PII_CONSENT = nameof(GATEWAY_PII_CONSENT);
233
238 public override Task<bool> EnvironmentConfiguration()
239 {
240 if (!this.TryGetEnvironmentVariable(GATEWAY_PII_CONSENT, false, out this.consented) || !this.consented)
241 return Task.FromResult(false);
242
243 this.consentedTimestamp = DateTime.Now;
244 return Task.FromResult(true);
245 }
246
247 }
248}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
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
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
Definition: ClientEvents.cs:51
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
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
Provides information about personal data processing.
const string GATEWAY_PII_CONSENT
Environment variable name for personal data consent configuration.
override Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
static PersonalDataConfiguration Instance
Current instance of configuration.
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
override async Task InitSetup(HttpServer WebServer)
Initializes the setup object.
override int Priority
Priority of the setting. Configurations are sorted in ascending order.
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
override Task< string > Title(Language Language)
Gets a title for the system configuration.
override Task ConfigureSystem()
Is called during startup to configure the system.
string InformationHash
Digest of transparent information presented to which consent is asked.
DateTime ConsentedTimestamp
When consent to personal data processing has been received.
static IProcessingActivity[] ProcessingActivities
Available processing activities.
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
bool Consented
If consent to personal data processing has been received.
override string Resource
Resource to be redirected to, to perform the configuration.
Abstract base class for system configurations.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
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
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
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
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
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1353
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:84
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
Interface for system configurations. The gateway will scan all module for system configuration classe...
Interface for Personal Data Processing Activities