/0_View/ChangePassword.cs

https://github.com/1nv4d3r5/Hypermarket-Shop-Management-Tool · C# · 116 lines · 105 code · 9 blank · 2 comment · 12 complexity · 902588970782d3e24296286dfb87f465 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. //public delegate void UpdateGreetingInfoDelegate(string passwordValidDaysLeft);
  10. namespace Hypermarket_Shop_Management_Tool._0_View
  11. {
  12. public partial class ChangePassword : Form
  13. {
  14. private _2_Controller.CoordinatingController mainController = _2_Controller.CoordinatingController.getInstance();
  15. //public UpdateGreetingInfoDelegate UpdateMainInterfaceGreetingInfoDel;
  16. private string newPassword;
  17. private string reenterPassword;
  18. public ChangePassword()
  19. {
  20. InitializeComponent();
  21. }
  22. private bool IsStringEmptyOrNull(string inputString)
  23. {
  24. return (string.IsNullOrEmpty(inputString) || string.IsNullOrWhiteSpace(inputString));
  25. }
  26. private void VerifyAndUpdatePassword()
  27. {
  28. lblFeedback.Visible = true;
  29. newPassword = txtNewPassword.Text.ToLower();
  30. reenterPassword = txtReenterNewPassword.Text.ToLower();
  31. try
  32. {
  33. if (IsStringEmptyOrNull(txtNewPassword.Text))
  34. {
  35. lblFeedback.Text = Constant.ERROR_EMPTY_NEW_PASSWORD;
  36. txtNewPassword.Focus();
  37. }
  38. else if (IsStringEmptyOrNull(txtReenterNewPassword.Text))
  39. {
  40. lblFeedback.Text = Constant.ERROR_EMPTY_REENTER_PASSWORD;
  41. txtReenterNewPassword.Clear();
  42. txtReenterNewPassword.Focus();
  43. }
  44. else if (!newPassword.Equals(reenterPassword))
  45. {
  46. lblFeedback.Text = Constant.ERROR_PASSWORD_MISMATCH;
  47. txtReenterNewPassword.Focus();
  48. }
  49. else if (newPassword.Length < Constant.PASSWORD_LIMITED_LENGTH || !isAlphanumeric(newPassword))
  50. {
  51. lblFeedback.Text = Constant.ERROR_PASSWORD_LENGTH;
  52. txtNewPassword.Clear();
  53. txtReenterNewPassword.Clear();
  54. txtNewPassword.Focus();
  55. }
  56. else
  57. {
  58. mainController.ChangePassword(GlobalVariableAccessor.CurrentStaffID, newPassword);
  59. GlobalVariableAccessor.PasswordValidDays = Constant.PASSWORD_VALID_DAYS.ToString();
  60. this.Close();
  61. }
  62. }
  63. catch
  64. {
  65. throw;
  66. }
  67. }
  68. private bool isAlphanumeric(string password)
  69. {
  70. if (System.Text.RegularExpressions.Regex.IsMatch(password, @"
  71. # Match string having one letter and one digit (min).
  72. \A # Anchor to start of string.
  73. (?=[^0-9]*[0-9]) # at least one number and
  74. (?=[^A-Za-z]*[A-Za-z]) # at least one letter.
  75. \w+ # Match string of alphanums.
  76. \Z # Anchor to end of string.
  77. ",
  78. System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace))
  79. {
  80. return true;
  81. }
  82. return false;
  83. }
  84. private void btnSave_Click(object sender, EventArgs e)
  85. {
  86. try
  87. {
  88. VerifyAndUpdatePassword();
  89. }
  90. catch (Exception ex)
  91. {
  92. lblFeedback.Text = ex.Message;
  93. }
  94. }
  95. private void btnCancel_Click(object sender, EventArgs e)
  96. {
  97. this.Close();
  98. }
  99. private void ChangePasswordInterface_KeyPress(object sender, KeyPressEventArgs e)
  100. {
  101. if (e.KeyChar == (char)13)
  102. {
  103. VerifyAndUpdatePassword();
  104. }
  105. }
  106. }
  107. }