forked from daggen/XrmToolBox-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cs
534 lines (469 loc) · 21.3 KB
/
Controller.cs
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Daggen.SecurityRole.Model;
using Daggen.SecurityRole.Properties;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using XrmToolBox.Extensibility;
using XrmToolBox.Extensibility.Interfaces;
namespace Daggen.SecurityRole
{
public partial class Controller : PluginControlBase, IPayPalPlugin, IGitHubPlugin
{
private List<Actor> actors = new List<Actor>();
private IEnumerable<Actor> workingListActors = new List<Actor>();
private List<Model.SecurityRole> roles = new List<Model.SecurityRole>();
private string reportPath = "";
private int userListViewColumnOrder = -1;
private const string DataFile = "ActorRoleData{0}.csv";
private const string ReportFile = "ActorSecurityRoleReport.xlsx";
public Controller()
{
InitializeComponent();
}
private void Controller_Load(object sender, EventArgs e)
{
}
private IEnumerable<ActorInRole> GetActorsInSecurityRoles()
{
var qEsystemuserroles = new QueryExpression("systemuserroles");
qEsystemuserroles.ColumnSet.AddColumns("roleid", "systemuserid");
var list = Service.RetrieveAll(qEsystemuserroles).Entities
.Select(e => new ActorInRole {
Actor = (Guid) e.Attributes["systemuserid"],
Role = (Guid) e.Attributes["roleid"]}).ToList();
var qEteamroles = new QueryExpression("teamroles");
qEteamroles.ColumnSet.AddColumns("roleid", "teamid");
list.AddRange(Service.RetrieveAll(qEteamroles).Entities
.Select(e => new ActorInRole
{
Actor = (Guid) e.Attributes["teamid"],
Role = (Guid) e.Attributes["roleid"]
}));
return list;
}
private List<Model.SecurityRole> GetRoles()
{
var qErole = new QueryExpression("role");
qErole.ColumnSet.AddColumns("name", "parentroleid", "businessunitid");
return Service.RetrieveAll(qErole).Entities
.GroupBy(e => e.Attributes["name"].ToString(),
e => new {e.Id, IsParent = !e.Contains("parentroleid"), BusinessUnit = (EntityReference) e.Attributes["businessunitid"] },
(name, ids) => new Model.SecurityRole
{
Role = name,
Id = ids.First(l => l.IsParent).Id,
Ids = ids.ToDictionary(l => l.BusinessUnit.Id, l => l.Id)
}).ToList();
}
private List<Actor> GetActors()
{
var qEsystemuser = new QueryExpression("systemuser");
qEsystemuser.ColumnSet.AddColumns("systemuserid", "businessunitid", "fullname", "isdisabled");
var qEteam = new QueryExpression("team");
qEteam.ColumnSet.AddColumns("name", "businessunitid");
qEteam.Criteria.AddCondition("teamtype", ConditionOperator.Equal, 0);
var list = Service.RetrieveAll(qEsystemuser).Entities.Select(e => new Actor()
{
Name = (string)e.Attributes["fullname"],
IsDisabled = (bool)e.Attributes["isdisabled"],
BusinessUnitName = ((EntityReference)e.Attributes["businessunitid"]).Name,
BusinessUnit = ((EntityReference)e.Attributes["businessunitid"]).Id,
Id = e.Id,
Type = ActorType.User
}).ToList();
list.AddRange(Service.RetrieveAll(qEteam).Entities.Select(e => new Actor
{
Name = e.Attributes["name"].ToString(),
IsDisabled = false,
BusinessUnitName = ((EntityReference)e.Attributes["businessunitid"]).Name,
BusinessUnit = ((EntityReference)e.Attributes["businessunitid"]).Id,
Id = e.Id,
Type = ActorType.Team
}));
return list;
}
private void toolStripButtonClosePlugin_Click(object sender, EventArgs e)
{
CloseTool();
}
private void toolStripButtonRetriveData_Click(object sender, EventArgs ea)
{
WorkAsync(new WorkAsyncInfo
{
Message = "Loading",
Work = (w, e) =>
{
w.ReportProgress(0, "Loading Actors");
actors = GetActors();
w.ReportProgress(25, "Loading Security Roles");
roles = GetRoles();
w.ReportProgress(50, "Loading Actor Security Role Mapping");
foreach (var actorInSecurityRole in GetActorsInSecurityRoles())
{
var actor = actors.First(a => a.Id.Equals(actorInSecurityRole.Actor));
var role = roles.First(r => r.Ids.Values.Contains(actorInSecurityRole.Role));
actor.SecurityRoles.Add(role);
role.Actors.Add(actor);
}
w.ReportProgress(75, "Finishing");
},
PostWorkCallBack = e =>
{
PopulateListViews();
toolStripButtonGenerateReport.Enabled = true;
},
ProgressChanged = e =>
{
SetWorkingMessage(e.UserState.ToString());
}
});
}
private void PopulateListViews()
{
SetActorList(actors);
SortListView(listViewActors);
SetSecurityRoleList(roles);
SortListView(listViewSecurityRoles);
}
private ListViewItem[] GetUsersAsListViewItems(IEnumerable<Actor> users)
{
return users.Select(user => new ListViewItem
{
Text = user.Name,
ImageIndex = 0,
StateImageIndex = 0,
Tag = user,
SubItems = {user.BusinessUnitName, user.Type.ToString(), user.IsDisabled ? "Yes": ""}
}).ToArray();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
var dialog = new FolderBrowserDialog
{
Description = Resources.Controller_button2_Click_Select_where_to_save_the_files_,
SelectedPath = reportPath
};
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
reportPath = dialog.SelectedPath;
using (var file =
new StreamWriter(reportPath + "/" + string.Format(DataFile, DateTime.Now.ToString("yyyyMMddHHmmss")), false, Encoding.UTF8))
{
file.WriteLine(string.Join(";", "Name", "Security Role", "Business Unit", "Status", "Type"));
var actorsToExport = actors;
if (!checkBoxIncludeDisabled.Checked)
actorsToExport = actorsToExport.Where(acc => !acc.IsDisabled).ToList();
if (!checkBoxIncludeTeams.Checked)
actorsToExport = actorsToExport.Where(act => act.Type != ActorType.Team).ToList();
if (!checkBoxIncludeUsers.Checked)
actorsToExport = actorsToExport.Where(act => act.Type != ActorType.User).ToList();
foreach (var actor in actorsToExport)
{
foreach (var role in actor.SecurityRoles)
{
file.WriteLine(string.Join(";", actor.Name, role.Role, actor.BusinessUnitName, actor.IsDisabled ? "Disabled" : "Active", actor.Type.ToString()));
}
}
}
File.WriteAllBytes(reportPath + "/" + ReportFile,
Resources.ActorSecurityRoleReport);
MessageBox.Show(Resources.Controller_toolStripButton1_Click_Report_is_now_saved);
}
private void ListViewUsers_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column == userListViewColumnOrder)
{
SortListView(listViewActors, e.Column, listViewActors.Sorting == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending);
}
else
{
userListViewColumnOrder = e.Column;
SortListView(listViewActors, e.Column);
}
}
private void listViewSecurityRoles_ColumnClick(object sender, ColumnClickEventArgs e)
{
SortListView(listViewSecurityRoles, e.Column, listViewSecurityRoles.Sorting == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending);
}
private void SortListView(ListView listView, int column = 0, SortOrder order = SortOrder.Ascending)
{
listView.Sorting = order;
listView.ListViewItemSorter = new ListViewItemComparer(column, listView.Sorting);
}
private void buttonReset_Click(object sender, EventArgs e)
{
PopulateListViews();
}
private void buttonCombine_Click(object sender, EventArgs e)
{
if (radioButtonToSecurityRole.Checked)
{
if (listViewActors.CheckedItems.Count == 0)
{
MessageBox.Show(Resources.WarningOnEmptySelection);
return;
}
SetSecurityRoleList(listViewActors.CheckedItems.Cast<ListViewItem>()
.Select(item => ((Actor)item.Tag).SecurityRoles)
.SelectMany(ls => ls));
}
else
{
if (listViewSecurityRoles.CheckedItems.Count == 0)
{
MessageBox.Show(Resources.WarningOnEmptySelection);
return;
}
SetActorList(listViewSecurityRoles.CheckedItems.Cast<ListViewItem>()
.Select(item => ((Model.SecurityRole)item.Tag).Actors)
.SelectMany(ls => ls));
}
}
private static ListViewItem[] GetSecurityRoleAsListViewItems(IEnumerable<Model.SecurityRole> securityRoles)
{
return securityRoles
.Select(securityRole => new ListViewItem
{
Text = securityRole.Role,
ImageIndex = 0,
StateImageIndex = 0,
Tag = securityRole
}).ToArray();
}
private void buttonIntersect_Click(object sender, EventArgs e)
{
if (radioButtonToSecurityRole.Checked)
{
if (listViewActors.CheckedItems.Count == 0)
{
MessageBox.Show(Resources.WarningOnEmptySelection);
return;
}
SetSecurityRoleList(listViewActors.CheckedItems.Cast<ListViewItem>()
.Select(item => ((Actor)item.Tag).SecurityRoles)
.Select(l => (IEnumerable<Model.SecurityRole>) l)
.Aggregate((w, n) => n.Intersect(w)));
}
else
{
if (listViewSecurityRoles.CheckedItems.Count == 0)
{
MessageBox.Show(Resources.WarningOnEmptySelection);
return;
}
SetActorList(listViewSecurityRoles.CheckedItems.Cast<ListViewItem>()
.Select(item => ((Model.SecurityRole)item.Tag).Actors)
.Select(l => (IEnumerable<Actor>)l)
.Aggregate((w, n) => n.Intersect(w)));
}
}
private void SetActorList(IEnumerable<Actor> actors)
{
workingListActors = actors;
listViewActors.Items.Clear();
var toSet = FilterActors(workingListActors).Distinct().ToList();
textBoxNumberOfActors.Text = toSet.Count.ToString();
listViewActors.Items.AddRange(
GetUsersAsListViewItems(toSet));
}
private void SetSecurityRoleList(IEnumerable<Model.SecurityRole> securityRoles)
{
listViewSecurityRoles.Items.Clear();
var toSet = securityRoles.Distinct().ToList();
textBoxNumberOfSecurityRoles.Text = toSet.Count.ToString();
listViewSecurityRoles.Items.AddRange(
GetSecurityRoleAsListViewItems(toSet));
}
private IEnumerable<Actor> FilterActors(IEnumerable<Actor> actors)
{
if (!checkBoxIncludeTeams.Checked)
actors = actors.Where(a => a.Type != ActorType.Team);
if (!checkBoxIncludeUsers.Checked)
actors = actors.Where(a => a.Type != ActorType.User);
if (!checkBoxIncludeDisabled.Checked)
actors = actors.Where(a => !a.IsDisabled);
return actors.ToList();
}
private void listViewUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if (listViewActors.SelectedItems.Count > 0
&& listViewSecurityRoles.CheckedItems.Count == 0)
SetSecurityRoleList(((Actor) listViewActors.SelectedItems.Cast<ListViewItem>().Last().Tag).SecurityRoles);
}
private void listViewUsers_DoubleClick(object sender, EventArgs e)
{
var item = (Actor) ((ListView) sender).SelectedItems.Cast<ListViewItem>().Last().Tag;
var type = item.Type == ActorType.User ? "systemuser" : "team";
Process.Start(ConnectionDetail.WebApplicationUrl + "/main.aspx?etn=" + type + "&pagetype=entityrecord&id={" + item.Id +"}");
}
private void listViewSecurityRoles_SelectedIndexChanged(object sender, EventArgs e)
{
if (listViewSecurityRoles.SelectedItems.Count == 0
|| listViewActors.CheckedItems.Count > 0)
return;
SetActorList(((Model.SecurityRole)listViewSecurityRoles.SelectedItems.Cast<ListViewItem>().Last().Tag).Actors);
}
private void listViewSecurityRoles_DoubleClick(object sender, EventArgs e)
{
var item = (Model.SecurityRole)((ListView)sender).SelectedItems.Cast<ListViewItem>().Last().Tag;
Process.Start(ConnectionDetail.WebApplicationUrl + "/biz/roles/edit.aspx?id={" + item.Id + "}");
}
private void checkBoxIncludeFilters_CheckedChanged(object sender, EventArgs e)
{
SetActorList(workingListActors);
}
private void listViewSecurityRoles_ItemChecked(object sender, ItemCheckedEventArgs e)
{
radioButtonToUser.Checked = true;
}
private void listViewUsers_ItemChecked(object sender, ItemCheckedEventArgs e)
{
radioButtonToSecurityRole.Checked = true;
}
public string DonationDescription => "Donation for User Team and Security Role Xrm Plugin";
public string EmailAccount => "[email protected]";
public string RepositoryName => "XrmToolBox-Plugins";
public string UserName => "daggen";
private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
{
foreach (var item in listViewActors.Items.Cast<ListViewItem>())
{
item.Checked = true;
}
}
private void linkLabelUncheckAllActors_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
foreach (var item in listViewActors.Items.Cast<ListViewItem>())
{
item.Checked = false;
}
}
private void linkLabelCheckAllSecurityRoles_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
foreach (var item in listViewSecurityRoles.Items.Cast<ListViewItem>())
{
item.Checked = true;
}
}
private void linkLabelUncheckAllSecurityRoles_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
foreach (var item in listViewSecurityRoles.Items.Cast<ListViewItem>())
{
item.Checked = false;
}
}
private void buttonAddRole_Click(object sender, EventArgs e)
{
if (MessageBox.Show(Resources.Controller_buttonAddRole_Click_Confirm_Add_Roles_To_Actors,
Resources.Controller_buttonAddRole_Click_Confirm_Modification,
MessageBoxButtons.OKCancel) == DialogResult.OK)
ModifyActorSecurityRoleRelation(Service.Associate);
}
private ModifyStatus ModifyRelationRoleToActor(Action<string,
Guid, Relationship, EntityReferenceCollection> modifyFunc, Actor actor, Model.SecurityRole role)
{
if ((modifyFunc == Service.Associate && actor.SecurityRoles.Contains(role))
|| (modifyFunc == Service.Disassociate && !actor.SecurityRoles.Contains(role))
|| actor.IsDisabled)
return ModifyStatus.Ignored;
if (!role.Ids.ContainsKey(actor.BusinessUnit))
return ModifyStatus.Ignored;
var type = actor.Type == ActorType.User ? "systemuser" : "team";
var association = actor.Type == ActorType.User ? "systemuserroles_association" : "teamroles_association";
try
{
modifyFunc(type,
actor.Id,
new Relationship(association),
new EntityReferenceCollection() {new EntityReference("role", role.Ids[actor.BusinessUnit])});
if (modifyFunc == Service.Associate)
{
actor.SecurityRoles.Add(role);
role.Actors.Add(actor);
}
else
{
actor.SecurityRoles.Remove(role);
role.Actors.Remove(actor);
}
return ModifyStatus.Success;
}
catch (Exception)
{
return ModifyStatus.Failed;
}
}
private void buttonRemoveRole_Click(object sender, EventArgs e)
{
if (MessageBox.Show(Resources.Controller_buttonRemoveRole_Click_Confirm_Remove_Roles_To_Actors,
Resources.Controller_buttonAddRole_Click_Confirm_Modification,
MessageBoxButtons.OKCancel) == DialogResult.OK)
ModifyActorSecurityRoleRelation(Service.Disassociate);
}
private void ModifyActorSecurityRoleRelation(Action<string, Guid, Relationship, EntityReferenceCollection> relationshipFunc)
{
WorkAsync(new WorkAsyncInfo
{
Message = "Modifying roles",
Work = (w, e) =>
{
var counter = new Dictionary<ModifyStatus, int>
{
{ ModifyStatus.Success, 0},
{ ModifyStatus.Failed, 0},
{ ModifyStatus.Ignored, 0}
};
var selectedActors = listViewActors.CheckedItems.Cast<ListViewItem>().Select(l => (Actor)l.Tag).ToList();
var selectedSecurityRoles = listViewSecurityRoles.CheckedItems.Cast<ListViewItem>().Select(l => (Model.SecurityRole)l.Tag).ToList();
if (selectedActors.Any() && selectedSecurityRoles.Any())
{
var count = 0;
foreach (var actor in selectedActors)
{
w.ReportProgress((count * 100) / selectedActors.Count(), "Modifying " + actor.Name);
foreach (var role in selectedSecurityRoles)
{
var res = ModifyRelationRoleToActor(relationshipFunc, actor, role);
counter[res]++;
}
count++;
}
}
e.Result = counter;
w.ReportProgress(95, "Finishing");
},
PostWorkCallBack = e =>
{
var counter = (IDictionary<ModifyStatus, int>) e.Result;
MessageBox.Show("Operation finished.\n" +
"Successfull: " + counter[ModifyStatus.Success] + ", Ignored: " +
counter[ModifyStatus.Ignored] + " and Failed: " + counter[ModifyStatus.Failed]);
},
ProgressChanged = e =>
{
SetWorkingMessage(e.UserState.ToString());
}
});
}
private void linkLabelShowAllActors_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SetActorList(actors);
SortListView(listViewActors);
}
private void linkLabelShowAllRoles_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SetSecurityRoleList(roles);
SortListView(listViewSecurityRoles);
}
}
internal enum ModifyStatus
{
Success, Failed, Ignored
}
}