GlobalTokenGrid.cs 28 KB

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