38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use MintyPHP\Support\Flash;
|
||
|
|
use MintyPHP\Support\Guard;
|
||
|
|
use MintyPHP\Router;
|
||
|
|
use MintyPHP\Service\AuthService;
|
||
|
|
use MintyPHP\Service\DepartmentService;
|
||
|
|
use MintyPHP\Service\PermissionService;
|
||
|
|
use MintyPHP\Service\TenantScopeService;
|
||
|
|
|
||
|
|
Guard::requireLogin();
|
||
|
|
Guard::requirePermissionOrForbidden(PermissionService::DEPARTMENTS_DELETE);
|
||
|
|
|
||
|
|
$uuid = trim((string) ($id ?? ''));
|
||
|
|
if ($uuid === '') {
|
||
|
|
Router::redirect('admin/departments');
|
||
|
|
}
|
||
|
|
|
||
|
|
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
|
||
|
|
$department = $uuid !== '' ? DepartmentService::findByUuid($uuid) : null;
|
||
|
|
if ($department && !TenantScopeService::canAccess('departments', (int) ($department['id'] ?? 0), $currentUserId)) {
|
||
|
|
Router::redirect('error/forbidden');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = DepartmentService::deleteByUuid($uuid);
|
||
|
|
if (!($result['ok'] ?? false)) {
|
||
|
|
Flash::error('Department not found', 'admin/departments', 'department_not_found');
|
||
|
|
Router::redirect('admin/departments');
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($currentUserId > 0) {
|
||
|
|
AuthService::loadTenantDataIntoSession($currentUserId);
|
||
|
|
}
|
||
|
|
|
||
|
|
Flash::success('Department deleted', 'admin/departments', 'department_deleted');
|
||
|
|
Router::redirect('admin/departments');
|