GlobalTokenGrid.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. var row = _table.NewRow();
  243. _table.LoadRow(row, _descriptor);
  244. _table.Rows.Add(row);
  245. }
  246. }
  247. });
  248. }
  249. action.Invoke(_table, null);
  250. }
  251. private bool GetGlobalOrDefault(string code, bool globaldefault)
  252. {
  253. var global = Items.FirstOrDefault(x => x.Type.Equals(SecurityTokenType.Global) && string.Equals(x.Descriptor, code));
  254. if (global != null)
  255. return global.Enabled;
  256. return globaldefault;
  257. }
  258. private bool GetGroupOrDefault(string code, Guid groupid, bool globaldefault)
  259. {
  260. var group = Items.FirstOrDefault(
  261. x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.Group) && Equals(x.ID, groupid));
  262. if (group != null)
  263. return group.Enabled;
  264. return GetGlobalOrDefault(code, globaldefault);
  265. }
  266. private bool GetUserOrDefault(string code, Guid userid, Guid groupid, bool globaldefault)
  267. {
  268. var user = Items.FirstOrDefault(x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.User) && Equals(x.ID, userid));
  269. if (user != null)
  270. return user.Enabled;
  271. return GetGroupOrDefault(code, groupid, globaldefault);
  272. }
  273. private BitmapImage? GlobalImage(CoreRow? row)
  274. {
  275. if (row == null)
  276. return null;
  277. var code = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  278. var item = Items.FirstOrDefault(x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.Global));
  279. if (item != null)
  280. return item.Enabled ? tick : disabled;
  281. return row.Get<SecurityDescriptor, bool>(c => c.Default) ? defaulttick : defaultdisabled;
  282. }
  283. private BitmapImage? GroupImage(CoreRow? row, Guid groupid)
  284. {
  285. if (row == null)
  286. return null;
  287. var code = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  288. var group = Items.FirstOrDefault(
  289. x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.Group) && Equals(x.ID, groupid));
  290. if (group != null)
  291. return group.Enabled ? tick : disabled;
  292. return GetGlobalOrDefault(code, row.Get<SecurityDescriptor, bool>(c => c.Default))
  293. ? defaulttick
  294. : defaultdisabled;
  295. }
  296. private BitmapImage? UserImage(CoreRow? row, Guid groupid, Guid userid)
  297. {
  298. if (row == null)
  299. return null;
  300. var code = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  301. var user = Items.FirstOrDefault(x => string.Equals(x.Descriptor, code) && x.Type.Equals(SecurityTokenType.User) && Equals(x.ID, userid));
  302. if (user != null)
  303. return user.Enabled ? tick : disabled;
  304. return GetGroupOrDefault(code, groupid, row.Get<SecurityDescriptor, bool>(c => c.Default))
  305. ? defaulttick
  306. : defaultdisabled;
  307. }
  308. private void ResetGlobals(IEnumerable<string> codes)
  309. {
  310. var globals = Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.Global));
  311. if (!globals.Any())
  312. return;
  313. var globalupdates = globals.Select(x => new GlobalSecurityToken { ID = x.RecordID }).ToArray();
  314. new Client<GlobalSecurityToken>().Delete(globalupdates, "", (o, e) => { });
  315. Items.RemoveAll(x => globals.Contains(x));
  316. }
  317. private void ResetGroups(Guid groupid, IEnumerable<string> codes)
  318. {
  319. var groups = groupid == Guid.Empty
  320. ? Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.Group))
  321. : Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.Group) && Equals(groupid, x.ID));
  322. var groupupdates = groups.Select(x => new SecurityToken { ID = x.RecordID }).ToArray();
  323. new Client<SecurityToken>().Delete(groupupdates, "", (o, e) => { });
  324. Items.RemoveAll(x => groups.Contains(x));
  325. }
  326. private void ResetUsers(Guid groupid, IEnumerable<string> codes)
  327. {
  328. var users = groupid == Guid.Empty
  329. ? Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User))
  330. : Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User) && UserGroups[x.ID].Equals(groupid));
  331. var userupdates = users.Select(x => new UserSecurityToken { ID = x.RecordID }).ToArray();
  332. new Client<UserSecurityToken>().Delete(userupdates, "", (o, e) => { });
  333. Items.RemoveAll(x => users.Contains(x));
  334. }
  335. private void ResetUser(Guid userid, IEnumerable<string> codes)
  336. {
  337. var users = Items.Where(x => codes.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User) && Equals(userid, x.ID));
  338. var userupdates = users.Select(x => new UserSecurityToken { ID = x.RecordID }).ToArray();
  339. new Client<UserSecurityToken>().Delete(userupdates, "", (o, e) => { });
  340. Items.RemoveAll(x => users.Contains(x));
  341. }
  342. private enum TokenAction
  343. {
  344. Enable,
  345. Disable,
  346. Toggle,
  347. Reset
  348. }
  349. private readonly Dictionary<TokenAction, Tuple<String,String>> _tokennames = new()
  350. {
  351. { TokenAction.Enable, new("Enable", "Enabling") },
  352. { TokenAction.Disable, new("Disable", "Enabling") },
  353. { TokenAction.Toggle, new("Toggle", "Enabling") },
  354. { TokenAction.Reset, new("Reset", "Enabling") },
  355. };
  356. private bool CreateGlobalMenu()
  357. {
  358. var menu = new ContextMenu();
  359. menu.Items.Add(new MenuItem() { Header = "Enable All Tokens", Command = new Command((o) => GlobalAction(GetVisibleRows(), TokenAction.Enable)) });
  360. menu.Items.Add(new MenuItem() { Header = "Disable All Tokens", Command = new Command((o) => GlobalAction(GetVisibleRows(), TokenAction.Disable)) });
  361. menu.Items.Add(new Separator());
  362. menu.Items.Add(new MenuItem() { Header = "Reset All Tokens", Command = new Command((o) => GlobalAction(GetVisibleRows(), TokenAction.Reset)) });
  363. menu.IsOpen = true;
  364. return false;
  365. }
  366. private bool CreateGroupMenu(Guid groupid)
  367. {
  368. var menu = new ContextMenu();
  369. menu.Items.Add(new MenuItem() { Header = "Enable All Tokens", Command = new Command((o) => GroupAction(GetVisibleRows(), groupid, TokenAction.Enable)) });
  370. menu.Items.Add(new MenuItem() { Header = "Disable All Tokens", Command = new Command((o) => GroupAction(GetVisibleRows(), groupid, TokenAction.Disable)) });
  371. menu.Items.Add(new Separator());
  372. menu.Items.Add(new MenuItem() { Header = "Reset All Tokens", Command = new Command((o) => GroupAction(GetVisibleRows(), groupid, TokenAction.Reset)) });
  373. menu.IsOpen = true;
  374. return false;
  375. }
  376. private bool CreateUserMenu(Guid groupid, Guid userid)
  377. {
  378. var menu = new ContextMenu();
  379. menu.Items.Add(new MenuItem() { Header = "Enable All Tokens", Command = new Command((o) => UserAction(GetVisibleRows(), groupid, userid, TokenAction.Enable)) });
  380. menu.Items.Add(new MenuItem() { Header = "Disable All Tokens", Command = new Command((o) => UserAction(GetVisibleRows(), groupid, userid, TokenAction.Disable)) });
  381. menu.Items.Add(new Separator());
  382. menu.Items.Add(new MenuItem() { Header = "Reset All Tokens", Command = new Command((o) => UserAction(GetVisibleRows(), groupid, userid, TokenAction.Reset)) });
  383. menu.IsOpen = true;
  384. return false;
  385. }
  386. private bool GlobalAction(IList<CoreRow> rows, TokenAction action)
  387. {
  388. if (!rows.Any())
  389. return false;
  390. var descriptors = rows.Select(r =>r.Get<SecurityDescriptor, string>(c => c.Descriptor)).ToArray();
  391. var resetchildren = Items.Where(x => descriptors.Contains(x.Descriptor) && !x.Type.Equals(SecurityTokenType.Global)).Any();
  392. if (resetchildren)
  393. {
  394. var confirm = MessageBox.Show($"{_tokennames[action].Item1} Group and User Tokens as well?", $"{_tokennames[action].Item1} All",
  395. MessageBoxButton.YesNoCancel);
  396. if (confirm == MessageBoxResult.Cancel)
  397. return false;
  398. resetchildren = confirm == MessageBoxResult.Yes;
  399. }
  400. var resetusers = new List<string>();
  401. var resetgroups = new List<string>();
  402. var resetglobals = new List<string>();
  403. var updates = new List<GlobalSecurityToken>();
  404. Progress.ShowModal($"{_tokennames[action].Item2} Tokens", (progress) =>
  405. {
  406. int i = 1;
  407. foreach (var row in rows)
  408. {
  409. progress.Report($"{_tokennames[action].Item2} Tokens ({(double)(i) * 100.0D / (double)rows.Count:F2}% complete)");
  410. i++;
  411. string descriptor = row.Get<SecurityToken, String>(c => c.Descriptor);
  412. bool defaultvalue = row.Get<SecurityDescriptor, bool>(c => c.Default);
  413. bool desiredvalue = action switch
  414. {
  415. TokenAction.Enable => true,
  416. TokenAction.Disable => false,
  417. TokenAction.Reset => defaultvalue,
  418. _ => !GetGlobalOrDefault(descriptor, defaultvalue)
  419. };
  420. var currentvalue = GetGlobalOrDefault(descriptor, row.Get<SecurityDescriptor, bool>(c => c.Default));
  421. if (currentvalue != defaultvalue)
  422. resetglobals.Add(descriptor);
  423. if (resetchildren)
  424. {
  425. resetgroups.Add(descriptor);
  426. resetusers.Add(descriptor);
  427. }
  428. if (desiredvalue != defaultvalue)
  429. {
  430. // ResetGlobals(new[] { descriptor });
  431. //
  432. // if (resetchildren)
  433. // {
  434. // ResetGroups(Guid.Empty, new[] { descriptor });
  435. // ResetUsers(Guid.Empty, new[] { descriptor });
  436. // }
  437. if (currentvalue == defaultvalue)
  438. {
  439. var token = new GlobalSecurityToken
  440. {
  441. Descriptor = descriptor,
  442. Enabled = !currentvalue
  443. };
  444. updates.Add(token);
  445. // new Client<GlobalSecurityToken>().Save(token, "");
  446. // var item = new SecurityTokenItem
  447. // {
  448. // Type = SecurityTokenType.Global,
  449. // Descriptor = descriptor,
  450. // ID = Guid.Empty,
  451. // RecordID = token.ID,
  452. // Enabled = token.Enabled
  453. // };
  454. // Items.Add(item);
  455. }
  456. }
  457. }
  458. progress.Report("Clearing Old Tokens...");
  459. if (resetusers.Any())
  460. ResetUsers(Guid.Empty,resetusers);
  461. if (resetgroups.Any())
  462. ResetGroups(Guid.Empty, resetgroups);
  463. if (resetglobals.Any())
  464. ResetGlobals(resetglobals);
  465. progress.Report("Creating new Tokens...");
  466. if (updates.Any())
  467. {
  468. new Client<GlobalSecurityToken>().Save(updates, "");
  469. Items.AddRange(updates.Select(x => new SecurityTokenItem()
  470. {
  471. Type = SecurityTokenType.Global,
  472. Descriptor = x.Descriptor,
  473. ID = Guid.Empty,
  474. RecordID = x.ID,
  475. Enabled = x.Enabled
  476. }));
  477. }
  478. });
  479. Refresh(false, true);
  480. return false;
  481. }
  482. private bool GroupAction(IList<CoreRow> rows, Guid groupid, TokenAction action)
  483. {
  484. if (!rows.Any())
  485. return false;
  486. var descriptors = rows.Select(r =>r.Get<SecurityDescriptor, string>(c => c.Descriptor)).ToArray();
  487. var resetchildren = Items.Where(x => descriptors.Contains(x.Descriptor) && x.Type.Equals(SecurityTokenType.User) && UserGroups[x.ID].Equals(groupid)).Any();
  488. if (resetchildren)
  489. {
  490. var confirm = MessageBox.Show($"{_tokennames[action].Item1} User Tokens as well?", $"{_tokennames[action].Item1} All",
  491. MessageBoxButton.YesNoCancel);
  492. if (confirm == MessageBoxResult.Cancel)
  493. return false;
  494. resetchildren = confirm == MessageBoxResult.Yes;
  495. }
  496. var resetusers = new List<string>();
  497. var resetgroups = new List<string>();
  498. var updates = new List<SecurityToken>();
  499. Progress.ShowModal($"{_tokennames[action].Item2} Tokens", (progress) =>
  500. {
  501. int i = 1;
  502. foreach (var row in rows)
  503. {
  504. progress.Report($"{_tokennames[action].Item2} Tokens ({(double)(i) * 100.0D / (double)rows.Count:F2}% complete)");
  505. i++;
  506. string descriptor = row.Get<SecurityDescriptor, String>(c => c.Descriptor);
  507. bool globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  508. bool defaultvalue = GetGlobalOrDefault(descriptor, globaldefault);
  509. var currentvalue = GetGroupOrDefault(descriptor, groupid, globaldefault);
  510. bool desiredvalue = action switch
  511. {
  512. TokenAction.Enable => true,
  513. TokenAction.Disable => false,
  514. TokenAction.Reset => GetGlobalOrDefault(descriptor, defaultvalue),
  515. _ => !GetGroupOrDefault(descriptor, groupid, defaultvalue)
  516. };
  517. if (currentvalue != defaultvalue)
  518. resetgroups.Add(descriptor);
  519. if (resetchildren)
  520. resetusers.Add(descriptor);
  521. if (desiredvalue != defaultvalue)
  522. {
  523. var token = new SecurityToken
  524. {
  525. Descriptor = descriptor,
  526. Enabled = desiredvalue
  527. };
  528. token.Group.ID = groupid;
  529. updates.Add(token);
  530. }
  531. }
  532. progress.Report("Clearing Old Tokens...");
  533. if (resetusers.Any())
  534. ResetUsers(groupid,resetusers);
  535. if (resetgroups.Any())
  536. ResetGroups(groupid, resetgroups);
  537. progress.Report("Creating new Tokens...");
  538. if (updates.Any())
  539. {
  540. new Client<SecurityToken>().Save(updates, "");
  541. Items.AddRange(updates.Select(x => new SecurityTokenItem()
  542. {
  543. Type = SecurityTokenType.Group,
  544. Descriptor = x.Descriptor,
  545. ID = groupid,
  546. RecordID = x.ID,
  547. Enabled = x.Enabled
  548. }));
  549. }
  550. });
  551. Refresh(false, true);
  552. return false;
  553. }
  554. private bool UserAction(IList<CoreRow> rows, Guid groupid, Guid userid, TokenAction action)
  555. {
  556. if (!rows.Any())
  557. return false;
  558. Progress.ShowModal($"{_tokennames[action].Item2} Tokens", (progress) =>
  559. {
  560. var resets = new List<string>();
  561. var updates = new List<UserSecurityToken>();
  562. int i = 1;
  563. foreach (var row in rows)
  564. {
  565. progress.Report($"{_tokennames[action].Item2} Tokens ({(double)(i) * 100.0D / (double)rows.Count:F2}% complete)");
  566. i++;
  567. var descriptor = row.Get<SecurityDescriptor, string>(c => c.Descriptor);
  568. bool globaldefault = row.Get<SecurityDescriptor, bool>(c => c.Default);
  569. bool defaultvalue = GetGroupOrDefault(descriptor, groupid, globaldefault);
  570. var currentvalue = GetUserOrDefault(descriptor, userid, groupid, defaultvalue);
  571. bool desiredvalue = action switch
  572. {
  573. TokenAction.Enable => true,
  574. TokenAction.Disable => false,
  575. TokenAction.Toggle => !currentvalue,
  576. _ => GetGroupOrDefault(descriptor, groupid, globaldefault)
  577. };
  578. if (currentvalue != defaultvalue)
  579. resets.Add(descriptor);
  580. if (desiredvalue != defaultvalue)
  581. {
  582. var token = new UserSecurityToken
  583. {
  584. Descriptor = descriptor,
  585. Enabled = desiredvalue
  586. };
  587. token.User.ID = userid;
  588. updates.Add(token);
  589. }
  590. }
  591. progress.Report("Clearing Old Tokens...");
  592. if (resets.Any())
  593. ResetUser(userid, resets);
  594. progress.Report("Creating new Tokens...");
  595. if (updates.Any())
  596. {
  597. new Client<UserSecurityToken>().Save(updates, "");
  598. Items.AddRange(updates.Select(x => new SecurityTokenItem()
  599. {
  600. Type = SecurityTokenType.User,
  601. Descriptor = x.Descriptor,
  602. ID = userid,
  603. RecordID = x.ID,
  604. Enabled = x.Enabled
  605. }));
  606. }
  607. });
  608. Refresh(false, true);
  609. return false;
  610. }
  611. public override void DeleteItems(params CoreRow[] row)
  612. {
  613. // Not required or implemented
  614. }
  615. public override void SaveItem(SecurityDescriptor item)
  616. {
  617. // Not required or implemented
  618. }
  619. }
  620. }