PasswordResetPage.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using InABox.Clients;
  2. using InABox.Configuration;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using XF.Material.Forms.UI.Dialogs;
  12. namespace comal.timesheets
  13. {
  14. public delegate void PasswordResetEvent();
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class PasswordResetPage : ContentPage
  17. {
  18. public event PasswordResetEvent OnPasswordReset;
  19. private User user;
  20. public PasswordResetPage(string userid)
  21. {
  22. InitializeComponent();
  23. Task.Run(() =>
  24. {
  25. ClientFactory.SetBypass();
  26. CoreTable table = new Client<User>().Query(new Filter<User>(x => x.UserID).IsEqualTo(userid));
  27. if (table.Rows.Any())
  28. {
  29. user = table.Rows.FirstOrDefault().ToObject<User>();
  30. }
  31. else
  32. {
  33. Device.BeginInvokeOnMainThread(() =>
  34. {
  35. DisplayAlert("Error", "User not found", "OK");
  36. });
  37. }
  38. });
  39. }
  40. private async void SaveNewPassword_Clicked(object sender, EventArgs e)
  41. {
  42. bool bOK = CheckPasswordValidity();
  43. if (user.ID != Guid.Empty && bOK)
  44. {
  45. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  46. {
  47. user.Password = newPasswordEnt1.Text;
  48. new Client<User>().Save(user, "Password updated by User from Timebench");
  49. ClientFactory.UnsetBypass();
  50. App.Settings.Password = user.Password;
  51. new LocalConfiguration<ConnectionSettings>().Save(App.Settings);
  52. }
  53. DisplayAlert("Success", "Password changed", "OK");
  54. Navigation.PopAsync();
  55. OnPasswordReset?.Invoke();
  56. }
  57. }
  58. private bool CheckPasswordValidity()
  59. {
  60. if (string.IsNullOrWhiteSpace(newPasswordEnt1.Text) || string.IsNullOrWhiteSpace(newPasswordEnt2.Text))
  61. {
  62. DisplayAlert("Error", "Password entries may not be blank", "OK");
  63. return false;
  64. }
  65. if (newPasswordEnt1.Text.Length < 5 || newPasswordEnt2.Text.Length < 5)
  66. {
  67. DisplayAlert("Error", "Passwords must be at least 5 characters", "OK");
  68. return false;
  69. }
  70. if (newPasswordEnt1.Text != newPasswordEnt2.Text)
  71. {
  72. DisplayAlert("Error", "Passwords do not match", "OK");
  73. return false;
  74. }
  75. return true;
  76. }
  77. }
  78. }