Admin view hinzugefügt
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Casino
|
||||
{
|
||||
public partial class EditUserView : Window
|
||||
{
|
||||
private readonly Database db = new Database();
|
||||
|
||||
private readonly User selectedUser;
|
||||
|
||||
public EditUserView(User user)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
selectedUser = user;
|
||||
|
||||
// Bestehende Werte anzeigen
|
||||
TxtName.Text = selectedUser.Name;
|
||||
TxtAge.Text = selectedUser.Alter.ToString();
|
||||
TxtGeld.Text = selectedUser.Geld.ToString();
|
||||
|
||||
// Rolle auswählen
|
||||
foreach (ComboBoxItem item in CmbRolle.Items)
|
||||
{
|
||||
if (item.Content?.ToString() == selectedUser.Rolle)
|
||||
{
|
||||
CmbRolle.SelectedItem = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// SPEICHERN
|
||||
// ==========================================
|
||||
|
||||
private void BtnSpeichern_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Name prüfen
|
||||
if (string.IsNullOrWhiteSpace(TxtName.Text))
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Bitte gib einen Namen ein.",
|
||||
"Fehler",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Warning);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Alter prüfen
|
||||
if (!int.TryParse(TxtAge.Text, out int age) ||
|
||||
age < 0 ||
|
||||
age > 150)
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Bitte gib ein gültiges Alter ein.",
|
||||
"Fehler",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Warning);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Geld prüfen
|
||||
if (!decimal.TryParse(TxtGeld.Text, out decimal geld))
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Bitte gib einen gültigen Geldbetrag ein.",
|
||||
"Fehler",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Warning);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Rolle prüfen
|
||||
if (CmbRolle.SelectedItem is not ComboBoxItem selectedRoleItem)
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Bitte wähle eine Rolle aus.",
|
||||
"Fehler",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Warning);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
string rolle = selectedRoleItem.Content?.ToString() ?? "User";
|
||||
|
||||
// Passwort:
|
||||
// leer = altes Passwort behalten
|
||||
string passwort = selectedUser.Passwort;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(TxtPasswort.Password))
|
||||
{
|
||||
passwort = HashPassword(TxtPasswort.Password);
|
||||
}
|
||||
|
||||
// Datenbank aktualisieren
|
||||
bool success = db.UpdateUser(
|
||||
selectedUser.ID,
|
||||
TxtName.Text.Trim(),
|
||||
age,
|
||||
passwort,
|
||||
geld,
|
||||
rolle);
|
||||
|
||||
if (success)
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Benutzer wurde erfolgreich aktualisiert.",
|
||||
"Erfolgreich",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Information);
|
||||
|
||||
Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Die Änderungen konnten nicht gespeichert werden.",
|
||||
"Fehler",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// ABBRECHEN
|
||||
// ==========================================
|
||||
|
||||
private void BtnAbbrechen_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// PASSWORT HASHEN
|
||||
// ==========================================
|
||||
|
||||
private static string HashPassword(string password)
|
||||
{
|
||||
using SHA256 sha256 = SHA256.Create();
|
||||
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(password);
|
||||
byte[] hash = sha256.ComputeHash(bytes);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
foreach (byte b in hash)
|
||||
{
|
||||
builder.Append(b.ToString("X2"));
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user