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

161 lines
6.2 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
use MintyPHP\Buffer;
use MintyPHP\Http\SessionStoreInterface;
2026-02-04 23:31:53 +01:00
use MintyPHP\Router;
use MintyPHP\Service\Access\DepartmentAuthorizationPolicy;
use MintyPHP\Session;
2026-02-23 12:58:19 +01:00
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
2026-02-04 23:31:53 +01:00
$session = app(SessionStoreInterface::class)->all();
2026-02-04 23:31:53 +01:00
Guard::requireLogin();
2026-03-04 15:56:58 +01:00
$request = requestInput();
$returnTarget = requestResolveReturnTarget();
$closeTarget = requestResolveReturnTarget('admin/departments');
2026-03-04 15:56:58 +01:00
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
2026-02-04 23:31:53 +01:00
$currentUserId = (int) ($session['user']['id'] ?? 0);
2026-02-04 23:31:53 +01:00
if ($currentUserId > 0) {
app(\MintyPHP\Service\Access\PermissionService::class)->getUserPermissions($currentUserId);
2026-02-04 23:31:53 +01:00
}
2026-03-04 15:56:58 +01:00
$departmentService = app(\MintyPHP\Service\Org\DepartmentService::class);
$tenantService = app(\MintyPHP\Service\Tenant\TenantService::class);
$directoryScopeGateway = app(\MintyPHP\Service\Tenant\TenantScopeService::class);
2026-02-04 23:31:53 +01:00
$uuid = trim((string) ($id ?? ''));
$editTarget = requestPathWithReturnTarget("admin/departments/edit/{$uuid}", $returnTarget);
2026-03-04 15:56:58 +01:00
$department = $uuid !== '' ? $departmentService->findByUuid($uuid) : null;
2026-02-04 23:31:53 +01:00
if (!$department) {
Flash::error('Department not found', $closeTarget, 'department_not_found');
Router::redirect($closeTarget);
2026-02-04 23:31:53 +01:00
}
$departmentId = (int) ($department['id'] ?? 0);
$contextDecision = $authorizationService->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT, [
'actor_user_id' => $currentUserId,
'target_department_id' => $departmentId,
]);
if (!$contextDecision->isAllowed()) {
2026-02-04 23:31:53 +01:00
Router::redirect('error/forbidden');
return;
}
$capabilities = $contextDecision->attribute('capabilities', []);
if (!is_array($capabilities)) {
$capabilities = [];
}
$canViewPage = (bool) ($capabilities['can_view_page'] ?? false);
$canUpdateDepartment = (bool) ($capabilities['can_update_department'] ?? false);
2026-03-12 13:01:06 +01:00
$canDeleteDepartment = (bool) ($capabilities['can_delete_department'] ?? false);
2026-03-04 15:56:58 +01:00
$viewAuth['page'] = [
'can_update_department' => $canUpdateDepartment,
2026-03-12 13:01:06 +01:00
'can_delete_department' => $canDeleteDepartment,
2026-03-04 15:56:58 +01:00
];
$allowedTenantIdsRaw = $capabilities['allowed_tenant_ids'] ?? [];
$allowedTenantIds = is_array($allowedTenantIdsRaw)
? array_values(array_unique(array_filter(array_map('intval', $allowedTenantIdsRaw), static fn (int $tenantId): bool => $tenantId > 0)))
: [];
if (!$canViewPage) {
Router::redirect('error/forbidden');
return;
}
app(\MintyPHP\Service\Audit\AuditMetadataEnricherInterface::class)->enrich($department);
2026-02-04 23:31:53 +01:00
2026-03-04 15:56:58 +01:00
$errorBag = formErrors();
2026-02-04 23:31:53 +01:00
$errors = [];
2026-02-11 19:28:12 +01:00
$warnings = [];
2026-02-04 23:31:53 +01:00
$form = $department;
2026-03-04 15:56:58 +01:00
$tenants = $tenantService->list();
2026-02-04 23:31:53 +01:00
if ($allowedTenantIds) {
$tenants = array_values(array_filter($tenants, static function (array $tenant) use ($allowedTenantIds): bool {
$tenantId = (int) ($tenant['id'] ?? 0);
return $tenantId > 0 && in_array($tenantId, $allowedTenantIds, true);
}));
if (!in_array((int) ($form['tenant_id'] ?? 0), $allowedTenantIds, true)) {
$form['tenant_id'] = 0;
}
2026-03-04 15:56:58 +01:00
} elseif ($directoryScopeGateway->isStrict()) {
2026-02-04 23:31:53 +01:00
$tenants = [];
$form['tenant_id'] = 0;
2026-02-04 23:31:53 +01:00
}
if ($request->isMethod('POST') && !Session::checkCsrfToken()) {
Flash::error(t('Form expired, please try again'), $editTarget, 'csrf_expired');
Router::redirect($editTarget);
return;
}
if ($request->isMethod('POST')) {
$submitDecision = $authorizationService->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_SUBMIT, [
'actor_user_id' => $currentUserId,
'target_department_id' => $departmentId,
2026-03-04 15:56:58 +01:00
'input' => $request->bodyAll(),
]);
if (!$submitDecision->isAllowed()) {
2026-02-04 23:31:53 +01:00
Router::redirect('error/forbidden');
return;
}
$submitCapabilities = $submitDecision->attribute('capabilities', []);
if (is_array($submitCapabilities)) {
$canUpdateDepartment = (bool) ($submitCapabilities['can_update_department'] ?? $canUpdateDepartment);
2026-03-12 13:01:06 +01:00
$canDeleteDepartment = (bool) ($submitCapabilities['can_delete_department'] ?? $canDeleteDepartment);
2026-03-04 15:56:58 +01:00
$viewAuth['page'] = [
'can_update_department' => $canUpdateDepartment,
2026-03-12 13:01:06 +01:00
'can_delete_department' => $canDeleteDepartment,
2026-03-04 15:56:58 +01:00
];
}
if (!$canUpdateDepartment) {
Router::redirect('error/forbidden');
return;
}
2026-03-04 15:56:58 +01:00
$selectedTenantId = $request->bodyInt('tenant_id');
if ($allowedTenantIds) {
$selectedTenantId = in_array($selectedTenantId, $allowedTenantIds, true) ? $selectedTenantId : 0;
2026-03-04 15:56:58 +01:00
} elseif ($directoryScopeGateway->isStrict()) {
$selectedTenantId = 0;
}
2026-03-04 15:56:58 +01:00
$input = $request->bodyAll();
$input['tenant_id'] = $selectedTenantId;
2026-03-04 15:56:58 +01:00
$result = $departmentService->updateFromAdmin($departmentId, $input, $currentUserId);
2026-02-04 23:31:53 +01:00
$form = $result['form'] ?? $form;
2026-03-04 15:56:58 +01:00
$errorBag->merge($result['errors'] ?? []);
2026-02-11 19:28:12 +01:00
$warnings = $result['warnings'] ?? [];
$form['tenant_id'] = $selectedTenantId;
2026-02-04 23:31:53 +01:00
2026-03-04 15:56:58 +01:00
if (($result['ok'] ?? false) && !$errorBag->hasAny()) {
$cleaned = $departmentService->cleanupUserAssignments($departmentId);
$action = (string) $request->body('action', 'save');
2026-02-04 23:31:53 +01:00
if ($cleaned > 0) {
Flash::info(t('Department assignments cleaned: %d', $cleaned), $editTarget, 'department_assignments_cleaned');
2026-02-04 23:31:53 +01:00
}
if ($action === 'save_close') {
2026-02-11 19:28:12 +01:00
if ($warnings) {
Flash::info(implode(' ', $warnings), $closeTarget, 'department_warning');
2026-02-11 19:28:12 +01:00
}
Flash::success('Department updated', $closeTarget, 'department_updated');
Router::redirect($closeTarget);
2026-02-04 23:31:53 +01:00
} else {
2026-02-11 19:28:12 +01:00
if ($warnings) {
Flash::info(implode(' ', $warnings), $editTarget, 'department_warning');
2026-02-11 19:28:12 +01:00
}
Flash::success('Department updated', $editTarget, 'department_updated');
Router::redirect($editTarget);
2026-02-04 23:31:53 +01:00
}
}
}
2026-03-04 15:56:58 +01:00
$validationSummaryErrors = $errorBag->toArray();
$errors = $errorBag->toFlatList();
$titleText = $canUpdateDepartment ? t('Edit department') : t('View department');
Buffer::set('title', $titleText);
$breadcrumbs = [
['label' => t('Home'), 'path' => 'admin'],
['label' => t('Departments'), 'path' => 'admin/departments'],
['label' => $titleText],
];