tbf/SharedDatabase/Forms/PasswordChangeDlg.cs

194 lines
6.0 KiB
C#

///
/// Copyright (c) 2019-2021 Sensus Slovensko a.s.
///
using System;
using System.Drawing;
using System.Windows.Forms;
using Common;
using GemCard;
using SharedDatabase.Resources;
namespace SharedDatabase.Forms
{
/// <summary>
/// Provides login dialog as well as user authorization using TBF.User.Authorize(...)
/// (i.e. when DialogResult is OK, user was authorized).
/// </summary>
public partial class PasswordChangeDlg : Form
{
/// <summary>
/// Constructor
/// </summary>
public PasswordChangeDlg()
{
InitializeComponent();
this.Icon = Properties.Resources.TBF_icon;
}
/// <summary>
/// Constructor with a predefined user.
/// </summary>
public PasswordChangeDlg(string predefinedUser)
{
InitializeComponent();
this.Icon = Properties.Resources.TBF_icon;
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;
}
/// <summary>
/// Try to authorize the user when OK clicked.
/// </summary>
private void okButton_Click(object sender, EventArgs e)
{
string userName = userNameTextBox.Text;
string oldPassword = oldPasswTextBox.Text;
if (Common.Utils.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;
Entities.User loadedUser = null;
///
bool oldPasswdOK = false;
if (!oldPasswdOK)
{
DBSettings[] dbs = new DBSettings[] { CurrentUser.RemoteUsersDB, CurrentUser.LocalUsersDB };
foreach (var db in dbs)
{
try
{
loadedUser = 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 && (UsersDB.CurrentSession != null) && (UsersDB.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);
UsersDB.CurrentSession.SaveOrUpdate(loadedUser);
UsersDB.CurrentSession.Flush();
DialogResult = DialogResult.OK;
Close();
return;
}
MessageBox.Show(Strings.Invalid_username_or_password, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand);
DialogResult = DialogResult.None;
return;
}
/// <summary>
/// Cancel clicked, do not try to authorize.
/// </summary>
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);
}
}
}
}