git_bsmd/ENI2/EditControls/ChangePasswordDialog.xaml.cs

91 lines
2.9 KiB
C#

// Copyright (c) 2017- schick Informatik
// Description: Users may change their password
//
using bsmd.database;
using ENI2.Controls;
using System.Windows;
using System.Windows.Controls;
namespace ENI2.EditControls
{
/// <summary>
/// Interaction logic for ChangePasswordDialog.xaml
/// </summary>
public partial class ChangePasswordDialog : EditWindowBase
{
#region Construction
public ChangePasswordDialog()
{
InitializeComponent();
}
#endregion
#region Properties
public ReportingParty CurrentUser { get; set; }
#endregion
#region event handler
private void buttonChangePassword_Click(object sender, RoutedEventArgs e)
{
bool success = false;
string message = "";
if(!textBoxOldPassword.Password.IsNullOrEmpty())
{
if(CurrentUser.GetHash(textBoxOldPassword.Password).Equals(CurrentUser.PasswordHash))
{
if(textBoxNew1Password.Password.IsNullOrEmpty() ||
textBoxNew2Password.Password.IsNullOrEmpty() ||
!textBoxNew1Password.Password.Equals(textBoxNew2Password.Password))
{
message = "New passwords are empty or do not match";
}
else
{
if (textBoxOldPassword.Password.Equals(textBoxNew1Password.Password))
{
message = "Old and new password are the same";
}
else
{
CurrentUser.SetPassword(textBoxNew1Password.Password);
DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Save(CurrentUser);
success = true;
}
}
}
else
{
message = "Old password is not correct";
}
}
else
{
message = "Old password empty";
}
if (!success)
MessageBox.Show(message, "Changing password failed", MessageBoxButton.OK, MessageBoxImage.Error);
else
MessageBox.Show("Password successfully changed.", "Password changed", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void EditWindowBase_Loaded(object sender, RoutedEventArgs e)
{
this.textBlockName.Text = CurrentUser.Name;
this.AddVisible = false;
this.OkVisible = false;
var cancelButton = (Button)Template.FindName("buttonCancel", this);
cancelButton.Content = "Close";
}
#endregion
}
}