nextcloud-pride-flags/lib/Controller/SettingsController.php

56 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2024-07-28 15:57:30 +00:00
<?php
declare(strict_types=1);
namespace OCA\PrideFlags\Controller;
use OCA\PrideFlags\Settings\AppSettings;
use Closure;
use OCP\IRequest;
use OCP\IConfig;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
2024-07-28 15:57:30 +00:00
class SettingsController extends Controller {
2024-08-08 21:16:22 +00:00
public function __construct($appName, IRequest $request, private AppSettings $appSettings, private $userId) {
2024-07-28 15:57:30 +00:00
parent::__construct($appName, $request);
}
#[NoAdminRequired]
#[NoCSRFRequired]
public function get(): JSONResponse {
2024-08-08 21:16:22 +00:00
return $this->makeJSONResponse(fn () => $this->appSettings->getAll($this->userId));
2024-07-28 15:57:30 +00:00
}
#[NoAdminRequired]
public function set(string $folderVariant, string $buttonVariant): JSONResponse {
2024-08-08 21:16:22 +00:00
$this->appSettings->set($this->userId, $folderVariant, $buttonVariant);
return $this->makeJSONResponse(fn () => $this->appSettings->getAll($this->userId));
2024-07-28 15:57:30 +00:00
}
#[AuthorizedAdminSetting]
public function setGlobal(string $folderVariant, string $buttonVariant): JSONResponse {
$this->appSettings->setGlobal($folderVariant, $buttonVariant);
return $this->makeJSONResponse(fn () => $this->appSettings->getGlobal());
}
#[NoCSRFRequired]
public function getGlobal(): JSONResponse {
return $this->makeJSONResponse(fn () => $this->appSettings->getGlobal());
}
2024-07-28 15:57:30 +00:00
protected function makeJSONResponse(Closure $closure): JSONResponse {
try {
return new JSONResponse($closure(), HTTP::STATUS_OK);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], HTTP::INTERNAL_SERVER_ERROR);
}
}
}