96 lines
3.4 KiB
PHP
96 lines
3.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use MintyPHP\Buffer;
|
||
|
|
use MintyPHP\Http\SessionStoreInterface;
|
||
|
|
use MintyPHP\Router;
|
||
|
|
use MintyPHP\Service\Access\SettingsAuthorizationPolicy;
|
||
|
|
use MintyPHP\Service\Settings\AdminSettingsService;
|
||
|
|
use MintyPHP\Support\Flash;
|
||
|
|
use MintyPHP\Support\Guard;
|
||
|
|
|
||
|
|
$session = app(SessionStoreInterface::class)->all();
|
||
|
|
Guard::requireLogin();
|
||
|
|
$request = requestInput();
|
||
|
|
|
||
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
||
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
||
|
|
$viewDecision = $authorizationService->authorize(SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_VIEW, [
|
||
|
|
'actor_user_id' => $currentUserId,
|
||
|
|
]);
|
||
|
|
if (!$viewDecision->isAllowed()) {
|
||
|
|
Guard::deny();
|
||
|
|
}
|
||
|
|
$viewCapabilities = $viewDecision->attribute('capabilities', []);
|
||
|
|
$canUpdateSettings = is_array($viewCapabilities) ? (bool) ($viewCapabilities['can_update_settings'] ?? false) : false;
|
||
|
|
|
||
|
|
$adminSettingsService = app(AdminSettingsService::class);
|
||
|
|
$pageData = $adminSettingsService->buildPageData();
|
||
|
|
$values = is_array($pageData['values'] ?? null) ? $pageData['values'] : [];
|
||
|
|
$settings = is_array($pageData['settings'] ?? null) ? $pageData['settings'] : [];
|
||
|
|
$activeLoginTokens = (int) ($pageData['active_login_tokens'] ?? 0);
|
||
|
|
|
||
|
|
if ($request->isMethod('POST') && !actionRequireCsrf('admin/settings/security', 'admin/settings/security', 'csrf_expired')) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($request->isMethod('POST')) {
|
||
|
|
$updateDecision = $authorizationService->authorize(SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_UPDATE, [
|
||
|
|
'actor_user_id' => $currentUserId,
|
||
|
|
]);
|
||
|
|
if (!$updateDecision->isAllowed()) {
|
||
|
|
Guard::deny();
|
||
|
|
}
|
||
|
|
|
||
|
|
$sectionKeys = [
|
||
|
|
'app_registration',
|
||
|
|
'session_idle_timeout_minutes',
|
||
|
|
'session_absolute_timeout_hours',
|
||
|
|
'remember_token_lifetime_days',
|
||
|
|
'microsoft_auto_remember_default',
|
||
|
|
'user_inactivity_deactivate_days',
|
||
|
|
'user_inactivity_delete_days',
|
||
|
|
'system_audit_enabled',
|
||
|
|
'system_audit_retention_days',
|
||
|
|
'frontend_telemetry_enabled',
|
||
|
|
'frontend_telemetry_sample_rate',
|
||
|
|
'frontend_telemetry_allowed_events',
|
||
|
|
];
|
||
|
|
$mergedPost = settingsSectionMergePost($values, $request->bodyAll(), $sectionKeys);
|
||
|
|
|
||
|
|
$updateResult = $adminSettingsService->updateFromAdmin($mergedPost);
|
||
|
|
$errorBag = formErrors();
|
||
|
|
foreach ((array) ($updateResult['errors'] ?? []) as $error) {
|
||
|
|
if (is_array($error)) {
|
||
|
|
$message = trim((string) ($error['message'] ?? ''));
|
||
|
|
$field = trim((string) ($error['field'] ?? 'input'));
|
||
|
|
if ($message !== '') {
|
||
|
|
$errorBag->add($field !== '' ? $field : 'input', $message);
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$message = trim((string) $error);
|
||
|
|
if ($message !== '') {
|
||
|
|
$errorBag->addGlobal($message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($errorBag->hasAny()) {
|
||
|
|
flashFormErrors($errorBag, 'admin/settings/security', 'settings_update_error');
|
||
|
|
Router::redirect('admin/settings/security');
|
||
|
|
}
|
||
|
|
|
||
|
|
$redirectTarget = ((string) $request->body('action', 'save') === 'save_close')
|
||
|
|
? 'admin/settings'
|
||
|
|
: 'admin/settings/security';
|
||
|
|
Flash::success('Settings updated', $redirectTarget, 'settings_updated');
|
||
|
|
Router::redirect($redirectTarget);
|
||
|
|
}
|
||
|
|
|
||
|
|
Buffer::set('title', t('Security settings'));
|
||
|
|
|
||
|
|
$breadcrumbs = [
|
||
|
|
['label' => t('Home'), 'path' => 'admin'],
|
||
|
|
['label' => t('Settings'), 'path' => 'admin/settings'],
|
||
|
|
['label' => t('Security')],
|
||
|
|
];
|