2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-03-22 18:25:33 +01:00
|
|
|
use MintyPHP\Http\Request;
|
2026-03-06 11:28:22 +01:00
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
2026-02-04 23:31:53 +01:00
|
|
|
use MintyPHP\Router;
|
2026-02-24 08:49:40 +01:00
|
|
|
use MintyPHP\Service\Access\PermissionAuthorizationPolicy;
|
2026-03-13 11:31:33 +01:00
|
|
|
use MintyPHP\Service\Access\PermissionService;
|
2026-03-05 11:17:42 +01:00
|
|
|
use MintyPHP\Support\Flash;
|
|
|
|
|
use MintyPHP\Support\Guard;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-06 11:28:22 +01:00
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
2026-02-04 23:31:53 +01:00
|
|
|
Guard::requireLogin();
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-06 11:28:22 +01:00
|
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
2026-03-04 15:56:58 +01:00
|
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
2026-03-22 18:25:33 +01:00
|
|
|
// Authorization is checked generically (can user delete ANY permission?) before loading the entity.
|
|
|
|
|
// Context-aware policies (departments, tenants, users) load the entity first to pass target_id.
|
|
|
|
|
// Both patterns are valid; context-aware is preferred for new code.
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $authorizationService->authorize(PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_DELETE, [
|
|
|
|
|
'actor_user_id' => $currentUserId,
|
|
|
|
|
]);
|
|
|
|
|
if (!$decision->isAllowed()) {
|
2026-03-22 18:25:33 +01:00
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code($decision->status());
|
|
|
|
|
Router::json(['error' => $decision->error()]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-24 08:49:40 +01:00
|
|
|
Guard::deny();
|
|
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
$id = (int) ($id ?? 0);
|
|
|
|
|
if ($id <= 0) {
|
|
|
|
|
Router::redirect('admin/permissions');
|
2026-02-24 08:49:40 +01:00
|
|
|
return;
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$roleIds = app(\MintyPHP\Repository\Access\RolePermissionRepository::class)->listRoleIdsByPermissionId($id);
|
|
|
|
|
$affectedUserIds = app(\MintyPHP\Repository\Access\UserRoleRepository::class)->listUserIdsByRoleIds($roleIds);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$result = app(PermissionService::class)->deleteById($id);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!($result['ok'] ?? false)) {
|
2026-03-22 18:25:33 +01:00
|
|
|
$error = (string) ($result['error'] ?? 'not_found');
|
|
|
|
|
$status = $result['status'] ?? 500;
|
|
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code($status);
|
|
|
|
|
Router::json(['error' => $error]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-11 19:28:12 +01:00
|
|
|
if ($error === 'system_permission_protected') {
|
|
|
|
|
Flash::error('System permission can not be deleted', 'admin/permissions', 'permission_system_protected');
|
|
|
|
|
} else {
|
|
|
|
|
Flash::error('Permission not found', 'admin/permissions', 'permission_not_found');
|
|
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
Router::redirect('admin/permissions');
|
2026-03-22 18:25:33 +01:00
|
|
|
return;
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
if ($affectedUserIds) {
|
|
|
|
|
app(\MintyPHP\Repository\User\UserWriteRepository::class)->bumpAuthzVersionByUserIds($affectedUserIds);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 18:25:33 +01:00
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code(204);
|
|
|
|
|
Router::json([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 23:31:53 +01:00
|
|
|
Flash::success('Permission deleted', 'admin/permissions', 'permission_deleted');
|
|
|
|
|
Router::redirect('admin/permissions');
|