GlobalTokenGrid.cs 31 KB

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