48 lines
1.7 KiB
PHP
48 lines
1.7 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Service\Access\UserAuthorizationPolicy;
|
|
use MintyPHP\Support\Flash;
|
|
use MintyPHP\Support\Guard;
|
|
|
|
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;
|
|
}
|
|
|
|
$name = trim((string) (requestInput()->bodyAll()['token_name'] ?? ''));
|
|
|
|
if ($name === '') {
|
|
Flash::error(t('Token name is required'), "admin/users/edit/{$uuid}", 'api_token_error');
|
|
Router::redirect("admin/users/edit/{$uuid}");
|
|
}
|
|
|
|
$result = $apiTokenService->create($userId, $name, null, null, $currentUserId);
|
|
|
|
if ($result['ok'] ?? false) {
|
|
$_SESSION['api_token_created'] = ['token' => $result['token'], 'uuid' => $uuid];
|
|
Flash::success(t('API token created. Copy it now — it will not be shown again.'), "admin/users/edit/{$uuid}", 'api_token_created');
|
|
} else {
|
|
$error = $result['error'] ?? 'create_failed';
|
|
Flash::error(t('Could not create API token: %s', $error), "admin/users/edit/{$uuid}", 'api_token_error');
|
|
}
|
|
|
|
Router::redirect("admin/users/edit/{$uuid}");
|