Files
firefly-iii/app/Api/V1/Controllers/System/DynamicConfigController.php

125 lines
3.9 KiB
PHP
Raw Normal View History

2018-06-24 15:33:36 +02:00
<?php
2021-02-17 06:17:48 +01:00
/*
2018-06-24 15:33:36 +02:00
* ConfigurationController.php
2021-02-17 06:17:48 +01:00
* Copyright (c) 2021 james@firefly-iii.org
2018-06-24 15:33:36 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 15:33:36 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-06-24 15:33:36 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 15:33:36 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-06-24 15:33:36 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-24 15:33:36 +02:00
*/
declare(strict_types=1);
2021-02-17 06:17:48 +01:00
namespace FireflyIII\Api\V1\Controllers\System;
2018-06-24 15:33:36 +02:00
2021-02-17 06:17:48 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
2018-12-09 06:39:56 +01:00
use FireflyIII\Api\V1\Requests\ConfigurationRequest;
2018-06-24 15:33:36 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Configuration;
2018-07-05 06:10:35 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-06-24 15:33:36 +02:00
use Illuminate\Http\JsonResponse;
/**
2021-02-17 06:17:48 +01:00
* Class DynamicConfigController.
2019-09-04 17:39:39 +02:00
*
* @codeCoverageIgnore
2018-06-24 15:33:36 +02:00
*/
2021-02-17 06:17:48 +01:00
class DynamicConfigController extends Controller
2018-06-24 15:33:36 +02:00
{
2021-02-17 06:17:48 +01:00
private UserRepositoryInterface $repository;
2020-07-31 09:24:08 +02:00
2018-07-05 06:10:35 +02:00
/**
* ConfigurationController constructor.
2018-07-05 06:10:35 +02:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(UserRepositoryInterface::class);
return $next($request);
}
);
}
2018-06-24 15:33:36 +02:00
/**
* Show all configuration.
*
2018-07-05 21:18:53 +02:00
* @return JsonResponse
2018-06-24 15:33:36 +02:00
*/
2018-07-05 21:18:53 +02:00
public function index(): JsonResponse
2018-06-24 15:33:36 +02:00
{
$configData = $this->getConfigData();
2020-10-03 16:51:44 +02:00
return response()->json(['data' => $configData])->header('Content-Type', self::CONTENT_TYPE);
2018-06-24 15:33:36 +02:00
}
2021-02-17 06:17:48 +01:00
/**
* Show all configuration.
*
* @param string $value
* @return JsonResponse
*/
public function show(string $value): JsonResponse
{
$configData = $this->getConfigData();
return response()->json([$value => $configData[$value]])->header('Content-Type', self::CONTENT_TYPE);
}
2018-06-24 15:33:36 +02:00
/**
* Get all config values.
*
2018-06-24 15:33:36 +02:00
* @return array
*/
private function getConfigData(): array
{
/** @var Configuration $isDemoSite */
$isDemoSite = app('fireflyconfig')->get('is_demo_site');
/** @var Configuration $updateCheck */
$updateCheck = app('fireflyconfig')->get('permission_update_check');
/** @var Configuration $lastCheck */
$lastCheck = app('fireflyconfig')->get('last_update_check');
/** @var Configuration $singleUser */
$singleUser = app('fireflyconfig')->get('single_user_mode');
2020-03-15 08:16:16 +01:00
return [
2018-06-24 15:33:36 +02:00
'is_demo_site' => null === $isDemoSite ? null : $isDemoSite->data,
2021-02-17 06:17:48 +01:00
'permission_update_check' => null === $updateCheck ? null : (int)$updateCheck->data,
'last_update_check' => null === $lastCheck ? null : (int)$lastCheck->data,
2018-06-24 15:33:36 +02:00
'single_user_mode' => null === $singleUser ? null : $singleUser->data,
];
}
2020-10-18 08:01:10 +02:00
/**
* Update the configuration.
*
* @param ConfigurationRequest $request
* @param string $name
*
* @return JsonResponse
*/
public function update(ConfigurationRequest $request, string $name): JsonResponse
{
2021-02-17 06:17:48 +01:00
if (!$this->repository->hasRole(auth()->user(), 'owner')) {
throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore
}
2020-10-18 08:01:10 +02:00
$data = $request->getAll();
app('fireflyconfig')->set($name, $data['value']);
$configData = $this->getConfigData();
return response()->json(['data' => $configData])->header('Content-Type', self::CONTENT_TYPE);
}
}