Files
server/core/lib/Controllers/TenantSettingsController.php
T
2026-07-27 00:48:11 -04:00

75 lines
2.2 KiB
PHP

<?php
namespace KTXC\Controllers;
use KTXC\Http\Response\JsonResponse;
use KTXC\Service\TenantService;
use KTXC\Context\TenantContextInterface;
use KTXF\Controller\ControllerAbstract;
use KTXF\Routing\Attributes\AuthenticatedRoute;
/**
* Tenant-scoped settings controller.
*
* Mirrors UserSettingsController but operates on the current tenant record
* rather than the current user. Write access is guarded by the
* `tenant.settings.update` permission so only administrators can mutate
* tenant-wide configuration.
*/
class TenantSettingsController extends ControllerAbstract
{
public function __construct(
private readonly TenantContextInterface $tenantContext,
private readonly TenantService $tenantService,
) {}
/**
* Retrieve all settings for the current tenant.
*
* @return JsonResponse Settings data as key-value pairs
*/
#[AuthenticatedRoute(
'/tenant/settings',
name: 'tenant.settings.read',
methods: ['GET'],
permissions: ['tenant.settings.read'],
)]
public function read(): JsonResponse
{
$settings = $this->tenantService->fetchSettings($this->tenantContext->identifier());
return new JsonResponse($settings, JsonResponse::HTTP_OK);
}
/**
* Update one or more settings for the current tenant.
*
* @param array $data Key-value pairs to persist
*
* @example request body:
* {
* "data": {
* "theme_default_mode": "dark",
* "theme_palette": {"light": {"colors": {"primary": "#0284C7"}}},
* "theme_lock": true
* }
* }
*
* @return JsonResponse The updated values that were written
*/
#[AuthenticatedRoute(
'/tenant/settings',
name: 'tenant.settings.update',
methods: ['PUT', 'PATCH'],
permissions: ['tenant.settings.update'],
)]
public function update(array $data): JsonResponse
{
$this->tenantService->storeSettings($this->tenantContext->identifier(), $data);
$updatedSettings = $this->tenantService->fetchSettings($this->tenantContext->identifier(), array_keys($data));
return new JsonResponse($updatedSettings, JsonResponse::HTTP_OK);
}
}