UserGrid.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 PRSDesktop.Panels.Users;
  15. using Syncfusion.Windows.Shared;
  16. namespace PRSDesktop
  17. {
  18. internal class UserGrid : DynamicDataGrid<User>
  19. {
  20. private bool ShowAll;
  21. public UserGrid()
  22. {
  23. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.MultiSelect,
  24. DynamicGridOption.SelectColumns);
  25. AddButton("Show All", PRSDesktop.Resources.anonymous.AsBitmapImage(Color.White), ToggleDisabledUsers);
  26. ActionColumns.Add(new DynamicTickColumn<User, int>(x => x.Logins, null, PRSDesktop.Resources.tick.AsBitmapImage(), null, null));
  27. HiddenColumns.Add(x => x.AuthenticatorToken);
  28. HiddenColumns.Add(x => x.Logins);
  29. HiddenColumns.Add(x => x.Password);
  30. OnAfterSave += AfterSave;
  31. OnCustomiseEditor += UserGrid_OnCustomiseEditor;
  32. OnEditorValueChanged += UserGrid_OnEditorValueChanged;
  33. if (Security.IsAllowed<CanCreateMobilePhoneCredentialLinks>())
  34. ActionColumns.Add(new DynamicActionColumn(EmailImage, SendEmail));
  35. }
  36. private bool SendEmail(CoreRow? row)
  37. {
  38. if (row is null) return false;
  39. User user = row.ToObject<User>();
  40. string ioslink = @"prsmobile://open/";
  41. string androidlink = @"http://www.prsmobile.com/open/";
  42. string toEncrypt = App.DatabaseSettings.URL + "," + App.DatabaseSettings.Port + "," + user.UserID + "," + user.Password + "," + DateTime.Now.AddMinutes(5);
  43. string encrypted = Encryption.Encrypt(toEncrypt, "logindetailslink", true);
  44. ioslink = ioslink + encrypted;
  45. androidlink = androidlink + encrypted;
  46. string emailcontent = "Please ensure PRS Mobile is closed, then choose a link below:" + Environment.NewLine + Environment.NewLine +
  47. "For Apple devices, click this link: " + ioslink + Environment.NewLine + Environment.NewLine
  48. + "For Android devices (Samsung, Google, Xiaomi, Oppo, Vivo, Huawei, Motorola etc), click this link: " + androidlink + Environment.NewLine + Environment.NewLine +
  49. "Please restart the app after loading from the link." + Environment.NewLine + Environment.NewLine +
  50. "These links will expire after 10 minutes";
  51. Clipboard.SetText(emailcontent);
  52. if (string.IsNullOrWhiteSpace(user.EmailAddress))
  53. {
  54. MessageBox.Show("User email is blank - please populate email address or send the text copied to your clipboard");
  55. return false;
  56. }
  57. var currentuser = new Client<User>()
  58. .Query(new Filter<User>(x => x.ID).IsEqualTo(ClientFactory.UserGuid))
  59. .Rows.FirstOrDefault()?.ToObject<User>();
  60. if (currentuser is null)
  61. return false;
  62. var result = MessageBox.Show("Send email with user credentials to " + user.EmailAddress + " ?", "Alert", MessageBoxButton.YesNo);
  63. switch (result)
  64. {
  65. case MessageBoxResult.Yes:
  66. break;
  67. case MessageBoxResult.No:
  68. return false;
  69. default:
  70. return false;
  71. }
  72. bool currentUserEmailDetailsOK = !string.IsNullOrWhiteSpace(currentuser.EmailHost) && currentuser.EmailPort != 0
  73. && !string.IsNullOrWhiteSpace(currentuser.EmailAddress) && !string.IsNullOrWhiteSpace(currentuser.EmailPassword);
  74. var mailer = new ExchangeMailer
  75. {
  76. MailboxHost = currentuser.EmailHost,
  77. MailboxPort = currentuser.EmailPort,
  78. MailboxUserName = currentuser.EmailAddress,
  79. MailboxPassword = currentuser.EmailPassword
  80. };
  81. if (mailer.Connect())
  82. {
  83. var msg = mailer.CreateMessage();
  84. msg.To = new string[] { user.EmailAddress };
  85. msg.Subject = "Link to PRS Mobile";
  86. msg.Body = emailcontent;
  87. var bOK = mailer.SendMessage(msg);
  88. if (!currentUserEmailDetailsOK)
  89. MessageBox.Show("Unable to send email - Email host, port, address or password missing for current user. Please update. The link has been copied to your clipboard for sending");
  90. else if (bOK)
  91. MessageBox.Show("Link has been sent to email service. The link has been copied to your clipboard in case it is not received by the user");
  92. }
  93. return true;
  94. }
  95. private BitmapImage? EmailImage(CoreRow? arg)
  96. {
  97. return PRSDesktop.Resources.email.AsBitmapImage();
  98. }
  99. private Dictionary<string, object?> UserGrid_OnEditorValueChanged(object sender, string name, object value)
  100. {
  101. var editorForm = (DynamicEditorForm)sender;
  102. if (name == nameof(User.TwoFactorAuthenticationType))
  103. {
  104. var addressEditor = editorForm.FindEditor(nameof(User.Recipient2FA));
  105. var editor = editorForm.FindEditor(name) as LookupEditorControl;
  106. var choice = (TwoFactorAuthenticationType)value;
  107. var isGoogle = choice == TwoFactorAuthenticationType.GoogleAuthenticator;
  108. addressEditor.SetEnabled(!isGoogle);
  109. (editor.EditorDefinition as EnumLookupEditor)!.Buttons[0].SetEnabled(isGoogle);
  110. }
  111. return new();
  112. }
  113. private void UserGrid_OnCustomiseEditor(IDynamicEditorForm sender, User[]? items, DynamicGridColumn column, BaseEditor editor)
  114. {
  115. var user = items?.FirstOrDefault();
  116. if (user is null)
  117. return;
  118. if (column.ColumnName == nameof(User.TwoFactorAuthenticationType) && editor is EnumLookupEditor enumEditor)
  119. {
  120. var qrCodeButton = new EditorButton(user, "View QR Code", 100, ViewQRCode_Click, false);
  121. qrCodeButton.SetEnabled(user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator);
  122. enumEditor.Buttons = new[] { qrCodeButton };
  123. }
  124. else if (column.ColumnName == nameof(User.Recipient2FA))
  125. {
  126. editor.Editable = user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator ? Editable.Disabled : Editable.Enabled;
  127. }
  128. }
  129. private void ViewQRCode_Click(object editor, object? item)
  130. {
  131. if (item is User user && user.TwoFactorAuthenticationType == TwoFactorAuthenticationType.GoogleAuthenticator)
  132. {
  133. var qrWindow = new QR2FAWindow(user);
  134. qrWindow.ShowDialog();
  135. }
  136. }
  137. private bool ToggleDisabledUsers(Button btn, CoreRow[] rows)
  138. {
  139. ShowAll = !ShowAll;
  140. UpdateButton(btn, PRSDesktop.Resources.anonymous.AsBitmapImage(Color.White), ShowAll ? "Hide Finished" : "Show All");
  141. return true;
  142. }
  143. protected override void Reload(Filters<User> criteria, Columns<User> columns, ref SortOrder<User> sort, Action<CoreTable?, Exception?> action)
  144. {
  145. if (!ShowAll)
  146. criteria.Add(new Filter<User>(x => x.Disabled).IsEqualTo(false));
  147. sort = new SortOrder<User>(x => x.UserID);
  148. base.Reload(criteria, columns, ref sort, action);
  149. }
  150. protected override void SaveItem(User item)
  151. {
  152. base.SaveItem(item);
  153. if (item.ID == ClientFactory.UserGuid)
  154. Security.Reset();
  155. }
  156. private void AfterSave(DynamicEditorForm editor, User[] items)
  157. {
  158. var ids = items.Select(x => x.ID).ToArray();
  159. var linkedUsers = new Client<Employee>().Query(
  160. new Filter<Employee>(x => x.UserID).InList(ids),
  161. new Columns<Employee>(x => x.UserID)).Rows.Select(x => (Guid)x["UserID"]).ToArray();
  162. var newEmployees = new List<Employee>();
  163. foreach (var item in items)
  164. {
  165. if (!linkedUsers.Contains(item.ID))
  166. {
  167. var result = MessageBox.Show($"{item.UserID} is not associated with an employee. Do you wish to create one?", "Create new Employee?", MessageBoxButton.YesNo);
  168. if (result == MessageBoxResult.Yes)
  169. {
  170. var newEmployee = new Employee()
  171. {
  172. Name = item.Description
  173. };
  174. newEmployee.UserLink.ID = item.ID;
  175. newEmployee.UserLink.Synchronise(item);
  176. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Employee));
  177. grid.EditItems(new object[] { newEmployee });
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }