Files
breadcrumb-the-shire/pages/admin/departments/delete($id).php
fs c429ff43cd refactor: fix 7 CRUD system inconsistencies for robustness and maintainability
- Add user-assignment dependency check to DepartmentService.deleteByUuid (409 when users assigned)
- Standardize POST detection to isMethod('POST') in 10 create/edit actions
- Align RoleService code uniqueness to warning pattern (matching departments)
- Normalize repository create() return types from mixed to int|false (3 repos + 3 interfaces)
- Add JSON response branches to 4 non-user delete actions
- Document delete authorization ordering pattern
- Decompose AdminSettingsService.updateFromAdmin into domain-scoped private methods

Workflow: .agents/runs/CRUD-CONSISTENCY-001/
Reviewed by: Code Reviewer (pass), Security Reviewer (pass), Acceptance Tester (pass)
Quality gates: QG-001 PHPUnit pass, QG-002 PHPStan pass, QG-003 Architecture pass

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 18:25:33 +01:00

78 lines
2.4 KiB
PHP

<?php
use MintyPHP\Http\Request;
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) {
if (Request::wantsJson()) {
http_response_code(404);
Router::json(['error' => 'not_found']);
return;
}
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()) {
if (Request::wantsJson()) {
http_response_code($decision->status());
Router::json(['error' => $decision->error()]);
return;
}
Router::redirect('error/forbidden');
return;
}
$result = app(\MintyPHP\Service\Org\DepartmentService::class)->deleteByUuid($uuid);
if (!($result['ok'] ?? false)) {
$error = $result['error'] ?? 'not_found';
$status = $result['status'] ?? 500;
if (Request::wantsJson()) {
http_response_code($status);
Router::json(['error' => $error]);
return;
}
if ($error === 'department_has_users') {
Flash::error(t('Department can not be deleted while users are assigned'), 'admin/departments', 'department_has_users');
} else {
Flash::error(t('Department not found'), 'admin/departments', 'department_not_found');
}
Router::redirect('admin/departments');
return;
}
if ($currentUserId > 0) {
app(\MintyPHP\Service\Auth\AuthService::class)->loadTenantDataIntoSession($currentUserId);
}
if (Request::wantsJson()) {
http_response_code(204);
Router::json([]);
return;
}
Flash::success('Department deleted', 'admin/departments', 'department_deleted');
Router::redirect('admin/departments');