forked from fa/breadcrumb-the-shire
CSRF guards added to 16 admin pages that previously accepted POST requests without token verification (GR-SEC-001): departments, permissions, roles, settings, tenants, users (create/edit/bulk/tokens), and lang switch. New contract tests: - PostEndpointCsrfContractTest: scans all pages/ for POST handlers and verifies checkCsrfToken is present (API/data endpoints exempt). - DatabaseUpdatesContractTest: validates db/updates/ scripts follow naming convention and use idempotent SQL patterns (GR-CORE-010). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Service\Access\UserAuthorizationPolicy;
|
|
use MintyPHP\Session;
|
|
use MintyPHP\Support\Flash;
|
|
use MintyPHP\Support\Guard;
|
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
|
Guard::requireLogin();
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
|
$userAccountService = app(\MintyPHP\Service\User\UserAccountService::class);
|
|
$apiTokenService = app(\MintyPHP\Service\Auth\ApiTokenService::class);
|
|
|
|
$uuid = trim((string) ($id ?? ''));
|
|
$user = $uuid !== '' ? $userAccountService->findByUuid($uuid) : null;
|
|
if (!$user) {
|
|
Router::redirect('admin/users');
|
|
}
|
|
|
|
$userId = (int) ($user['id'] ?? 0);
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
|
$decision = $authorizationService->authorize(UserAuthorizationPolicy::ABILITY_ADMIN_USERS_API_TOKENS_MANAGE, [
|
|
'actor_user_id' => $currentUserId,
|
|
'target_user_id' => $userId,
|
|
]);
|
|
if (!$decision->isAllowed()) {
|
|
Router::redirect('error/forbidden');
|
|
return;
|
|
}
|
|
|
|
if (!Session::checkCsrfToken()) {
|
|
Flash::error(t('Form expired, please try again'), "admin/users/edit/{$uuid}", 'csrf_expired');
|
|
Router::redirect("admin/users/edit/{$uuid}");
|
|
return;
|
|
}
|
|
|
|
$tokenId = (int) (requestInput()->bodyAll()['token_id'] ?? 0);
|
|
|
|
if ($tokenId > 0) {
|
|
$apiTokenService->revoke($tokenId);
|
|
Flash::success(t('API token revoked'), "admin/users/edit/{$uuid}", 'api_token_revoked');
|
|
}
|
|
|
|
Router::redirect("admin/users/edit/{$uuid}");
|