Files
server/core/lib/Controllers/TenantSettingsController.php
Sebastian Krupinski b68ac538ce
All checks were successful
JS Unit Tests / test (pull_request) Successful in 17s
Build Test / build (pull_request) Successful in 19s
PHP Unit Tests / test (pull_request) Successful in 54s
feat: theming
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-02-22 21:25:26 -05:00

75 lines
2.1 KiB
PHP

<?php
namespace KTXC\Controllers;
use KTXC\Http\Response\JsonResponse;
use KTXC\Service\TenantService;
use KTXC\SessionTenant;
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 SessionTenant $tenantIdentity,
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->tenantIdentity->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": {
* "default_mode": "dark",
* "primary_color": "#6366F1",
* "lock_user_colors": 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->tenantIdentity->identifier(), $data);
$updatedSettings = $this->tenantService->fetchSettings($this->tenantIdentity->identifier(), array_keys($data));
return new JsonResponse($updatedSettings, JsonResponse::HTTP_OK);
}
}