/// /// Copyright (c) 2019 Sensus Slovensko a.s. /// using System; using System.Windows.Forms; using Common; using GemCard; using Users.Resources; using System.Drawing; namespace Users.Forms { /// /// Provides login dialog as well as user authorization using TBF.User.Authorize(...) /// (i.e. when DialogResult is OK, user was authorized). /// public partial class PasswordChangeDlg : Form { /// /// Constructor /// public PasswordChangeDlg() { InitializeComponent(); } /// /// Constructor with a predefined user. /// public PasswordChangeDlg(string predefinedUser) { InitializeComponent(); userNameTextBox.Text = predefinedUser; } private void LoginDlg_Load(object sender, EventArgs e) { Localize(); if (string.IsNullOrEmpty(userNameTextBox.Text)) { userNameTextBox.Enabled = true; userNameTextBox.Select(); } else { oldPasswTextBox.Select(); } } void Localize() { Text = Strings.Change_your_password_please; userNameLabel.Text = Strings.User_name; oldPasswLabel.Text = Strings.Old_password; newPasswLabel.Text = Strings.New_password; newPasswVerifLabel.Text = Strings.New_password_verification; okButton.Text = Strings.OkBtnText; cancelButton.Text = Strings.CancelBtnText; } /// /// Try to authorize the user when OK clicked. /// private void okButton_Click(object sender, EventArgs e) { string userName = userNameTextBox.Text; string oldPassword = oldPasswTextBox.Text; if (Entities.User.IsPowerUser(userName, oldPassword)) { MessageBox.Show(Strings.Power_user_cannot_change_its_password, Strings.Warning, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); DialogResult = DialogResult.OK; Close(); return; } string explanation; if (newPasswTextBox.Text != newPasswVerifTextBox.Text) { MessageBox.Show(Strings.New_password_and_its_verification_are_different, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand); DialogResult = DialogResult.None; return; } else if (!Entities.User.IsPasswordMeetsRequirements(newPasswTextBox.Text, out explanation)) { MessageBox.Show(Strings.Password_does_not_meet_requirements + Environment.NewLine + explanation, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand); DialogResult = DialogResult.None; return; } AuthorizedAs authorizedAs = AuthorizedAs.RemoteUser; Users.Entities.User loadedUser = null; /// bool oldPasswdOK = false; if (!oldPasswdOK) { DBSettings[] dbs = new DBSettings[] { GlobalData.RemoteUsersDB, GlobalData.LocalUsersDB }; foreach (var db in dbs) { try { loadedUser = Users.Entities.User.LoadUserByName(userName, db); if (loadedUser != null) { oldPasswdOK = (userName == loadedUser.UserName) && loadedUser.IsCorrectPassword(oldPassword); } } catch (Exception) { } if (oldPasswdOK) break; authorizedAs = AuthorizedAs.LocalUser; } } if (oldPasswdOK && (DB.CurrentSession != null) && (DB.CurrentSession.IsOpen)) { if (loadedUser.IsPasswordUsedInPast(newPasswTextBox.Text)) { MessageBox.Show(Strings.Password_has_been_used_in_past, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand); DialogResult = DialogResult.None; return; } /// /// Now the password is changed in database that was used to authorize the user /// loadedUser.SetPassword(newPasswTextBox.Text); DB.CurrentSession.SaveOrUpdate(loadedUser); DB.CurrentSession.Flush(); DialogResult = DialogResult.OK; Close(); return; } MessageBox.Show(Strings.Invalid_username_or_password, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand); DialogResult = DialogResult.None; return; } /// /// Cancel clicked, do not try to authorize. /// private void cancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); return; } private void userNameTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (Convert.ToInt32(e.KeyChar) == 13) { oldPasswTextBox.Focus(); } } private void passwordTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (Convert.ToInt32(e.KeyChar) == 13) { newPasswTextBox.Focus(); } } private void newPasswTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (Convert.ToInt32(e.KeyChar) == 13) { newPasswVerifTextBox.Focus(); } } private void newPasswVerifTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (Convert.ToInt32(e.KeyChar) == 13) { okButton_Click(sender, e); } } } }