diff --git a/Casino/AdminView.xaml b/Casino/AdminView.xaml
new file mode 100644
index 0000000..c3667b1
--- /dev/null
+++ b/Casino/AdminView.xaml
@@ -0,0 +1,102 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Casino/AdminView.xaml.cs b/Casino/AdminView.xaml.cs
new file mode 100644
index 0000000..c6f7a15
--- /dev/null
+++ b/Casino/AdminView.xaml.cs
@@ -0,0 +1,148 @@
+using System.Collections.ObjectModel;
+using System.Windows;
+
+namespace Casino
+{
+ public partial class AdminView : Window
+ {
+ private readonly Database db = new Database();
+
+ private ObservableCollection users;
+
+ public AdminView()
+ {
+ InitializeComponent();
+
+ // Prüfen, ob der aktuell eingeloggte Benutzer Admin ist
+ if (MainWindow.currentUser < 0)
+ {
+ MessageBox.Show(
+ "Es ist kein Benutzer eingeloggt.",
+ "Fehler",
+ MessageBoxButton.OK,
+ MessageBoxImage.Error);
+
+ Close();
+ return;
+ }
+
+ users = db.GetUser();
+
+ // Sicherheitsprüfung über die Datenbank
+ if (MainWindow.currentUser >= users.Count ||
+ !db.SelectAdmin(users[MainWindow.currentUser].ID))
+ {
+ MessageBox.Show(
+ "Du hast keine Admin-Rechte.",
+ "Zugriff verweigert",
+ MessageBoxButton.OK,
+ MessageBoxImage.Warning);
+
+ Close();
+ return;
+ }
+
+ UserDataGrid.ItemsSource = users;
+ }
+
+ // ==========================================
+ // BEARBEITEN
+ // ==========================================
+
+ private void BtnBearbeiten_Click(object sender, RoutedEventArgs e)
+ {
+ if (UserDataGrid.SelectedItem is not User selectedUser)
+ {
+ MessageBox.Show(
+ "Bitte wähle zuerst einen Benutzer aus.",
+ "Kein Benutzer ausgewählt",
+ MessageBoxButton.OK,
+ MessageBoxImage.Warning);
+
+ return;
+ }
+
+ EditUserView editView = new EditUserView(selectedUser);
+
+ // Eigenes Fenster öffnen
+ editView.ShowDialog();
+
+ // Nach dem Schließen Daten neu laden
+ users = db.GetUser();
+ UserDataGrid.ItemsSource = users;
+ }
+
+ // ==========================================
+ // LÖSCHEN
+ // ==========================================
+
+ private void BtnLoeschen_Click(object sender, RoutedEventArgs e)
+ {
+ if (UserDataGrid.SelectedItem is not User selectedUser)
+ {
+ MessageBox.Show(
+ "Bitte wähle zuerst einen Benutzer aus.",
+ "Kein Benutzer ausgewählt",
+ MessageBoxButton.OK,
+ MessageBoxImage.Warning);
+
+ return;
+ }
+
+ // Sicherheitsabfrage
+ MessageBoxResult result = MessageBox.Show(
+ $"Möchtest du den Benutzer '{selectedUser.Name}' wirklich löschen?",
+ "Benutzer löschen",
+ MessageBoxButton.YesNo,
+ MessageBoxImage.Warning);
+
+ if (result != MessageBoxResult.Yes)
+ {
+ return;
+ }
+
+ // Benutzer aus Datenbank löschen
+ bool success = db.DeleteUser(selectedUser.ID);
+
+ if (success)
+ {
+ MessageBox.Show(
+ "Benutzer wurde erfolgreich gelöscht.",
+ "Erfolgreich",
+ MessageBoxButton.OK,
+ MessageBoxImage.Information);
+
+ // Tabelle aktualisieren
+ users = db.GetUser();
+ UserDataGrid.ItemsSource = users;
+ }
+ else
+ {
+ MessageBox.Show(
+ "Der Benutzer konnte nicht gelöscht werden.",
+ "Fehler",
+ MessageBoxButton.OK,
+ MessageBoxImage.Error);
+ }
+ }
+
+ // ==========================================
+ // AKTUALISIEREN
+ // ==========================================
+
+ private void BtnAktualisieren_Click(object sender, RoutedEventArgs e)
+ {
+ users = db.GetUser();
+ UserDataGrid.ItemsSource = users;
+ }
+
+ // ==========================================
+ // SCHLIESSEN
+ // ==========================================
+
+ private void BtnSchliessen_Click(object sender, RoutedEventArgs e)
+ {
+ Close();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Casino/Database.cs b/Casino/Database.cs
index 4273045..935851f 100644
--- a/Casino/Database.cs
+++ b/Casino/Database.cs
@@ -5,6 +5,7 @@ using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using System.Windows;
using MySql.Data.MySqlClient;
namespace Casino
@@ -40,7 +41,8 @@ namespace Casino
Name = myReader.GetString("name"),
Alter = myReader.GetInt32("age"),
Passwort = myReader.GetString("passwort"),
- Geld = myReader.GetDecimal("geld")
+ Geld = myReader.GetDecimal("geld"),
+ Rolle = myReader.GetString("Rolle")
};
user.Add(u);
@@ -200,5 +202,119 @@ namespace Casino
}
}
+ public bool SelectAdmin(string userId)
+ {
+ using MySqlConnection connection = new MySqlConnection(myConnectionString);
+
+ try
+ {
+ connection.Open();
+
+ string sql = @"
+ SELECT COUNT(*)
+ FROM Nutzer
+ WHERE id = @id
+ AND Rolle = 'Admin'";
+
+ using MySqlCommand command = new MySqlCommand(sql, connection);
+
+ command.Parameters.AddWithValue("@id", userId);
+
+ int count = Convert.ToInt32(command.ExecuteScalar());
+
+ return count > 0;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(
+ "Fehler bei der Admin-Abfrage:\n" + ex.Message,
+ "Datenbankfehler",
+ MessageBoxButton.OK,
+ MessageBoxImage.Error);
+
+ return false;
+ }
+ }
+
+ public bool DeleteUser(string userId)
+ {
+ using MySqlConnection connection = new MySqlConnection(myConnectionString);
+
+ try
+ {
+ connection.Open();
+
+ string sql = @"
+ DELETE FROM Nutzer
+ WHERE id = @id";
+
+ using MySqlCommand command = new MySqlCommand(sql, connection);
+
+ command.Parameters.AddWithValue("@id", userId);
+
+ int affectedRows = command.ExecuteNonQuery();
+
+ return affectedRows > 0;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(
+ "Fehler beim Löschen des Benutzers:\n" + ex.Message,
+ "Datenbankfehler",
+ MessageBoxButton.OK,
+ MessageBoxImage.Error);
+
+ return false;
+ }
+ }
+
+ public bool UpdateUser(
+ string userId,
+ string name,
+ int age,
+ string passwort,
+ decimal geld,
+ string rolle)
+ {
+ using MySqlConnection connection = new MySqlConnection(myConnectionString);
+
+ try
+ {
+ connection.Open();
+
+ string sql = @"
+ UPDATE Nutzer
+ SET name = @name,
+ age = @age,
+ passwort = @passwort,
+ geld = @geld,
+ Rolle = @rolle
+ WHERE id = @id";
+
+ using MySqlCommand command = new MySqlCommand(sql, connection);
+
+ command.Parameters.AddWithValue("@name", name);
+ command.Parameters.AddWithValue("@age", age);
+ command.Parameters.AddWithValue("@passwort", passwort);
+ command.Parameters.AddWithValue("@geld", geld);
+ command.Parameters.AddWithValue("@rolle", rolle);
+ command.Parameters.AddWithValue("@id", userId);
+
+ int affectedRows = command.ExecuteNonQuery();
+
+ return affectedRows > 0;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(
+ "Fehler beim Aktualisieren des Benutzers:\n" + ex.Message,
+ "Datenbankfehler",
+ MessageBoxButton.OK,
+ MessageBoxImage.Error);
+
+ return false;
+ }
+ }
+
}
}
diff --git a/Casino/EditUserView.xaml b/Casino/EditUserView.xaml
new file mode 100644
index 0000000..2af82bc
--- /dev/null
+++ b/Casino/EditUserView.xaml
@@ -0,0 +1,127 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Casino/EditUserView.xaml.cs b/Casino/EditUserView.xaml.cs
new file mode 100644
index 0000000..04a95b3
--- /dev/null
+++ b/Casino/EditUserView.xaml.cs
@@ -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();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Casino/Login.xaml.cs b/Casino/Login.xaml.cs
index 99dcd05..1866eb0 100644
--- a/Casino/Login.xaml.cs
+++ b/Casino/Login.xaml.cs
@@ -26,11 +26,14 @@ namespace Casino
///
public partial class Login : Window
{
+ Database db = new Database();
+ ObservableCollection users = new ObservableCollection();
+
public Login()
{
InitializeComponent();
-
+ users = db.GetUser();
}
@@ -39,10 +42,19 @@ namespace Casino
{
if (checkLogin(TxtName.Text, TxtPasswort.Password))
{
-
+ Console.WriteLine(users[MainWindow.currentUser].Rolle);
+ if (users[MainWindow.currentUser].Rolle.Equals("Admin"))
+ {
+ AdminView adminView = new AdminView();
+ adminView.Show();
+ Close();
+ }else
+ {
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
Close();
+ }
+
}
else
@@ -53,9 +65,7 @@ namespace Casino
private bool checkLogin(string name, string passwort)
{
- Database db = new Database();
- ObservableCollection users = new ObservableCollection();
- users = db.GetUser();
+
string hash = HashPassword(passwort);
for (int i = 0; i < users.Count(); i++)
diff --git a/Casino/READ ME.txt b/Casino/READ ME.txt
index e69de29..15e09ca 100644
--- a/Casino/READ ME.txt
+++ b/Casino/READ ME.txt
@@ -0,0 +1,9 @@
+Name/Passwort
+Test User:
+(Günter/123)
+(Nutzer/Test)
+(Gerdi/Mast)
+
+
+Admin:
+(Admin/nimda)
\ No newline at end of file
diff --git a/Casino/User.cs b/Casino/User.cs
index e32f4b3..31b5140 100644
--- a/Casino/User.cs
+++ b/Casino/User.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -13,6 +14,7 @@ namespace Casino
public int Alter { get; set; }
public string Passwort { get; set; }
public decimal Geld { get; set; }
+ public string Rolle { get; set; }
public User(string name, int age, string passwort)
{
ID = ID_Generation();
@@ -20,14 +22,16 @@ namespace Casino
Alter = age;
Passwort = passwort;
Geld = 1000;
+ Rolle = "User";
}
- public User(string id, string name, int age,string passwort, int geld)
+ public User(string id, string name, int age,string passwort, int geld, string rolle)
{
ID = id;
Name = name;
Alter = age;
Passwort = passwort;
Geld = geld;
+ Rolle = rolle;
}
private string ID_Generation()