GlobalTokenGrid.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media.Imaging;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. using javax.sql.rowset;
  13. namespace PRSDesktop
  14. {
  15. public class GlobalTokenGrid : DynamicGrid<SecurityDescriptor>
  16. {
  17. private List<SecurityDescriptor> _descriptors;
  18. private static readonly BitmapImage defaultdisabled = PRSDesktop.Resources.disabled.Fade(0.25F).AsBitmapImage();
  19. private static readonly BitmapImage defaulttick = PRSDesktop.Resources.tick.Fade(0.25F).AsBitmapImage();
  20. private static readonly BitmapImage disabled = PRSDesktop.Resources.disabled.AsBitmapImage();
  21. private static readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  22. public GlobalTokenGrid()
  23. {
  24. GroupNames = new Dictionary<Guid, string>();
  25. UserNames = new Dictionary<Guid, string>();
  26. UserGroups = new Dictionary<Guid, Guid>();
  27. GroupID = Guid.Empty;
  28. Items = new List<SecurityTokenItem>();
  29. Options.AddRange(DynamicGridOption.FilterRows);
  30. HiddenColumns.Add(x => x.Descriptor);
  31. HiddenColumns.Add(x => x.Default);
  32. HeaderHeight = 125;
  33. }
  34. public Dictionary<Guid, string> GroupNames { get; }
  35. public Dictionary<Guid, string> UserNames { get; }
  36. public Dictionary<Guid, Guid> UserGroups { get; }
  37. public List<SecurityTokenItem> Items { get; }
  38. public Guid GroupID { get; set; }
  39. private const String ENABLED_TOKENS = "Enabled";
  40. private const String DISABLED_TOKENS = "Disabled";
  41. private const String OVERRIDDEN_TOKENS = "Overridden";
  42. private const String DEFAULT_TOKENS = "Default";
  43. private static readonly string[] ENABLED_FILTERS = new[] { ENABLED_TOKENS, DISABLED_TOKENS };
  44. private static readonly string[] OVERRIDDEN_FILTERS = new[] { OVERRIDDEN_TOKENS, DEFAULT_TOKENS };
  45. private static readonly string[] ALL_FILTERS = new[] { ENABLED_TOKENS, DISABLED_TOKENS, OVERRIDDEN_TOKENS, DEFAULT_TOKENS };
  46. protected override DynamicGridColumns LoadColumns()
  47. {
  48. var columns = new DynamicGridColumns();
  49. columns.Add(new DynamicGridColumn { ColumnName = "Group", Caption = "Group", Width = 150, Alignment = Alignment.MiddleCenter });
  50. columns.Add(new DynamicGridColumn { ColumnName = "Description", Caption = "Description", Width = 0, Alignment = Alignment.MiddleLeft });
  51. columns.Add(new DynamicGridColumn { ColumnName = "Category", Caption = "Applies To", Width = 200, Alignment = Alignment.MiddleCenter });
  52. ActionColumns.Clear();
  53. if (GroupID == Guid.Empty)
  54. {
  55. ActionColumns.Add(
  56. new DynamicImageColumn(
  57. GlobalImage,
  58. r => r != null
  59. ? GlobalAction(new CoreRow[] { r }, TokenAction.Toggle)
  60. : CreateGlobalMenu()
  61. )
  62. {
  63. HeaderText = "Default",
  64. Filters = ALL_FILTERS,
  65. FilterRecord = (r,f) => GlobalFilter(r,f)
  66. });
  67. foreach (var groupid in GroupNames.Keys)
  68. ActionColumns.Add(
  69. new DynamicImageColumn(
  70. r => GroupImage(r, groupid),
  71. r => r != null
  72. ? GroupAction(new CoreRow[] { r }, groupid, TokenAction.Toggle)
  73. : CreateGroupMenu(groupid)
  74. )
  75. {
  76. HeaderText = GroupNames[groupid],
  77. Filters = ALL_FILTERS,
  78. FilterRecord = (r,f) => GroupFilter(r,f,groupid)
  79. }
  80. );
  81. }
  82. else
  83. {
  84. ActionColumns.Add(
  85. new DynamicImageColumn(
  86. r => GroupImage(r, GroupID),
  87. r => r != null
  88. ? GroupAction(new CoreRow[] { r }, GroupID, TokenAction.Toggle)
  89. : CreateGroupMenu(GroupID)
  90. )
  91. {
  92. HeaderText = GroupNames[GroupID],
  93. Filters = ALL_FILTERS,
  94. FilterRecord = (r,f) => GroupFilter(r,f, GroupID)
  95. }
  96. );
  97. foreach (var userid in UserNames.Keys)
  98. if (UserGroups[userid] == GroupID)
  99. ActionColumns.Add(
  100. new DynamicImageColumn(
  101. r => UserImage(r, GroupID, userid),
  102. r => r != null
  103. ? UserAction(new CoreRow[] { r }, GroupID, userid, TokenAction.Toggle)
  104. : CreateUserMenu(GroupID, userid)
  105. )
  106. {
  107. HeaderText = UserNames[userid],
  108. Filters = ALL_FILTERS,
  109. FilterRecord = (r,f) => UserFilter(r,f, GroupID, userid)
  110. }
  111. );
  112. }
  113. return columns;
  114. }
  115. private static bool MatchFilter(string[] filter, string[] test)
  116. {
  117. if ((filter == null) && (test == null))
  118. return true;
  119. if ((filter == null) || (test == null))
  120. return false;
  121. if (filter.Length != test.Length)
  122. return false;
  123. if (filter.Except(test).Any())
  124. return false;
  125. if (test.Except(filter).Any())
  126. return false;
  127. return true;
  128. }
  129. private bool GlobalFilter(CoreRow row, string[] filter)
  130. {
  131. if (MatchFilter(filter, ALL_FILTERS))
  132. return true;
  133. var descriptor = row.Get<SecurityDescriptor, String>(c => c.Descriptor);
  134. var globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  135. if (!MatchFilter(filter, ENABLED_FILTERS))
  136. {
  137. bool isenabled = GetGlobalOrDefault(descriptor, globaldefault);
  138. var check = (filter.Contains(ENABLED_TOKENS) && isenabled) || (filter.Contains(DISABLED_TOKENS) && !isenabled);
  139. if (!check)
  140. return false;
  141. }
  142. if (!MatchFilter(filter, OVERRIDDEN_FILTERS))
  143. {
  144. bool isoverridden = Items.Any(x => String.Equals(x.Descriptor, descriptor) && (x.Type == SecurityTokenType.Global));
  145. var check = (filter.Contains(OVERRIDDEN_TOKENS) && isoverridden) || (filter.Contains(DEFAULT_TOKENS) && !isoverridden);
  146. if (!check)
  147. return false;
  148. }
  149. return true;
  150. }
  151. private bool GroupFilter(CoreRow row, string[] filter, Guid groupid)
  152. {
  153. if (MatchFilter(filter, ALL_FILTERS))
  154. return true;
  155. String descriptor = row.Get<SecurityDescriptor, String>(c => c.Descriptor);
  156. bool globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  157. if (!MatchFilter(filter, ENABLED_FILTERS))
  158. {
  159. bool isenabled = GetGroupOrDefault(descriptor, groupid, globaldefault);
  160. var check = (filter.Contains(ENABLED_TOKENS) && isenabled) || (filter.Contains(DISABLED_TOKENS) && !isenabled);
  161. if (!check)
  162. return false;
  163. }
  164. if (!MatchFilter(filter, OVERRIDDEN_FILTERS))
  165. {
  166. bool isoverridden = Items.Any(x => String.Equals(x.Descriptor, descriptor) && (x.Type == SecurityTokenType.Group));
  167. var check = (filter.Contains(OVERRIDDEN_TOKENS) && isoverridden) || (filter.Contains(DEFAULT_TOKENS) && !isoverridden);
  168. if (!check)
  169. return false;
  170. }
  171. return true;
  172. }
  173. private bool UserFilter(CoreRow row, string[] filter, Guid groupid, Guid userid)
  174. {
  175. if (MatchFilter(filter, ALL_FILTERS))
  176. return true;
  177. var descriptor = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  178. var globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  179. if (!MatchFilter(filter, ENABLED_FILTERS))
  180. {
  181. bool isenabled = GetUserOrDefault(descriptor, userid, groupid, globaldefault);
  182. var check = (filter.Contains(ENABLED_TOKENS) && isenabled) || (filter.Contains(DISABLED_TOKENS) && !isenabled);
  183. if (!check)
  184. return false;
  185. }
  186. if (!MatchFilter(filter, OVERRIDDEN_FILTERS))
  187. {
  188. bool isoverridden = Items.Any(x => string.Equals(x.Descriptor, descriptor) && (x.Type == SecurityTokenType.User));
  189. var check = (filter.Contains(OVERRIDDEN_TOKENS) && isoverridden) || (filter.Contains(DEFAULT_TOKENS) && !isoverridden);
  190. if (!check)
  191. return false;
  192. }
  193. return true;
  194. }
  195. protected override SecurityDescriptor LoadItem(CoreRow row)
  196. {
  197. return _descriptors[row.Index];
  198. }
  199. private CoreTable _table = null;
  200. protected override void Reload(Filters<SecurityDescriptor> criteria, Columns<SecurityDescriptor> columns,
  201. ref SortOrder<SecurityDescriptor>? sort,
  202. Action<CoreTable?, Exception?> action)
  203. {
  204. if (_table == null)
  205. {
  206. Progress.ShowModal("Scanning Tokens..", (progress) =>
  207. {
  208. _table = new CoreTable();
  209. foreach (var column in columns.Items)
  210. _table.Columns.Add(new CoreColumn { ColumnName = column.ToString() });
  211. if (_descriptors == null)
  212. {
  213. _descriptors = new List<SecurityDescriptor>();
  214. var list = Security.Descriptors.Where(x => x.Visible).ToArray();
  215. foreach (var descriptor in list)
  216. {
  217. progress.Report($"Loading Tokens ({(double)(_descriptors.Count+1) * 100.0D / (double)list.Length:F2}% complete)");
  218. var _descriptor = new SecurityDescriptor
  219. {
  220. Category = descriptor.Category,
  221. Group = descriptor.Type,
  222. Descriptor = descriptor.Code,
  223. Description = descriptor.Description,
  224. Default = descriptor.Value,
  225. IsGlobal = descriptor.HasScope(SecurityDescriptorScope.Global),
  226. IsGroup = descriptor.HasScope(SecurityDescriptorScope.Group),
  227. IsUser = descriptor.HasScope(SecurityDescriptorScope.User)
  228. };
  229. _descriptors.Add(_descriptor);
  230. var row = _table.NewRow();
  231. _table.LoadRow(row, _descriptor);
  232. _table.Rows.Add(row);
  233. }
  234. }
  235. });
  236. }
  237. action.Invoke(_table, null);
  238. }
  239. private bool GetGlobalOrDefault(string code, bool globaldefault)
  240. {
  241. var global = Items.FirstOrDefault(x => x.Type.Equals(SecurityTokenType.Global) && string.Equals(x.Descriptor, code));
  242. if (global != null)
  243. return global.Enabled;
  244. return globaldefault;
  245. }
  246. private bool GetGroupOrDefault(string code, Guid groupid, bool globaldefault)
  247. {
  248. var group = Items.FirstOrDefault(
  249. x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.Group) && Equals(x.ID, groupid));
  250. if (group != null)
  251. return group.Enabled;
  252. return GetGlobalOrDefault(code, globaldefault);
  253. }
  254. private bool GetUserOrDefault(string code, Guid userid, Guid groupid, bool globaldefault)
  255. {
  256. var user = Items.FirstOrDefault(x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.User) && Equals(x.ID, userid));
  257. if (user != null)
  258. return user.Enabled;
  259. return GetGroupOrDefault(code, groupid, globaldefault);
  260. }
  261. private BitmapImage GlobalImage(CoreRow row)
  262. {
  263. if (row == null)
  264. return null;
  265. var code = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  266. var item = Items.FirstOrDefault(x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.Global));
  267. if (item != null)
  268. return item.Enabled ? tick : disabled;
  269. return row.Get<SecurityDescriptor, bool>(c => c.Default) ? defaulttick : defaultdisabled;
  270. }
  271. private BitmapImage GroupImage(CoreRow row, Guid groupid)
  272. {
  273. if (row == null)
  274. return null;
  275. var code = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  276. var group = Items.FirstOrDefault(
  277. x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.Group) && Equals(x.ID, groupid));
  278. if (group != null)
  279. return group.Enabled ? tick : disabled;
  280. return GetGlobalOrDefault(code, row.Get<SecurityDescriptor, bool>(c => c.Default))
  281. ? defaulttick
  282. : defaultdisabled;
  283. }
  284. private BitmapImage UserImage(CoreRow row, Guid groupid, Guid userid)
  285. {
  286. if (row == null)
  287. return null;
  288. var code = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  289. var user = Items.FirstOrDefault(x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.User) && Equals(x.ID, userid));
  290. if (user != null)
  291. return user.Enabled ? tick : disabled;
  292. return GetGroupOrDefault(code, groupid, row.Get<SecurityDescriptor, bool>(c => c.Default))
  293. ? defaulttick
  294. : defaultdisabled;
  295. }
  296. private void ResetGlobals(IEnumerable<string> codes)
  297. {
  298. var globals = Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.Global));
  299. if (!globals.Any())
  300. return;
  301. var globalupdates = globals.Select(x => new GlobalSecurityToken { ID = x.RecordID }).ToArray();
  302. new Client<GlobalSecurityToken>().Delete(globalupdates, "", (o, e) => { });
  303. Items.RemoveAll(x => globals.Contains(x));
  304. }
  305. private void ResetGroups(Guid groupid, IEnumerable<string> codes)
  306. {
  307. var groups = groupid == Guid.Empty
  308. ? Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.Group))
  309. : Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.Group) && Equals(groupid, x.ID));
  310. var groupupdates = groups.Select(x => new SecurityToken { ID = x.RecordID }).ToArray();
  311. new Client<SecurityToken>().Delete(groupupdates, "", (o, e) => { });
  312. Items.RemoveAll(x => groups.Contains(x));
  313. }
  314. private void ResetUsers(Guid groupid, IEnumerable<string> codes)
  315. {
  316. var users = groupid == Guid.Empty
  317. ? Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User))
  318. : Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User) && UserGroups[x.ID].Equals(groupid));
  319. var userupdates = users.Select(x => new UserSecurityToken { ID = x.RecordID }).ToArray();
  320. new Client<UserSecurityToken>().Delete(userupdates, "", (o, e) => { });
  321. Items.RemoveAll(x => users.Contains(x));
  322. }
  323. private void ResetUser(Guid userid, IEnumerable<string> codes)
  324. {
  325. var users = Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User) && Equals(userid, x.ID));
  326. var userupdates = users.Select(x => new UserSecurityToken { ID = x.RecordID }).ToArray();
  327. new Client<UserSecurityToken>().Delete(userupdates, "", (o, e) => { });
  328. Items.RemoveAll(x => users.Contains(x));
  329. }
  330. private enum TokenAction
  331. {
  332. Enable,
  333. Disable,
  334. Toggle,
  335. Reset
  336. }
  337. private Dictionary<TokenAction, Tuple<String,String>> _tokennames = new Dictionary<TokenAction, Tuple<String,String>>()
  338. {
  339. { TokenAction.Enable, new("Enable", "Enabling") },
  340. { TokenAction.Disable, new("Disable", "Enabling") },
  341. { TokenAction.Toggle, new("Toggle", "Enabling") },
  342. { TokenAction.Reset, new("Reset", "Enabling") },
  343. };
  344. private bool CreateGlobalMenu()
  345. {
  346. var menu = new ContextMenu();
  347. menu.Items.Add(new MenuItem() { Header = "Enable All Tokens", Command = new Command((o) => GlobalAction(FilteredRows(), TokenAction.Enable)) });
  348. menu.Items.Add(new MenuItem() { Header = "Disable All Tokens", Command = new Command((o) => GlobalAction(FilteredRows(), TokenAction.Disable)) });
  349. menu.Items.Add(new Separator());
  350. menu.Items.Add(new MenuItem() { Header = "Reset All Tokens", Command = new Command((o) => GlobalAction(FilteredRows(), TokenAction.Reset)) });
  351. menu.IsOpen = true;
  352. return false;
  353. }
  354. private bool CreateGroupMenu(Guid groupid)
  355. {
  356. var menu = new ContextMenu();
  357. menu.Items.Add(new MenuItem() { Header = "Enable All Tokens", Command = new Command((o) => GroupAction(FilteredRows(), groupid, TokenAction.Enable)) });
  358. menu.Items.Add(new MenuItem() { Header = "Disable All Tokens", Command = new Command((o) => GroupAction(FilteredRows(), groupid, TokenAction.Disable)) });
  359. menu.Items.Add(new Separator());
  360. menu.Items.Add(new MenuItem() { Header = "Reset All Tokens", Command = new Command((o) => GroupAction(FilteredRows(), groupid, TokenAction.Reset)) });
  361. menu.IsOpen = true;
  362. return false;
  363. }
  364. private bool CreateUserMenu(Guid groupid, Guid userid)
  365. {
  366. var menu = new ContextMenu();
  367. menu.Items.Add(new MenuItem() { Header = "Enable All Tokens", Command = new Command((o) => UserAction(FilteredRows(), groupid, userid, TokenAction.Enable)) });
  368. menu.Items.Add(new MenuItem() { Header = "Disable All Tokens", Command = new Command((o) => UserAction(FilteredRows(), groupid, userid, TokenAction.Disable)) });
  369. menu.Items.Add(new Separator());
  370. menu.Items.Add(new MenuItem() { Header = "Reset All Tokens", Command = new Command((o) => UserAction(FilteredRows(), groupid, userid, TokenAction.Reset)) });
  371. menu.IsOpen = true;
  372. return false;
  373. }
  374. private bool GlobalAction(IList<CoreRow> rows, TokenAction action)
  375. {
  376. if (!rows.Any())
  377. return false;
  378. var descriptors = rows.Select(r =>r.Get<SecurityDescriptor, string>(c => c.Descriptor)).ToArray();
  379. var resetchildren = Items.Where(x => descriptors.Contains(x.Descriptor) && !x.Type.Equals(SecurityTokenType.Global)).Any();
  380. if (resetchildren)
  381. {
  382. var confirm = MessageBox.Show($"{_tokennames[action].Item1} Group and User Tokens as well?", $"{_tokennames[action].Item1} All",
  383. MessageBoxButton.YesNoCancel);
  384. if (confirm == MessageBoxResult.Cancel)
  385. return false;
  386. resetchildren = confirm == MessageBoxResult.Yes;
  387. }
  388. List<String> resetusers = new List<String>();
  389. List<String> resetgroups = new List<String>();
  390. List<String> resetglobals = new List<String>();
  391. List<GlobalSecurityToken> updates = new List<GlobalSecurityToken>();
  392. Progress.ShowModal($"{_tokennames[action].Item2} Tokens", (progress) =>
  393. {
  394. int i = 1;
  395. foreach (var row in rows)
  396. {
  397. progress.Report($"{_tokennames[action].Item2} Tokens ({(double)(i) * 100.0D / (double)rows.Count:F2}% complete)");
  398. i++;
  399. String descriptor = row.Get<SecurityToken, String>(c => c.Descriptor);
  400. bool defaultvalue = row.Get<SecurityDescriptor, bool>(c => c.Default);
  401. bool desiredvalue = action switch
  402. {
  403. TokenAction.Enable => true,
  404. TokenAction.Disable => false,
  405. TokenAction.Reset => defaultvalue,
  406. _ => !GetGlobalOrDefault(descriptor, defaultvalue)
  407. };
  408. var currentvalue = GetGlobalOrDefault(descriptor, row.Get<SecurityDescriptor, bool>(c => c.Default));
  409. if (currentvalue != defaultvalue)
  410. resetglobals.Add(descriptor);
  411. if (resetchildren)
  412. {
  413. resetgroups.Add(descriptor);
  414. resetusers.Add(descriptor);
  415. }
  416. if (desiredvalue != defaultvalue)
  417. {
  418. // ResetGlobals(new[] { descriptor });
  419. //
  420. // if (resetchildren)
  421. // {
  422. // ResetGroups(Guid.Empty, new[] { descriptor });
  423. // ResetUsers(Guid.Empty, new[] { descriptor });
  424. // }
  425. if (currentvalue == defaultvalue)
  426. {
  427. var token = new GlobalSecurityToken
  428. {
  429. Descriptor = descriptor,
  430. Enabled = !currentvalue
  431. };
  432. updates.Add(token);
  433. // new Client<GlobalSecurityToken>().Save(token, "");
  434. // var item = new SecurityTokenItem
  435. // {
  436. // Type = SecurityTokenType.Global,
  437. // Descriptor = descriptor,
  438. // ID = Guid.Empty,
  439. // RecordID = token.ID,
  440. // Enabled = token.Enabled
  441. // };
  442. // Items.Add(item);
  443. }
  444. }
  445. }
  446. progress.Report("Clearing Old Tokens...");
  447. if (resetusers.Any())
  448. ResetUsers(Guid.Empty,resetusers);
  449. if (resetgroups.Any())
  450. ResetGroups(Guid.Empty, resetgroups);
  451. if (resetglobals.Any())
  452. ResetGlobals(resetglobals);
  453. progress.Report("Creating new Tokens...");
  454. if (updates.Any())
  455. {
  456. new Client<GlobalSecurityToken>().Save(updates, "");
  457. Items.AddRange(updates.Select(x => new SecurityTokenItem()
  458. {
  459. Type = SecurityTokenType.Global,
  460. Descriptor = x.Descriptor,
  461. ID = Guid.Empty,
  462. RecordID = x.ID,
  463. Enabled = x.Enabled
  464. }));
  465. }
  466. });
  467. Refresh(false, true);
  468. return false;
  469. }
  470. private bool GroupAction(IList<CoreRow> rows, Guid groupid, TokenAction action)
  471. {
  472. if (!rows.Any())
  473. return false;
  474. var descriptors = rows.Select(r =>r.Get<SecurityDescriptor, string>(c => c.Descriptor)).ToArray();
  475. var resetchildren = Items.Where(x => descriptors.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User) && UserGroups[x.ID].Equals(groupid)).Any();
  476. if (resetchildren)
  477. {
  478. var confirm = MessageBox.Show($"{_tokennames[action].Item1} User Tokens as well?", $"{_tokennames[action].Item1} All",
  479. MessageBoxButton.YesNoCancel);
  480. if (confirm == MessageBoxResult.Cancel)
  481. return false;
  482. resetchildren = confirm == MessageBoxResult.Yes;
  483. }
  484. List<String> resetusers = new List<String>();
  485. List<String> resetgroups = new List<String>();
  486. List<SecurityToken> updates = new List<SecurityToken>();
  487. Progress.ShowModal($"{_tokennames[action].Item2} Tokens", (progress) =>
  488. {
  489. int i = 1;
  490. foreach (var row in rows)
  491. {
  492. progress.Report($"{_tokennames[action].Item2} Tokens ({(double)(i) * 100.0D / (double)rows.Count:F2}% complete)");
  493. i++;
  494. String descriptor = row.Get<SecurityDescriptor, String>(c => c.Descriptor);
  495. bool globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  496. bool defaultvalue = GetGlobalOrDefault(descriptor, globaldefault);
  497. var currentvalue = GetGroupOrDefault(descriptor, groupid, globaldefault);
  498. bool desiredvalue = action switch
  499. {
  500. TokenAction.Enable => true,
  501. TokenAction.Disable => false,
  502. TokenAction.Reset => GetGlobalOrDefault(descriptor, defaultvalue),
  503. _ => !GetGroupOrDefault(descriptor, groupid, defaultvalue)
  504. };
  505. if (currentvalue != defaultvalue)
  506. resetgroups.Add(descriptor);
  507. if (resetchildren)
  508. resetusers.Add(descriptor);
  509. if (desiredvalue != defaultvalue)
  510. {
  511. var token = new SecurityToken
  512. {
  513. Descriptor = descriptor,
  514. Enabled = desiredvalue
  515. };
  516. token.Group.ID = groupid;
  517. updates.Add(token);
  518. }
  519. }
  520. progress.Report("Clearing Old Tokens...");
  521. if (resetusers.Any())
  522. ResetUsers(groupid,resetusers);
  523. if (resetgroups.Any())
  524. ResetGroups(groupid, resetgroups);
  525. progress.Report("Creating new Tokens...");
  526. if (updates.Any())
  527. {
  528. new Client<SecurityToken>().Save(updates, "");
  529. Items.AddRange(updates.Select(x => new SecurityTokenItem()
  530. {
  531. Type = SecurityTokenType.Group,
  532. Descriptor = x.Descriptor,
  533. ID = groupid,
  534. RecordID = x.ID,
  535. Enabled = x.Enabled
  536. }));
  537. }
  538. });
  539. Refresh(false, true);
  540. return false;
  541. }
  542. private bool UserAction(IList<CoreRow> rows, Guid groupid, Guid userid, TokenAction action)
  543. {
  544. if (!rows.Any())
  545. return false;
  546. Progress.ShowModal($"{_tokennames[action].Item2} Tokens", (progress) =>
  547. {
  548. List<String> resets = new List<String>();
  549. List<UserSecurityToken> updates = new List<UserSecurityToken>();
  550. int i = 1;
  551. foreach (var row in rows)
  552. {
  553. progress.Report($"{_tokennames[action].Item2} Tokens ({(double)(i) * 100.0D / (double)rows.Count:F2}% complete)");
  554. i++;
  555. var descriptor = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  556. bool globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  557. bool defaultvalue = GetGroupOrDefault(descriptor, groupid, globaldefault);
  558. var currentvalue = GetUserOrDefault(descriptor, userid, groupid, defaultvalue);
  559. bool desiredvalue = action switch
  560. {
  561. TokenAction.Enable => true,
  562. TokenAction.Disable => false,
  563. TokenAction.Toggle => !currentvalue,
  564. _ => GetGroupOrDefault(descriptor, groupid, globaldefault)
  565. };
  566. if (currentvalue != defaultvalue)
  567. resets.Add(descriptor);
  568. if (desiredvalue != defaultvalue)
  569. {
  570. var token = new UserSecurityToken
  571. {
  572. Descriptor = descriptor,
  573. Enabled = desiredvalue
  574. };
  575. token.User.ID = userid;
  576. updates.Add(token);
  577. }
  578. }
  579. progress.Report("Clearing Old Tokens...");
  580. if (resets.Any())
  581. ResetUser(userid, resets);
  582. progress.Report("Creating new Tokens...");
  583. if (updates.Any())
  584. {
  585. new Client<UserSecurityToken>().Save(updates, "");
  586. Items.AddRange(updates.Select(x => new SecurityTokenItem()
  587. {
  588. Type = SecurityTokenType.User,
  589. Descriptor = x.Descriptor,
  590. ID = userid,
  591. RecordID = x.ID,
  592. Enabled = x.Enabled
  593. }));
  594. }
  595. });
  596. Refresh(false, true);
  597. return false;
  598. }
  599. protected override void DeleteItems(params CoreRow[] row)
  600. {
  601. // Not required or implemented
  602. }
  603. public override void SaveItem(SecurityDescriptor item)
  604. {
  605. // Not required or implemented
  606. }
  607. }
  608. }