105 lines
4.9 KiB
PHP
105 lines
4.9 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Http\ApiBootstrap;
|
|
use MintyPHP\Http\ApiResponse;
|
|
use MintyPHP\Service\Access\PermissionService;
|
|
use MintyPHP\Service\Settings\SettingServicesFactory;
|
|
|
|
ApiBootstrap::init();
|
|
|
|
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
|
|
$settingService = (new SettingServicesFactory())->createSettingService();
|
|
|
|
if ($method === 'GET') {
|
|
ApiResponse::requirePermission(PermissionService::SETTINGS_VIEW);
|
|
|
|
ApiResponse::success([
|
|
'data' => buildSettingsResponse($settingService),
|
|
]);
|
|
}
|
|
|
|
if ($method === 'PUT' || $method === 'PATCH') {
|
|
ApiResponse::requirePermission(PermissionService::SETTINGS_UPDATE);
|
|
|
|
$input = ApiResponse::readJsonBody();
|
|
$errors = [];
|
|
|
|
$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),
|
|
'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[$key] = ['invalid_value'];
|
|
}
|
|
}
|
|
|
|
if ($errors) {
|
|
ApiResponse::validationError($errors);
|
|
}
|
|
|
|
ApiResponse::success([
|
|
'data' => buildSettingsResponse($settingService),
|
|
]);
|
|
}
|
|
|
|
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(),
|
|
'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(),
|
|
];
|
|
}
|