GlobalTokenGrid.cs 31 KB

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