Files
breadcrumb-the-shire/pages/admin/departments/delete($id).php

50 lines
1.6 KiB
PHP

<?php
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Router;
use MintyPHP\Service\Access\DepartmentAuthorizationPolicy;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
$session = app(SessionStoreInterface::class)->all();
Guard::requireLogin();
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
$uuid = trim((string) ($id ?? ''));
if ($uuid === '') {
Router::redirect('admin/departments');
return;
}
$currentUserId = (int) ($session['user']['id'] ?? 0);
$department = app(\MintyPHP\Service\Org\DepartmentService::class)->findByUuid($uuid);
if (!$department) {
Flash::error('Department not found', 'admin/departments', 'department_not_found');
Router::redirect('admin/departments');
return;
}
$departmentId = (int) ($department['id'] ?? 0);
$decision = $authorizationService->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_DELETE, [
'actor_user_id' => $currentUserId,
'target_department_id' => $departmentId,
]);
if (!$decision->isAllowed()) {
Router::redirect('error/forbidden');
return;
}
$result = app(\MintyPHP\Service\Org\DepartmentService::class)->deleteByUuid($uuid);
if (!($result['ok'] ?? false)) {
Flash::error('Department not found', 'admin/departments', 'department_not_found');
Router::redirect('admin/departments');
return;
}
if ($currentUserId > 0) {
app(\MintyPHP\Service\Auth\AuthService::class)->loadTenantDataIntoSession($currentUserId);
}
Flash::success('Department deleted', 'admin/departments', 'department_deleted');
Router::redirect('admin/departments');