ChangePassword.xaml.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using InABox.Clients;
  2. using InABox.Configuration;
  3. using InABox.Core;
  4. using InABox.Wpf;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace PRSDesktop.Forms
  19. {
  20. /// <summary>
  21. /// Interaction logic for ChangePassword.xaml
  22. /// </summary>
  23. public partial class ChangePassword : ThemableWindow
  24. {
  25. private Guid UserGuid { get; set; }
  26. public string? Password { get; set; }
  27. public ChangePassword(Guid userGuid)
  28. {
  29. InitializeComponent();
  30. UserGuid = userGuid;
  31. }
  32. private void SetError(string message)
  33. {
  34. Error.Content = message;
  35. }
  36. public static void ChangeUserPassword(User user, string password)
  37. {
  38. user.Password = password;
  39. }
  40. private void ChangePassword_Click(object sender, RoutedEventArgs e)
  41. {
  42. var newPassword = NewPassword.Password;
  43. if (string.IsNullOrWhiteSpace(newPassword))
  44. {
  45. MessageBox.Show("Please enter a new password.");
  46. return;
  47. }
  48. else if(newPassword != ConfirmPassword.Password)
  49. {
  50. MessageBox.Show("The confirmed password does not match the new password!");
  51. NewPassword.Clear();
  52. ConfirmPassword.Clear();
  53. return;
  54. }
  55. var user = new Client<User>().Query(
  56. new Filter<User>(x => x.ID).IsEqualTo(UserGuid)
  57. .And(x => x.Password).IsEqualTo(CurrentPassword.Password),
  58. new Columns<User>(x => x.ID)).Rows.FirstOrDefault();
  59. if(user == null)
  60. {
  61. MessageBox.Show("The current password is incorrect!");
  62. CurrentPassword.Clear();
  63. return;
  64. }
  65. else if(newPassword == CurrentPassword.Password)
  66. {
  67. MessageBox.Show("The new password cannot be the same as the old one!");
  68. NewPassword.Clear();
  69. ConfirmPassword.Clear();
  70. return;
  71. }
  72. Password = newPassword;
  73. DialogResult = true;
  74. Close();
  75. }
  76. private void Cancel_Click(object sender, RoutedEventArgs e)
  77. {
  78. DialogResult = false;
  79. Close();
  80. }
  81. }
  82. }