UserGrid.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Mail;
  13. using InABox.WPF;
  14. using PRS.Shared;
  15. using PRSDesktop.Panels.Users;
  16. using Syncfusion.Windows.Shared;
  17. namespace PRSDesktop
  18. {
  19. internal class UserGrid : DynamicDataGrid<User>
  20. {
  21. private bool ShowAll;
  22. public UserGrid()
  23. {
  24. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.MultiSelect,
  25. DynamicGridOption.SelectColumns);
  26. AddButton("Show All", PRSDesktop.Resources.anonymous.AsBitmapImage(Color.White), ToggleDisabledUsers);
  27. ActionColumns.Add(new DynamicTickColumn<User, int>(x => x.Logins, null, PRSDesktop.Resources.tick.AsBitmapImage(), null, null));
  28. HiddenColumns.Add(x => x.AuthenticatorToken);
  29. HiddenColumns.Add(x => x.Logins);
  30. HiddenColumns.Add(x => x.Password);
  31. OnAfterSave += AfterSave;
  32. OnCustomiseEditor += UserGrid_OnCustomiseEditor;
  33. OnEditorValueChanged += UserGrid_OnEditorValueChanged;
  34. if (Security.IsAllowed<CanCreateMobilePhoneCredentialLinks>())
  35. ActionColumns.Add(new DynamicImageColumn(EmailImage, SendEmail));
  36. }
  37. private bool SendEmail(CoreRow? row)
  38. {
  39. if (row is null) return false;
  40. User user = row.ToObject<User>();
  41. string ioslink = @"prsmobile://open/";
  42. string androidlink = @"http://www.prsmobile.com/open/";
  43. string toEncrypt = App.DatabaseSettings.URL + "," + App.DatabaseSettings.Port + "," + user.UserID + "," + user.Password + "," + DateTime.Now.AddMinutes(5);
  44. string encrypted = Encryption.Encrypt(toEncrypt, "logindetailslink", true);
  45. ioslink = ioslink + encrypted;
  46. androidlink = androidlink + encrypted;
  47. string emailcontent = "Please ensure PRS Mobile is closed, then choose a link below:" + Environment.NewLine + Environment.NewLine +
  48. "For Apple devices, click this link: " + ioslink + Environment.NewLine + Environment.NewLine
  49. + "For Android devices (Samsung, Google, Xiaomi, Oppo, Vivo, Huawei, Motorola etc), click this link: " + androidlink + Environment.NewLine + Environment.NewLine +
  50. "Please restart the app after loading from the link." + Environment.NewLine + Environment.NewLine +
  51. "These links will expire after 10 minutes";
  52. EmailUtils.CreateEMLFile(user.EmailAddress, "PRS Mobile Configuration Links", emailcontent);
  53. return true;
  54. }
  55. private BitmapImage? EmailImage(CoreRow? arg)
  56. {
  57. return PRSDesktop.Resources.email.AsBitmapImage();
  58. }
  59. private Dictionary<string, object?> UserGrid_OnEditorValueChanged(object sender, string name, object value)
  60. {
  61. var editorForm = (IDynamicEditorForm)sender;
  62. if (name == nameof(User.TwoFactorAuthenticationType))
  63. {
  64. var addressEditor = editorForm.FindEditor(nameof(User.Recipient2FA));
  65. var editor = editorForm.FindEditor(name) as LookupEditorControl;
  66. var choice = (TwoFactorAuthenticationType)value;
  67. var isGoogle = choice == TwoFactorAuthenticationType.GoogleAuthenticator;
  68. addressEditor.SetEnabled(!isGoogle);
  69. (editor.EditorDefinition as EnumLookupEditor)!.Buttons[0].SetEnabled(isGoogle);
  70. }
  71. return new();
  72. }
  73. private void UserGrid_OnCustomiseEditor(IDynamicEditorForm sender, User[]? items, DynamicGridColumn column, BaseEditor editor)
  74. {
  75. var user = items?.FirstOrDefault();
  76. if (user is null)
  77. return;
  78. if (column.ColumnName == nameof(User.TwoFactorAuthenticationType) && editor is EnumLookupEditor enumEditor)
  79. {
  80. var qrCodeButton = new EditorButton(user, "View QR Code", 100, ViewQRCode_Click, false);
  81. qrCodeButton.SetEnabled(user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator);
  82. enumEditor.Buttons = new[] { qrCodeButton };
  83. }
  84. else if (column.ColumnName == nameof(User.Recipient2FA))
  85. {
  86. editor.Editable = user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator ? Editable.Disabled : Editable.Enabled;
  87. }
  88. }
  89. private void ViewQRCode_Click(object editor, object? item)
  90. {
  91. if (item is User user && user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator)
  92. {
  93. var qrWindow = new QR2FAWindow(user);
  94. qrWindow.ShowDialog();
  95. }
  96. }
  97. private bool ToggleDisabledUsers(Button btn, CoreRow[] rows)
  98. {
  99. ShowAll = !ShowAll;
  100. UpdateButton(btn, PRSDesktop.Resources.anonymous.AsBitmapImage(Color.White), ShowAll ? "Hide Finished" : "Show All");
  101. return true;
  102. }
  103. protected override void Reload(Filters<User> criteria, Columns<User> columns, ref SortOrder<User>? sort, Action<CoreTable?, Exception?> action)
  104. {
  105. if (!ShowAll)
  106. criteria.Add(new Filter<User>(x => x.Disabled).IsEqualTo(false));
  107. sort = new SortOrder<User>(x => x.UserID);
  108. base.Reload(criteria, columns, ref sort, action);
  109. }
  110. public override void SaveItem(User item)
  111. {
  112. base.SaveItem(item);
  113. if (item.ID == ClientFactory.UserGuid)
  114. Security.Reset();
  115. }
  116. private void AfterSave(IDynamicEditorForm editor, User[] items)
  117. {
  118. var ids = items.Select(x => x.ID).ToArray();
  119. var linkedUsers = new Client<Employee>().Query(
  120. new Filter<Employee>(x => x.UserID).InList(ids),
  121. new Columns<Employee>(x => x.UserID)).Rows.Select(x => (Guid)x["UserID"]).ToArray();
  122. var newEmployees = new List<Employee>();
  123. foreach (var item in items)
  124. {
  125. if (!linkedUsers.Contains(item.ID))
  126. {
  127. var result = MessageBox.Show($"{item.UserID} is not associated with an employee. Do you wish to create one?", "Create new Employee?", MessageBoxButton.YesNo);
  128. if (result == MessageBoxResult.Yes)
  129. {
  130. var newEmployee = new Employee()
  131. {
  132. Name = item.Description
  133. };
  134. newEmployee.UserLink.ID = item.ID;
  135. newEmployee.UserLink.Synchronise(item);
  136. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Employee));
  137. grid.EditItems(new object[] { newEmployee });
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }