firefly-iii/app/Api/V1/Controllers/ConfigurationController.php

122 lines
3.8 KiB
PHP
Raw Normal View History

2018-06-24 08:33:36 -05:00
<?php
/**
* ConfigurationController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
2018-12-08 23:39:56 -06:00
use FireflyIII\Api\V1\Requests\ConfigurationRequest;
2018-06-24 08:33:36 -05:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Configuration;
2018-07-04 23:10:35 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
2018-06-24 08:33:36 -05:00
use Illuminate\Http\JsonResponse;
/**
* Class ConfigurationController.
2019-09-04 10:39:39 -05:00
*
* @codeCoverageIgnore
2018-06-24 08:33:36 -05:00
*/
class ConfigurationController extends Controller
{
2018-07-04 23:10:35 -05:00
/** @var UserRepositoryInterface The user repository */
2018-07-04 23:10:35 -05:00
private $repository;
/**
* ConfigurationController constructor.
*
2018-07-04 23:10:35 -05:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2018-07-05 14:18:53 -05:00
/** @noinspection UnusedConstructorDependenciesInspection */
2018-07-04 23:10:35 -05:00
$this->repository = app(UserRepositoryInterface::class);
2018-07-05 11:02:02 -05:00
/** @var User $admin */
$admin = auth()->user();
if (!$this->repository->hasRole($admin, 'owner')) {
throw new FireflyException('No access to method.'); // @codeCoverageIgnore
}
2018-07-04 23:10:35 -05:00
return $next($request);
}
);
}
2018-06-24 08:33:36 -05:00
/**
* Show all configuration.
*
2018-07-05 14:18:53 -05:00
* @return JsonResponse
2018-06-24 08:33:36 -05:00
*/
2018-07-05 14:18:53 -05:00
public function index(): JsonResponse
2018-06-24 08:33:36 -05:00
{
$configData = $this->getConfigData();
2019-02-13 10:38:41 -06:00
return response()->json(['data' => $configData])->header('Content-Type', 'application/vnd.api+json');
2018-06-24 08:33:36 -05:00
}
2019-06-09 01:26:23 -05:00
/**
* Update the configuration.
*
* @param ConfigurationRequest $request
2019-09-04 10:39:39 -05:00
* @param string $name
2019-06-09 01:26:23 -05:00
*
* @return JsonResponse
*/
public function update(ConfigurationRequest $request, string $name): JsonResponse
{
$data = $request->getAll();
app('fireflyconfig')->set($name, $data['value']);
$configData = $this->getConfigData();
return response()->json(['data' => $configData])->header('Content-Type', 'application/vnd.api+json');
}
2018-06-24 08:33:36 -05:00
/**
* Get all config values.
*
2018-06-24 08:33:36 -05: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');
$data = [
'is_demo_site' => null === $isDemoSite ? null : $isDemoSite->data,
'permission_update_check' => null === $updateCheck ? null : (int)$updateCheck->data,
'last_update_check' => null === $lastCheck ? null : (int)$lastCheck->data,
'single_user_mode' => null === $singleUser ? null : $singleUser->data,
];
return $data;
}
}