108 lines
2.4 KiB
PHP
108 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Controllers;
|
||
|
|
|
||
|
|
use App\Core\Auth;
|
||
|
|
use App\Core\Image;
|
||
|
|
use App\Models\User;
|
||
|
|
|
||
|
|
class UserController
|
||
|
|
{
|
||
|
|
public function index(): void
|
||
|
|
{
|
||
|
|
view('users.index', ['users' => User::all()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(): void
|
||
|
|
{
|
||
|
|
// Im Original ohne Funktion.
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(): void
|
||
|
|
{
|
||
|
|
// Im Original ohne Funktion.
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(string $id): void
|
||
|
|
{
|
||
|
|
$user = User::find((int) $id);
|
||
|
|
|
||
|
|
if ($user === null) {
|
||
|
|
http_response_code(404);
|
||
|
|
exit('Benutzer nicht gefunden.');
|
||
|
|
}
|
||
|
|
|
||
|
|
view('users.show', ['user' => $user]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit(string $id): void
|
||
|
|
{
|
||
|
|
$user = User::find((int) $id);
|
||
|
|
|
||
|
|
if ($user === null) {
|
||
|
|
redirect('/member');
|
||
|
|
}
|
||
|
|
|
||
|
|
$current = Auth::user();
|
||
|
|
if (!$current->isAdmin() && (int) $current->id !== (int) $user->id) {
|
||
|
|
redirect('/member');
|
||
|
|
}
|
||
|
|
|
||
|
|
view('users.edit', ['user' => $user]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(string $id): void
|
||
|
|
{
|
||
|
|
$userId = (int) input('id', $id);
|
||
|
|
$user = User::find($userId);
|
||
|
|
|
||
|
|
if ($user === null) {
|
||
|
|
redirect('/member');
|
||
|
|
}
|
||
|
|
|
||
|
|
$current = Auth::user();
|
||
|
|
|
||
|
|
validate([
|
||
|
|
'name' => 'required|min:3',
|
||
|
|
'email' => 'required|email',
|
||
|
|
'mobile' => 'required',
|
||
|
|
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!empty($_FILES['bild']['name'])) {
|
||
|
|
Image::store($_FILES['bild'], 'user', (string) $userId);
|
||
|
|
}
|
||
|
|
|
||
|
|
$user->name = (string) input('name');
|
||
|
|
$user->email = (string) input('email');
|
||
|
|
$user->mobile = (string) input('mobile');
|
||
|
|
|
||
|
|
// Rolle nur durch Admins änderbar
|
||
|
|
if ($current->isAdmin()) {
|
||
|
|
$user->role = (string) input('role', $user->role);
|
||
|
|
}
|
||
|
|
|
||
|
|
$user->updated_at = now();
|
||
|
|
$user->save();
|
||
|
|
|
||
|
|
redirect('/member');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(string $user_id): void
|
||
|
|
{
|
||
|
|
$user = User::find((int) $user_id);
|
||
|
|
|
||
|
|
if ($user === null) {
|
||
|
|
redirect('/member');
|
||
|
|
}
|
||
|
|
|
||
|
|
// Zugehörige Buchungen mitlöschen, um verwaiste Einträge zu vermeiden.
|
||
|
|
db()->execute('DELETE FROM `bookings` WHERE user_id = ?', [(int) $user_id]);
|
||
|
|
db()->execute('DELETE FROM `users` WHERE id = ?', [(int) $user_id]);
|
||
|
|
|
||
|
|
redirect('/member');
|
||
|
|
}
|
||
|
|
}
|