PasswordResetPage.xaml.cs 2.8 KB

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