2019-09-14 08:44:20 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2019 Sensus Slovensko a.s.
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Windows.Forms;
|
2021-09-23 09:46:40 +00:00
|
|
|
|
using Common;
|
2019-09-14 08:44:20 +00:00
|
|
|
|
using GemCard;
|
|
|
|
|
|
using Users.Resources;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Users.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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor with a predefined user.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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 (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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-23 09:46:40 +00:00
|
|
|
|
AuthorizedAs authorizedAs = AuthorizedAs.RemoteUser;
|
2019-09-14 08:44:20 +00:00
|
|
|
|
Users.Entities.User loadedUser = null;
|
|
|
|
|
|
///
|
|
|
|
|
|
bool oldPasswdOK = false;
|
|
|
|
|
|
if (!oldPasswdOK)
|
|
|
|
|
|
{
|
2022-05-23 08:38:14 +00:00
|
|
|
|
DBSettings[] dbs = new DBSettings[] { CurrentUser.RemoteUsersDB, CurrentUser.LocalUsersDB };
|
2019-09-14 08:44:20 +00:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
2021-09-23 09:46:40 +00:00
|
|
|
|
authorizedAs = AuthorizedAs.LocalUser;
|
2019-09-14 08:44:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|