UserGrid.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 NPOI.SS.Formula.Functions;
  15. using PRS.Shared;
  16. using PRSDesktop.Panels.Users;
  17. using Syncfusion.Windows.Shared;
  18. namespace PRSDesktop
  19. {
  20. internal class UserGrid : DynamicDataGrid<User>
  21. {
  22. private bool ShowAll;
  23. public UserGrid()
  24. {
  25. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.MultiSelect,
  26. DynamicGridOption.SelectColumns);
  27. AddButton("Show All", PRSDesktop.Resources.anonymous.AsBitmapImage(Color.White), ToggleDisabledUsers);
  28. ActionColumns.Add(new DynamicTickColumn<User, int>(x => x.Logins, null, PRSDesktop.Resources.tick.AsBitmapImage(), null, null));
  29. HiddenColumns.Add(x => x.AuthenticatorToken);
  30. HiddenColumns.Add(x => x.Logins);
  31. HiddenColumns.Add(x => x.Password);
  32. OnAfterSave += AfterSave;
  33. OnCustomiseEditor += UserGrid_OnCustomiseEditor;
  34. OnEditorValueChanged += UserGrid_OnEditorValueChanged;
  35. if (Security.IsAllowed<CanCreateMobilePhoneCredentialLinks>())
  36. ActionColumns.Add(new DynamicImageColumn(EmailImage, SendEmail));
  37. }
  38. private bool SendEmail(CoreRow? row)
  39. {
  40. if (row is null)
  41. return false;
  42. User user = row.ToObject<User>();
  43. var menu = new ContextMenu();
  44. menu.AddItem("PRS Site App", null, () =>
  45. {
  46. CreateLink(user, @"prssite://open/", @"http://www.prssite.com/open/", CreateURLs(new string[] { "remote.com-al.com.au:8050" }));
  47. });
  48. menu.AddItem("PRS Timebench", null, () =>
  49. {
  50. CreateLink(user, @"prsmobile://open/", @"http://www.prsmobile.com/open/", CreateURLs(App.DatabaseSettings.URLs));
  51. });
  52. menu.IsOpen = true;
  53. return true;
  54. }
  55. private void CreateLink(User user, string ioslink, string androidlink, string URLs)
  56. {
  57. if (string.IsNullOrWhiteSpace(ioslink))
  58. return;
  59. var edt = new NumberEdit("Enter link expiry time in minutes", 10, 300, 10);
  60. bool result = (bool)edt.ShowDialog();
  61. if (!result)
  62. return;
  63. var expiry = edt.Value;
  64. string toEncrypt = URLs + "," + user.UserID + "," + user.Password + "," + DateTime.Now.AddMinutes(expiry);
  65. string encrypted = Encryption.Encrypt(toEncrypt, "logindetailslink", true);
  66. ioslink = ioslink + encrypted;
  67. androidlink = androidlink + encrypted;
  68. string emailcontent = "Please ensure PRS Mobile is closed, then choose a link below:" + Environment.NewLine + Environment.NewLine +
  69. "For Apple devices, click this link: " + ioslink + Environment.NewLine + Environment.NewLine
  70. + "For Android devices (Samsung, Google, Xiaomi, Oppo, Vivo, Huawei, Motorola etc), click this link: " + androidlink + Environment.NewLine + Environment.NewLine +
  71. "Please restart the app after loading from the link." + Environment.NewLine + Environment.NewLine +
  72. "These links will expire after " + expiry + " minutes.";
  73. EmailUtils.CreateEMLFile(user.EmailAddress, "PRS Mobile Configuration Links", emailcontent);
  74. }
  75. private string CreateURLs(string[] urls)
  76. {
  77. string URLs = "";
  78. foreach (var url in urls)
  79. {
  80. URLs = URLs + url + ",";
  81. }
  82. return URLs + "ENDURLS";
  83. }
  84. private BitmapImage? EmailImage(CoreRow? arg)
  85. {
  86. return PRSDesktop.Resources.email.AsBitmapImage();
  87. }
  88. private Dictionary<string, object?> UserGrid_OnEditorValueChanged(object sender, string name, object value)
  89. {
  90. var editorForm = (IDynamicEditorForm)sender;
  91. if (name == nameof(User.TwoFactorAuthenticationType))
  92. {
  93. var addressEditor = editorForm.FindEditor(nameof(User.Recipient2FA));
  94. var editor = editorForm.FindEditor(name) as LookupEditorControl;
  95. var choice = (TwoFactorAuthenticationType)value;
  96. var isGoogle = choice == TwoFactorAuthenticationType.GoogleAuthenticator;
  97. addressEditor.SetEnabled(!isGoogle);
  98. (editor.EditorDefinition as EnumLookupEditor)!.Buttons[0].SetEnabled(isGoogle);
  99. }
  100. return new();
  101. }
  102. private void UserGrid_OnCustomiseEditor(IDynamicEditorForm sender, User[]? items, DynamicGridColumn column, BaseEditor editor)
  103. {
  104. var user = items?.FirstOrDefault();
  105. if (user is null)
  106. return;
  107. if (column.ColumnName == nameof(User.TwoFactorAuthenticationType) && editor is EnumLookupEditor enumEditor)
  108. {
  109. var qrCodeButton = new EditorButton(user, "View QR Code", 100, ViewQRCode_Click, false);
  110. qrCodeButton.SetEnabled(user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator);
  111. enumEditor.Buttons = new[] { qrCodeButton };
  112. }
  113. else if (column.ColumnName == nameof(User.Recipient2FA))
  114. {
  115. editor.Editable = user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator ? Editable.Disabled : Editable.Enabled;
  116. }
  117. }
  118. private void ViewQRCode_Click(object editor, object? item)
  119. {
  120. if (item is User user && user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator)
  121. {
  122. var qrWindow = new QR2FAWindow(user);
  123. qrWindow.ShowDialog();
  124. }
  125. }
  126. private bool ToggleDisabledUsers(Button btn, CoreRow[] rows)
  127. {
  128. ShowAll = !ShowAll;
  129. UpdateButton(btn, PRSDesktop.Resources.anonymous.AsBitmapImage(Color.White), ShowAll ? "Hide Finished" : "Show All");
  130. return true;
  131. }
  132. protected override void Reload(Filters<User> criteria, Columns<User> columns, ref SortOrder<User>? sort, Action<CoreTable?, Exception?> action)
  133. {
  134. if (!ShowAll)
  135. criteria.Add(new Filter<User>(x => x.Disabled).IsEqualTo(false));
  136. sort = new SortOrder<User>(x => x.UserID);
  137. base.Reload(criteria, columns, ref sort, action);
  138. }
  139. public override void SaveItem(User item)
  140. {
  141. base.SaveItem(item);
  142. if (item.ID == ClientFactory.UserGuid)
  143. Security.Reset();
  144. }
  145. private void AfterSave(IDynamicEditorForm editor, BaseObject[] items)
  146. {
  147. var users = items.Cast<User>().ToArray();
  148. var ids = users.Select(x => x.ID).ToArray();
  149. var linkedEmployees = new Client<Employee>().Query(
  150. new Filter<Employee>(x => x.UserLink.ID).InList(ids),
  151. new Columns<Employee>(x => x.UserLink.ID)
  152. ).Rows.Select(r => r.Get<Employee,Guid>(c=>c.UserLink.ID)).ToArray();
  153. var newEmployees = new List<Employee>();
  154. foreach (var user in users)
  155. {
  156. if (!linkedEmployees.Contains(user.ID))
  157. {
  158. var result = MessageBox.Show($"{user.UserID} is not associated with an employee. Do you wish to create one?", "Create new Employee?", MessageBoxButton.YesNo);
  159. if (result == MessageBoxResult.Yes)
  160. {
  161. var newEmployee = new Employee()
  162. {
  163. Name = user.Description
  164. };
  165. if(!string.IsNullOrWhiteSpace(user.EmailAddress))
  166. newEmployee.Email = user.EmailAddress;
  167. newEmployee.UserLink.ID = user.ID;
  168. newEmployee.UserLink.Synchronise(user);
  169. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Employee));
  170. grid.EditItems(new object[] { newEmployee });
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }