52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Service\Access\SettingsAuthorizationPolicy;
|
|
use MintyPHP\Support\Flash;
|
|
use MintyPHP\Support\Guard;
|
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
|
Guard::requireLogin();
|
|
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
|
$decision = $authorizationService->authorize(SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_USER_LIFECYCLE_RUN, [
|
|
'actor_user_id' => $currentUserId,
|
|
]);
|
|
if (!$decision->isAllowed()) {
|
|
Guard::deny();
|
|
}
|
|
|
|
if (!actionRequirePost('admin/settings')) {
|
|
return;
|
|
}
|
|
|
|
if (!actionRequireCsrf('admin/settings', 'admin/settings', 'user_lifecycle_run')) {
|
|
return;
|
|
}
|
|
|
|
$errorBag = formErrors();
|
|
$result = app(\MintyPHP\Service\User\UserLifecycleService::class)->run($currentUserId);
|
|
if (!($result['ok'] ?? false)) {
|
|
$error = (string) ($result['error'] ?? 'unexpected_error');
|
|
if ($error === 'lock_not_acquired') {
|
|
$errorBag->addGlobal('User lifecycle already running');
|
|
} else {
|
|
$errorBag->addGlobal('User lifecycle failed');
|
|
}
|
|
flashFormErrors($errorBag, 'admin/settings', 'user_lifecycle_run');
|
|
Router::redirect('admin/settings');
|
|
}
|
|
|
|
Flash::success(
|
|
sprintf(
|
|
t('User lifecycle completed: %d deactivated, %d deleted'),
|
|
(int) ($result['deactivated_count'] ?? 0),
|
|
(int) ($result['deleted_count'] ?? 0)
|
|
),
|
|
'admin/settings',
|
|
'user_lifecycle_completed'
|
|
);
|
|
Router::redirect('admin/settings');
|