Files
breadcrumb-the-shire/pages/api/v1/settings/index().php
2026-03-05 11:17:42 +01:00

124 lines
6.0 KiB
PHP

<?php
use MintyPHP\Http\ApiAuth;
use MintyPHP\Http\ApiBootstrap;
use MintyPHP\Http\ApiResponse;
use MintyPHP\Service\Audit\SystemAuditService;
use MintyPHP\Service\Settings\SettingServicesFactory;
ApiBootstrap::init();
$request = requestInput();
$method = $request->method();
$settingService = (app(SettingServicesFactory::class))->createSettingService();
if ($method === 'GET') {
ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_SETTINGS_VIEW);
ApiResponse::success([
'data' => buildSettingsResponse($settingService),
]);
}
if ($method === 'PUT' || $method === 'PATCH') {
ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_SETTINGS_UPDATE);
$input = $request->bodyAll();
$errors = formErrors();
$beforeSettings = buildSettingsResponse($settingService);
$setters = [
'app_title' => static fn (?string $v) => $settingService->setAppTitle($v),
'app_locale' => static fn (?string $v) => $settingService->setAppLocale($v),
'app_theme' => static fn (?string $v) => $settingService->setAppTheme($v),
'app_theme_user_allowed' => static fn ($v) => $settingService->setUserThemeAllowed((bool) $v),
'app_registration_enabled' => static fn ($v) => $settingService->setRegistrationEnabled((bool) $v),
'app_primary_color' => static fn (?string $v) => $settingService->setAppPrimaryColor($v),
'api_token_default_ttl_days' => static fn ($v) => $settingService->setApiTokenDefaultTtlDays($v !== null ? (int) $v : null),
'api_token_max_ttl_days' => static fn ($v) => $settingService->setApiTokenMaxTtlDays($v !== null ? (int) $v : null),
'api_cors_allowed_origins' => static fn (?string $v) => $settingService->setApiCorsAllowedOrigins($v),
'default_tenant_id' => static fn ($v) => $settingService->setDefaultTenantId($v !== null ? (int) $v : null),
'default_role_id' => static fn ($v) => $settingService->setDefaultRoleId($v !== null ? (int) $v : null),
'default_department_id' => static fn ($v) => $settingService->setDefaultDepartmentId($v !== null ? (int) $v : null),
'user_inactivity_deactivate_days' => static fn ($v) => $settingService->setUserInactivityDeactivateDays($v !== null ? (int) $v : null),
'user_inactivity_delete_days' => static fn ($v) => $settingService->setUserInactivityDeleteDays($v !== null ? (int) $v : null),
'system_audit_enabled' => static fn ($v) => $settingService->setSystemAuditEnabled((bool) $v),
'system_audit_retention_days' => static fn ($v) => $settingService->setSystemAuditRetentionDays($v !== null ? (int) $v : null),
'microsoft_shared_client_id' => static fn (?string $v) => $settingService->setMicrosoftSharedClientId($v),
'microsoft_authority' => static fn (?string $v) => $settingService->setMicrosoftAuthority($v),
'smtp_host' => static fn (?string $v) => $settingService->setSmtpHost($v),
'smtp_port' => static fn ($v) => $settingService->setSmtpPort($v !== null ? (int) $v : null),
'smtp_user' => static fn (?string $v) => $settingService->setSmtpUser($v),
'smtp_password' => static fn (?string $v) => $settingService->setSmtpPassword($v),
'smtp_secure' => static fn (?string $v) => $settingService->setSmtpSecure($v),
'smtp_from' => static fn (?string $v) => $settingService->setSmtpFrom($v),
'smtp_from_name' => static fn (?string $v) => $settingService->setSmtpFromName($v),
];
foreach ($input as $key => $value) {
if (!isset($setters[$key])) {
continue;
}
$ok = $setters[$key]($value);
if (!$ok) {
$errors->add($key, 'invalid_value');
}
}
if ($errors->hasAny()) {
app(SystemAuditService::class)->record('admin.settings.update', 'failed', [
'actor_user_id' => ApiAuth::userId(),
'error_code' => 'validation_error',
'metadata' => ['errors' => array_keys($errors->toArray())],
]);
ApiResponse::validationFromFormErrors($errors);
}
$afterSettings = buildSettingsResponse($settingService);
app(SystemAuditService::class)->record('admin.settings.update', 'success', [
'actor_user_id' => ApiAuth::userId(),
'before' => $beforeSettings,
'after' => $afterSettings,
]);
ApiResponse::success([
'data' => $afterSettings,
]);
}
ApiResponse::methodNotAllowed();
/**
* @param \MintyPHP\Service\Settings\SettingService $s
* @return array<string, mixed>
*/
function buildSettingsResponse(\MintyPHP\Service\Settings\SettingService $s): array
{
return [
'app_title' => $s->getAppTitle(),
'app_locale' => $s->getAppLocale(),
'app_theme' => $s->getAppTheme(),
'app_theme_user_allowed' => $s->isUserThemeAllowed(),
'app_registration_enabled' => $s->isRegistrationEnabled(),
'app_primary_color' => $s->getAppPrimaryColor(),
'api_token_default_ttl_days' => $s->getApiTokenDefaultTtlDays(),
'api_token_max_ttl_days' => $s->getApiTokenMaxTtlDays(),
'api_cors_allowed_origins' => $s->getApiCorsAllowedOrigins(),
'default_tenant_id' => $s->getDefaultTenantId(),
'default_role_id' => $s->getDefaultRoleId(),
'default_department_id' => $s->getDefaultDepartmentId(),
'user_inactivity_deactivate_days' => $s->getUserInactivityDeactivateDays(),
'user_inactivity_delete_days' => $s->getUserInactivityDeleteDays(),
'system_audit_enabled' => $s->isSystemAuditEnabled(),
'system_audit_retention_days' => $s->getSystemAuditRetentionDays(),
'microsoft_shared_client_id' => $s->getMicrosoftSharedClientId(),
'microsoft_authority' => $s->getMicrosoftAuthority(),
'smtp_host' => $s->getSmtpHost(),
'smtp_port' => $s->getSmtpPort(),
'smtp_user' => $s->getSmtpUser(),
'smtp_secure' => $s->getSmtpSecure(),
'smtp_from' => $s->getSmtpFrom(),
'smtp_from_name' => $s->getSmtpFromName(),
];
}