From 84c8e5b8468a9869f371c83641486397f89ee817 Mon Sep 17 00:00:00 2001 From: torsten Date: Sun, 8 Nov 2026 19:07:19 +0100 Subject: [PATCH] =?UTF-8?q?Antr=C3=A4ge=20annehmen=20und=20Benutzer=20zu?= =?UTF-8?q?=20Mitglied=20machen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- motorradig/bin/migrate.php | 15 ++++ .../src/Controllers/AdminController.php | 88 ++++++++++++++++++- motorradig/src/routes.php | 2 + motorradig/views/admin/applications.php | 42 ++++++++- tools/migrate_motorradig.py | 21 +++++ 5 files changed, 164 insertions(+), 4 deletions(-) diff --git a/motorradig/bin/migrate.php b/motorradig/bin/migrate.php index f6192e0..04aeadb 100644 --- a/motorradig/bin/migrate.php +++ b/motorradig/bin/migrate.php @@ -84,6 +84,8 @@ if (!tableExists($pdo, 'membership_applications')) { `unterschrift_2` varchar(255) DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, + `accepted_at` timestamp NULL DEFAULT NULL, + `accepted_by` bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;'); $out("Tabelle 'membership_applications' angelegt"); @@ -91,6 +93,19 @@ if (!tableExists($pdo, 'membership_applications')) { $out("Tabelle 'membership_applications' existiert bereits (übersprungen)"); } +// Spalten für die Annahme von Anträgen ergänzen +foreach ([ + ['accepted_at', 'ALTER TABLE `membership_applications` ADD COLUMN `accepted_at` timestamp NULL DEFAULT NULL AFTER `updated_at`'], + ['accepted_by', 'ALTER TABLE `membership_applications` ADD COLUMN `accepted_by` bigint(20) unsigned NULL DEFAULT NULL AFTER `accepted_at`'], +] as [$column, $sql]) { + if (!columnExists($pdo, 'membership_applications', $column)) { + $pdo->exec($sql); + $out("Spalte 'membership_applications.$column' angelegt"); + } else { + $out("Spalte 'membership_applications.$column' existiert bereits"); + } +} + // Unbenutzte Tabellen entfernen foreach (['migrations', 'failed_jobs', 'galleries', 'gallery_items', 'password_resets'] as $table) { if (tableExists($pdo, $table)) { diff --git a/motorradig/src/Controllers/AdminController.php b/motorradig/src/Controllers/AdminController.php index c2868ac..3af7186 100644 --- a/motorradig/src/Controllers/AdminController.php +++ b/motorradig/src/Controllers/AdminController.php @@ -7,6 +7,7 @@ namespace App\Controllers; use App\Core\Auth; use App\Models\MembershipApplication; use App\Models\Page; +use App\Models\User; class AdminController { @@ -64,7 +65,92 @@ class AdminController exit('Antrag nicht gefunden.'); } - view('admin.application', ['application' => $application]); + view('admin.application', [ + 'application' => $application, + 'acceptedBy' => $application->accepted_by ? User::find((int) $application->accepted_by) : null, + ]); + } + + public function acceptApplication(string $id): void + { + $application = MembershipApplication::find((int) $id); + + if ($application === null) { + flash_set('error', 'Der Antrag wurde nicht gefunden.'); + redirect('/admin/antraege'); + } + + if (!empty($application->accepted_at)) { + flash_set('status', 'Der Antrag wurde bereits angenommen.'); + redirect('/admin/antraege/' . $id); + } + + $user = $application->user_id ? User::find((int) $application->user_id) : null; + + if ($user === null && !empty($application->email)) { + $user = User::byEmail((string) $application->email); + } + + if ($user === null) { + flash_set('error', 'Zu diesem Antrag wurde kein Benutzerkonto gefunden. Bitte zuerst ein Konto mit der angegebenen E-Mail-Adresse anlegen.'); + redirect('/admin/antraege/' . $id); + } + + $roleChanged = false; + + if ($user->role === 'GUEST') { + $user->role = 'MEMBER'; + $user->updated_at = now(); + $user->save(); + $roleChanged = true; + } + + $application->user_id = (int) $user->id; + $application->accepted_at = now(); + $application->accepted_by = Auth::id(); + $application->updated_at = now(); + $application->save(); + + app_log('info', 'Mitgliedsantrag angenommen', [ + 'application_id' => (int) $id, + 'user_id' => (int) $user->id, + 'role' => $user->role, + ]); + + flash_set('status', $roleChanged + ? 'Antrag angenommen. Der Benutzer ist jetzt Mitglied.' + : 'Antrag angenommen. Der Benutzer hatte bereits eine Mitglieds- oder höhere Rolle.'); + + redirect('/admin/antraege/' . $id); + } + + public function deleteApplication(string $id): void + { + $application = MembershipApplication::find((int) $id); + + if ($application === null) { + flash_set('error', 'Der Antrag wurde nicht gefunden.'); + redirect('/admin/antraege'); + } + + $dir = storage_path('img_storage/mitgliedsantrag'); + + foreach (['unterschrift_1', 'unterschrift_2'] as $field) { + $file = basename((string) $application->$field); + + if ($file !== '' && is_file($dir . '/' . $file)) { + @unlink($dir . '/' . $file); + } + } + + $application->delete(); + + app_log('info', 'Mitgliedsantrag gelöscht', [ + 'application_id' => (int) $id, + ]); + + flash_set('status', 'Der Antrag wurde gelöscht.'); + redirect('/admin/antraege'); } public function editHome(): void diff --git a/motorradig/src/routes.php b/motorradig/src/routes.php index 62788f6..a3e37d2 100644 --- a/motorradig/src/routes.php +++ b/motorradig/src/routes.php @@ -40,6 +40,8 @@ $router->get('/admin/upload_mitgliedsantrag', [AdminController::class, 'uploadMi $router->post('/admin/store_mitgliedsantrag', [AdminController::class, 'storeMitgliedsantrag'], 'ADMIN'); $router->get('/admin/antraege', [AdminController::class, 'applications'], 'ADMIN'); $router->get('/admin/antraege/{id}', [AdminController::class, 'application'], 'ADMIN'); +$router->post('/admin/antraege/{id}/accept', [AdminController::class, 'acceptApplication'], 'ADMIN'); +$router->post('/admin/antraege/{id}/delete', [AdminController::class, 'deleteApplication'], 'ADMIN'); $router->get('/admin/edit_home', [AdminController::class, 'editHome'], 'ADMIN'); $router->get('/admin/edit_activities', [AdminController::class, 'editActivities'], 'ADMIN'); $router->post('/admin/store_home', [AdminController::class, 'storeHome'], 'ADMIN'); diff --git a/motorradig/views/admin/applications.php b/motorradig/views/admin/applications.php index bada86f..77bae7d 100644 --- a/motorradig/views/admin/applications.php +++ b/motorradig/views/admin/applications.php @@ -1,4 +1,8 @@ - +
@@ -10,6 +14,14 @@
+ + + + + + + +

@@ -22,13 +34,14 @@ Name E-Mail Telefon + Status Aktion - Es sind noch keine Anträge eingegangen. + Es sind noch keine Anträge eingegangen. @@ -39,10 +52,33 @@ vorname . ' ' . $application->nachname)) ?> email) ?> telefon) ?> - + + accepted_at)): ?> + Angenommen + + Offen + + + Ansehen + accepted_at)): ?> +
+ + +
+ +
+ + +
diff --git a/tools/migrate_motorradig.py b/tools/migrate_motorradig.py index ab4bf76..039c087 100755 --- a/tools/migrate_motorradig.py +++ b/tools/migrate_motorradig.py @@ -69,9 +69,30 @@ MIGRATIONS = [ `unterschrift_2` varchar(255) DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, + `accepted_at` timestamp NULL DEFAULT NULL, + `accepted_by` bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci''', + # Annahme-Felder für bestehende Installationen ergänzen (idempotent) + '''SET @has_accepted_at := (SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'membership_applications' AND COLUMN_NAME = 'accepted_at'); + SET @sql := IF(@has_accepted_at = 0, + 'ALTER TABLE `membership_applications` ADD COLUMN `accepted_at` timestamp NULL DEFAULT NULL AFTER `updated_at`', + 'DO 0'); + PREPARE mig_stmt FROM @sql; + EXECUTE mig_stmt; + DEALLOCATE PREPARE mig_stmt''', + + '''SET @has_accepted_by := (SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'membership_applications' AND COLUMN_NAME = 'accepted_by'); + SET @sql := IF(@has_accepted_by = 0, + 'ALTER TABLE `membership_applications` ADD COLUMN `accepted_by` bigint(20) unsigned NULL DEFAULT NULL AFTER `accepted_at`', + 'DO 0'); + PREPARE mig_stmt FROM @sql; + EXECUTE mig_stmt; + DEALLOCATE PREPARE mig_stmt''', + # tours.user_id -> tours.tour_guide_id umbenennen (idempotent) '''SET @has_user := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'tours' AND COLUMN_NAME = 'user_id');