-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataClassNotificationMgt.Codeunit.al
319 lines (275 loc) · 12.3 KB
/
DataClassNotificationMgt.Codeunit.al
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace Microsoft.Utilities;
using System.Environment.Configuration;
using System.Privacy;
using Microsoft.Foundation.Address;
using Microsoft.Foundation.Company;
using Microsoft.Purchases.Vendor;
using Microsoft.Sales.Customer;
using Microsoft.CRM.Contact;
using Microsoft.Projects.Resources.Resource;
using Microsoft.HumanResources.Employee;
codeunit 1756 "Data Class. Notification Mgt."
{
trigger OnRun()
begin
end;
var
DataClassificationNotificationActionTxt: Label 'Open Data Classification Guide';
DataClassificationNotificationMsg: Label 'It looks like you are either doing business in the EU or you have EU vendors, customers, contacts, resources or employees. Have you classified your data? We can help you do that.', Comment = '%1=Data Subject';
DontShowAgainTok: Label 'Don''t show me again';
SyncFieldsInFieldTableMsg: Label 'Your fields are %1 days old.', Comment = '%1=Number of days';
SyncAllFieldsTxt: Label 'Synchronize all fields';
UnclassifiedFieldsExistMsg: Label 'You have unclassified fields that require your attention.';
OpenWorksheetActionLbl: Label 'Open worksheet';
SyncFieldsReminderNotificationTxt: Label 'Data Classifications sync reminder';
SyncFieldsReminderNotificationDescriptionTxt: Label 'Remind me to find unclassified fields every 30 days';
UnclassifiedFieldsNotificationTxt: Label 'Data Sensitivities are missing';
UnclassifiedFieldsNotificationDescriptionTxt: Label 'Show a warning when there are fields with missing Data Sensitivity';
ReviewPrivacySettingsNotificationTxt: Label 'Review your privacy settings reminder';
ReviewPrivacySettingsNotificationDescriptionTxt: Label 'Show a warning to review your privacy settings when persons from EU are found in your system';
DataClassificationNotificationIdTxt: Label '23593a8e-947b-4b09-8382-36a8aaf89e01';
SyncFieldsNotificationIdTxt: Label '3bce2004-361a-4e7f-9ae6-2df91f29a195';
UnclassifiedFieldsNotificationIdTxt: Label 'fe7fc3ad-2382-4cbd-93f8-79bcd5b538ae';
[Scope('OnPrem')]
procedure FireDataClassificationNotification()
var
MyNotifications: Record "My Notifications";
Notification: Notification;
begin
if not MyNotifications.IsEnabled(DataClassificationNotificationIdTxt) then
exit;
CreateNotification(Notification, DataClassificationNotificationIdTxt, DataClassificationNotificationMsg);
Notification.AddAction(DataClassificationNotificationActionTxt,
CODEUNIT::"Data Class. Notification Mgt.", 'OpenDataClassificationWizard');
Notification.Send();
end;
[Scope('OnPrem')]
procedure FireSyncFieldsNotification(DaysSinceLastSync: Integer)
var
MyNotifications: Record "My Notifications";
Notification: Notification;
NotificationMessage: Text;
begin
if not MyNotifications.IsEnabled(SyncFieldsNotificationIdTxt) then
exit;
NotificationMessage := StrSubstNo(SyncFieldsInFieldTableMsg, DaysSinceLastSync);
CreateNotification(Notification, SyncFieldsNotificationIdTxt, NotificationMessage);
Notification.AddAction(SyncAllFieldsTxt, CODEUNIT::"Data Class. Notification Mgt.", 'SyncAllFieldsFromNotification');
Notification.Send();
end;
[Scope('OnPrem')]
procedure FireUnclassifiedFieldsNotification()
var
MyNotifications: Record "My Notifications";
Notification: Notification;
begin
if not MyNotifications.IsEnabled(UnclassifiedFieldsNotificationIdTxt) then
exit;
CreateNotification(Notification, UnclassifiedFieldsNotificationIdTxt, UnclassifiedFieldsExistMsg);
Notification.AddAction(OpenWorksheetActionLbl, CODEUNIT::"Data Class. Notification Mgt.", 'OpenClassificationWorksheetPage');
Notification.Send();
end;
local procedure CreateNotification(var Notification: Notification; Id: Text; Message: Text)
begin
Notification.Id := Id;
Notification.Message(Message);
Notification.AddAction(DontShowAgainTok, CODEUNIT::"Data Class. Notification Mgt.", 'DisableNotifications');
end;
procedure ShowNotifications()
var
DataSensitivity: Record "Data Sensitivity";
CountryRegion: Record "Country/Region";
CompanyInformation: Record "Company Information";
CompanyInformationMgt: Codeunit "Company Information Mgt.";
DataClassificationMgt: Codeunit "Data Classification Mgt.";
RecRef: RecordRef;
EURegionFilter: Text;
begin
if not DataSensitivity.WritePermission then
exit;
if CompanyInformation.Get() then;
if CompanyInformation."Demo Company" then
exit;
DataSensitivity.SetRange("Company Name", CompanyName);
if not DataSensitivity.IsEmpty() then
FireNotificationForNonEmptyDataSensitivityTable(DataSensitivity)
else
if CompanyInformationMgt.IsEUCompany(CompanyInformation) and DataClassificationMgt.DataPrivacyEntitiesExist() then
FireDataClassificationNotification()
else begin
CountryRegion.SetFilter("EU Country/Region Code", '<>%1', '');
RecRef.GetTable(CountryRegion);
EURegionFilter := GetFilterTextForFieldValuesInTable(RecRef, CountryRegion.FieldNo(Code));
if EURegionFilter = '' then
exit;
if CompanyHasVendorsInRegion(EURegionFilter) or CompanyHasCustomersInRegion(EURegionFilter) or
CompanyHasContactsInRegion(EURegionFilter) or CompanyHasResourcesInRegion(EURegionFilter) or
CompanyHasEmployeesInRegion(EURegionFilter)
then
FireDataClassificationNotification();
end;
end;
local procedure FireNotificationForNonEmptyDataSensitivityTable(DataSensitivity: Record "Data Sensitivity")
begin
DataSensitivity.SetRange("Data Sensitivity", DataSensitivity."Data Sensitivity"::Unclassified);
if DataSensitivity.FindFirst() then
FireUnclassifiedFieldsNotification()
else
ShowSyncFieldsNotificationIfThereAreUnsynchedFields();
end;
local procedure CompanyHasVendorsInRegion(RegionFilter: Text): Boolean
var
Vendor: Record Vendor;
begin
Vendor.SetRange("Partner Type", Vendor."Partner Type"::Person);
Vendor.SetFilter("Country/Region Code", RegionFilter);
if Vendor.FindFirst() then
exit(true);
exit(false);
end;
local procedure CompanyHasCustomersInRegion(RegionFilter: Text): Boolean
var
Customer: Record Customer;
begin
Customer.SetRange("Partner Type", Customer."Partner Type"::Person);
Customer.SetFilter("Country/Region Code", RegionFilter);
exit(not Customer.IsEmpty());
end;
local procedure CompanyHasContactsInRegion(RegionFilter: Text): Boolean
var
Contact: Record Contact;
begin
Contact.SetRange(Type, Contact.Type::Person);
Contact.SetFilter("Country/Region Code", RegionFilter);
if Contact.FindFirst() then
exit(true);
exit(false);
end;
local procedure CompanyHasResourcesInRegion(RegionFilter: Text): Boolean
var
Resource: Record Resource;
begin
Resource.SetRange(Type, Resource.Type::Person);
Resource.SetFilter("Country/Region Code", RegionFilter);
if Resource.FindFirst() then
exit(true);
exit(false);
end;
local procedure CompanyHasEmployeesInRegion(RegionFilter: Text): Boolean
var
Employee: Record Employee;
begin
Employee.SetFilter("Country/Region Code", RegionFilter);
if Employee.FindFirst() then
exit(true);
exit(false);
end;
[Scope('OnPrem')]
procedure DisableNotifications(Notification: Notification)
var
MyNotifications: Record "My Notifications";
begin
case Notification.Id of
GetDataClassificationNotificationId():
MyNotifications.InsertDefault(Notification.Id, ReviewPrivacySettingsNotificationTxt,
ReviewPrivacySettingsNotificationDescriptionTxt, false);
GetSyncFieldsNotificationId():
MyNotifications.InsertDefault(Notification.Id, SyncFieldsReminderNotificationTxt,
SyncFieldsReminderNotificationDescriptionTxt, false);
GetUnclassifiedFieldsNotificationId():
MyNotifications.InsertDefault(Notification.Id, UnclassifiedFieldsNotificationTxt,
UnclassifiedFieldsNotificationDescriptionTxt, false);
end;
MyNotifications.Disable(Notification.Id);
end;
procedure ShowSyncFieldsNotificationIfThereAreUnsynchedFields()
var
CompanyInformation: Record "Company Information";
DataClassificationMgt: Codeunit "Data Classification Mgt.";
DaysSinceLastSync: Integer;
LastStatusSyncDateTime: DateTime;
begin
if CompanyInformation.Get() then;
if CompanyInformation."Demo Company" then
exit;
LastStatusSyncDateTime := DataClassificationMgt.GetLastSyncStatusDate();
if LastStatusSyncDateTime <> 0DT then begin
DaysSinceLastSync := Round((CurrentDateTime - LastStatusSyncDateTime) / 1000 / 3600 / 24, 1);
if DaysSinceLastSync > 30 then
FireSyncFieldsNotification(DaysSinceLastSync);
end;
end;
[Scope('OnPrem')]
procedure GetFilterTextForFieldValuesInTable(var RecRef: RecordRef; FieldNo: Integer): Text
var
FilterText: Text;
begin
if RecRef.FindSet() then begin
repeat
FilterText := StrSubstNo('%1|%2', FilterText, RecRef.Field(FieldNo));
until RecRef.Next() = 0;
// remove the first vertical bar from the filter text
FilterText := DelChr(FilterText, '<', '|');
end;
exit(FilterText);
end;
[Scope('OnPrem')]
procedure OpenDataClassificationWizard(Notification: Notification)
begin
PAGE.Run(PAGE::"Data Classification Wizard");
end;
[Scope('OnPrem')]
procedure OpenClassificationWorksheetPage(Notification: Notification)
var
DataSensitivity: Record "Data Sensitivity";
begin
DataSensitivity.SetRange("Company Name", CompanyName);
DataSensitivity.SetRange("Data Sensitivity", DataSensitivity."Data Sensitivity"::Unclassified);
PAGE.Run(PAGE::"Data Classification Worksheet", DataSensitivity);
end;
[Scope('OnPrem')]
procedure GetDataClassificationNotificationId(): Guid
begin
exit(DataClassificationNotificationIdTxt);
end;
[Scope('OnPrem')]
procedure GetSyncFieldsNotificationId(): Guid
begin
exit(SyncFieldsNotificationIdTxt);
end;
[Scope('OnPrem')]
procedure GetUnclassifiedFieldsNotificationId(): Guid
begin
exit(UnclassifiedFieldsNotificationIdTxt);
end;
[Scope('OnPrem')]
procedure SyncAllFieldsFromNotification(Notification: Notification)
var
DataClassificationMgt: Codeunit "Data Classification Mgt.";
begin
DataClassificationMgt.SyncAllFields();
ShowNotificationIfThereAreUnclassifiedFields();
end;
[Scope('OnPrem')]
procedure ShowNotificationIfThereAreUnclassifiedFields()
var
CompanyInformation: Record "Company Information";
DataClassificationMgt: Codeunit "Data Classification Mgt.";
begin
if DataClassificationMgt.AreAllFieldsClassified() then
exit;
if CompanyInformation.Get() then;
if CompanyInformation."Demo Company" then
exit;
FireUnclassifiedFieldsNotification();
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Data Classification Mgt.", 'OnShowSyncFieldsNotification', '', false, false)]
local procedure OnShowSyncFieldsNotificationSubscriber()
begin
ShowSyncFieldsNotificationIfThereAreUnsynchedFields();
end;
}