/0_View/Login.cs

https://github.com/1nv4d3r5/Hypermarket-Shop-Management-Tool · C# · 216 lines · 191 code · 25 blank · 0 comment · 22 complexity · b252e2489b39aeb5af8066a152dfc7c8 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. namespace Hypermarket_Shop_Management_Tool._0_View
  10. {
  11. public partial class Login : Form
  12. {
  13. _1_Model.DBManager DBAccessor = _1_Model.DBManager.getInstance();
  14. private _2_Controller.CoordinatingController mainController = _2_Controller.CoordinatingController.getInstance();
  15. private string email;
  16. private string password;
  17. public Login()
  18. {
  19. InitializeComponent();
  20. }
  21. private void Login_Load(object sender, EventArgs e)
  22. {
  23. txtUserID.Focus();
  24. }
  25. #region Start Login
  26. private static void ThreadProc()
  27. {
  28. Application.Run(new Main());
  29. }
  30. private bool IsStringEmptyOrNull(string inputString)
  31. {
  32. return (string.IsNullOrEmpty(inputString) || string.IsNullOrWhiteSpace(inputString));
  33. }
  34. private void LoginInterfaceTextField_KeyPress(object sender, KeyPressEventArgs e)
  35. {
  36. if (e.KeyChar == (char)13)
  37. {
  38. LoginToSystem();
  39. }
  40. }
  41. private void GoToMainInterface()
  42. {
  43. System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
  44. t.SetApartmentState(System.Threading.ApartmentState.STA);
  45. t.Start();
  46. this.Close();
  47. }
  48. private void LoginToSystem()
  49. {
  50. lblErrorFeedback.Visible = true;
  51. email = txtUserID.Text;
  52. password = txtPassword.Text;
  53. try
  54. {
  55. if (IsStringEmptyOrNull(email))
  56. {
  57. lblErrorFeedback.Text = Constant.ERROR_EMPTY_USER_ID;
  58. txtUserID.Focus();
  59. }
  60. else if (IsStringEmptyOrNull(password))
  61. {
  62. lblErrorFeedback.Text = Constant.ERROR_EMPTY_PASSWORD;
  63. txtPassword.Focus();
  64. }
  65. else if (!mainController.IsValidUser(email))
  66. {
  67. lblErrorFeedback.Text = Constant.ERROR_INVALID_USER_ID;
  68. txtUserID.Focus();
  69. }
  70. else if (!mainController.VerifyUserIdAndPassword(email, password))
  71. {
  72. lblErrorFeedback.Text = Constant.ERROR_WRONG_PASSWORD;
  73. txtPassword.Focus();
  74. }
  75. else
  76. {
  77. if (mainController.RequireChangingPassword(email))
  78. {
  79. pnlLogin.Visible = false;
  80. pnlChangingPassword.Visible = true;
  81. txtNewPassword.Focus();
  82. }
  83. else
  84. {
  85. GoToMainInterface();
  86. }
  87. }
  88. }
  89. catch (Exception e)
  90. {
  91. lblErrorFeedback.Text = e.Message;
  92. }
  93. }
  94. private void btnExit_Click(object sender, EventArgs e)
  95. {
  96. this.Close();
  97. }
  98. private void btnLogin_Click(object sender, EventArgs e)
  99. {
  100. LoginToSystem();
  101. }
  102. private void btnClear_Click(object sender, EventArgs e)
  103. {
  104. txtUserID.Clear();
  105. txtPassword.Clear();
  106. }
  107. #endregion
  108. #region Start changing password
  109. private void ClearTextField()
  110. {
  111. txtNewPassword.Clear();
  112. txtReenterNewPassword.Clear();
  113. }
  114. private void SavePasswordAndLogin()
  115. {
  116. lblNewPasswordError.Visible = true;
  117. if (IsStringEmptyOrNull(txtNewPassword.Text))
  118. {
  119. lblNewPasswordError.Text = Constant.ERROR_EMPTY_NEW_PASSWORD;
  120. txtNewPassword.Clear();
  121. txtNewPassword.Focus();
  122. }
  123. else if (IsStringEmptyOrNull(txtReenterNewPassword.Text))
  124. {
  125. lblNewPasswordError.Text = Constant.ERROR_EMPTY_REENTER_PASSWORD;
  126. txtReenterNewPassword.Clear();
  127. txtReenterNewPassword.Focus();
  128. }
  129. else if (txtNewPassword.Text.Length < Constant.PASSWORD_LIMITED_LENGTH || !isAlphanumeric(txtNewPassword.Text))
  130. {
  131. lblNewPasswordError.Text = Constant.ERROR_PASSWORD_LENGTH;
  132. ClearTextField();
  133. txtNewPassword.Focus();
  134. }
  135. else if (!txtNewPassword.Text.Equals(txtReenterNewPassword.Text))
  136. {
  137. lblNewPasswordError.Text = Constant.ERROR_PASSWORD_MISMATCH;
  138. txtReenterNewPassword.Clear();
  139. txtReenterNewPassword.Focus();
  140. }
  141. else
  142. {
  143. mainController.ChangePassword(email, txtNewPassword.Text);
  144. GlobalVariableAccessor.PasswordValidDays = Constant.PASSWORD_VALID_DAYS.ToString();
  145. GoToMainInterface();
  146. }
  147. }
  148. private bool isAlphanumeric(string password)
  149. {
  150. if (System.Text.RegularExpressions.Regex.IsMatch(password, @"
  151. # Match string having one letter and one digit (min).
  152. \A # Anchor to start of string.
  153. (?=[^0-9]*[0-9]) # at least one number and
  154. (?=[^A-Za-z]*[A-Za-z]) # at least one letter.
  155. \w+ # Match string of alphanums.
  156. \Z # Anchor to end of string.
  157. ",
  158. System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace))
  159. {
  160. return true;
  161. }
  162. return false;
  163. }
  164. private void ChangingPasswordInterface_KeyPress(object sender, KeyPressEventArgs e)
  165. {
  166. if (e.KeyChar == (char)13)
  167. {
  168. SavePasswordAndLogin();
  169. }
  170. }
  171. private void btnClearNewPassword_Click(object sender, EventArgs e)
  172. {
  173. ClearTextField();
  174. }
  175. private void btnNewPasswordExit_Click(object sender, EventArgs e)
  176. {
  177. this.Close();
  178. }
  179. private void btnConfirmNewPassword_Click(object sender, EventArgs e)
  180. {
  181. SavePasswordAndLogin();
  182. }
  183. private void btnCancelChangingPassword_Click(object sender, EventArgs e)
  184. {
  185. ClearTextField();
  186. pnlChangingPassword.Visible = false;
  187. pnlLogin.Visible = true;
  188. }
  189. #endregion
  190. }
  191. }