Fix config routes.

This commit is contained in:
James Cole 2021-03-07 07:43:18 +01:00
parent 7606757d78
commit f2e5e7d182
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
6 changed files with 234 additions and 44 deletions

View File

@ -0,0 +1,184 @@
<?php
/*
* ConfigurationController.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
namespace FireflyIII\Api\V1\Controllers\System;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\System\UpdateRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Support\Binder\EitherConfigKey;
use Illuminate\Http\JsonResponse;
use Log;
/**
* Class ConfigurationController
*/
class ConfigurationController extends Controller
{
private UserRepositoryInterface $repository;
/**
* ConfigurationController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(UserRepositoryInterface::class);
return $next($request);
}
);
}
/**
* @return JsonResponse
* @throws FireflyException
*/
public function index(): JsonResponse
{
try {
$dynamicData = $this->getDynamicConfiguration();
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException('200030: Could not load config variables.');
}
$staticData = $this->getStaticConfiguration();
$return = [];
foreach ($dynamicData as $key => $value) {
$return[] = [
'title' => sprintf('configuration.%s', $key),
'value' => $value,
'editable' => true,
];
}
foreach ($staticData as $key => $value) {
$return[] = [
'title' => $key,
'value' => $value,
'editable' => false,
];
}
return response()->json($return);
}
/**
* @param string $configKey
*
* @return JsonResponse
* @throws FireflyException
*/
public function show(string $configKey): JsonResponse
{
$data = [];
$dynamic = $this->getDynamicConfiguration();
$shortKey = str_replace('configuration.', '', $configKey);
if ('configuration.' === substr($configKey, 0, 14)) {
$data = [
'title' => $configKey,
'value' => $dynamic[$shortKey],
'editable' => true,
];
}
if ('configuration.' !== substr($configKey, 0, 14)) {
$data = [
'title' => $configKey,
'value' => config($configKey),
'editable' => false,
];
}
return response()->json(['data' => $data])->header('Content-Type', self::CONTENT_TYPE);
}
/**
* Update the configuration.
*
* @param UpdateRequest $request
* @param string $name
*
* @return JsonResponse
*/
public function update(UpdateRequest $request, string $name): JsonResponse
{
if (!$this->repository->hasRole(auth()->user(), 'owner')) {
throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore
}
$data = $request->getAll();
$shortName = str_replace('configuration.','', $name);
app('fireflyconfig')->set($shortName, $data['value']);
// get updated config:
$newConfig = $this->getDynamicConfiguration();
$data = [
'title' => $name,
'value' => $newConfig[$shortName],
'editable' => true,
];
return response()->json(['data' => $data])->header('Content-Type', self::CONTENT_TYPE);
}
/**
* @return array
*/
private function getStaticConfiguration(): array
{
$list = EitherConfigKey::$static;
$return = [];
foreach ($list as $key) {
$return[$key] = config($key);
}
return $return;
}
/**
* Get all config values.
*
* @return array
* @throws FireflyException
*/
private function getDynamicConfiguration(): array
{
$isDemoSite = app('fireflyconfig')->get('is_demo_site');
$updateCheck = app('fireflyconfig')->get('permission_update_check');
$lastCheck = app('fireflyconfig')->get('last_update_check');
$singleUser = app('fireflyconfig')->get('single_user_mode');
return [
'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,
];
}
}

View File

@ -22,7 +22,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests; namespace FireflyIII\Api\V1\Requests\System;
use FireflyIII\Rules\IsBoolean; use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ChecksLogin;
@ -30,11 +30,11 @@ use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
/** /**
* Class ConfigurationRequest * Class UpdateRequest
* *
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
class ConfigurationRequest extends FormRequest class UpdateRequest extends FormRequest
{ {
use ConvertsDataTypes, ChecksLogin; use ConvertsDataTypes, ChecksLogin;
@ -47,14 +47,15 @@ class ConfigurationRequest extends FormRequest
*/ */
public function getAll(): array public function getAll(): array
{ {
$name = $this->route()->parameter('configName'); $name = $this->route()->parameter('dynamicConfigKey');
switch ($name) { switch ($name) {
default: default:
break; break;
case 'is_demo_site': case 'configuration.is_demo_site':
case 'single_user_mode': case 'configuration.single_user_mode':
return ['value' => $this->boolean('value')]; return ['value' => $this->boolean('value')];
case 'permission_update_check': case 'configuration.permission_update_check':
case 'configuration.last_update_check':
return ['value' => $this->integer('value')]; return ['value' => $this->integer('value')];
} }
@ -72,11 +73,13 @@ class ConfigurationRequest extends FormRequest
switch ($name) { switch ($name) {
default: default:
break; break;
case 'is_demo_site': case 'configuration.is_demo_site':
case 'single_user_mode': case 'configuration.single_user_mode':
return ['value' => ['required', new IsBoolean]]; return ['value' => ['required', new IsBoolean]];
case 'permission_update_check': case 'configuration.permission_update_check':
return ['value' => 'required|numeric|between:-1,1']; return ['value' => 'required|numeric|between:-1,1'];
case 'configuration.last_update_check':
return ['value' => 'required|numeric|min:464272080'];
} }
return ['value' => 'required']; // @codeCoverageIgnore return ['value' => 'required']; // @codeCoverageIgnore

View File

@ -29,6 +29,13 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
*/ */
class DynamicConfigKey class DynamicConfigKey
{ {
public static array $accepted = [
'configuration.is_demo_site',
'configuration.permission_update_check',
'configuration.single_user_mode',
'configuration.last_update_check',
];
/** /**
* @param string $value * @param string $value
* @param Route $route * @param Route $route
@ -38,8 +45,7 @@ class DynamicConfigKey
*/ */
public static function routeBinder(string $value, Route $route): string public static function routeBinder(string $value, Route $route): string
{ {
$accepted = ['is_demo_site', 'permission_update_check', 'single_user_mode']; if (in_array($value, self::$accepted, true)) {
if (in_array($value, $accepted, true)) {
return $value; return $value;
} }
throw new NotFoundHttpException; throw new NotFoundHttpException;

View File

@ -26,16 +26,16 @@ use Illuminate\Routing\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Class StaticConfigKey * Class EitherConfigKey
*/ */
class StaticConfigKey class EitherConfigKey
{ {
public static array $accepted = [ public static array $static = [
'firefly.version', 'firefly.version',
'firefly.api_version', 'firefly.api_version',
'firefly.default_location', 'firefly.default_location',
'firefly.account_to_transaction', 'firefly.account_to_transaction',
'firefly.allowed_opposing_types' 'firefly.allowed_opposing_types',
]; ];
/** /**
* @param string $value * @param string $value
@ -46,7 +46,7 @@ class StaticConfigKey
*/ */
public static function routeBinder(string $value, Route $route): string public static function routeBinder(string $value, Route $route): string
{ {
if (in_array($value, self::$accepted, true)) { if (in_array($value, self::$static, true) || in_array($value, DynamicConfigKey::$accepted, true)) {
return $value; return $value;
} }
throw new NotFoundHttpException; throw new NotFoundHttpException;

View File

@ -53,7 +53,7 @@ use FireflyIII\Support\Binder\CurrencyCode;
use FireflyIII\Support\Binder\Date; use FireflyIII\Support\Binder\Date;
use FireflyIII\Support\Binder\DynamicConfigKey; use FireflyIII\Support\Binder\DynamicConfigKey;
use FireflyIII\Support\Binder\JournalList; use FireflyIII\Support\Binder\JournalList;
use FireflyIII\Support\Binder\StaticConfigKey; use FireflyIII\Support\Binder\EitherConfigKey;
use FireflyIII\Support\Binder\TagList; use FireflyIII\Support\Binder\TagList;
use FireflyIII\Support\Binder\TagOrId; use FireflyIII\Support\Binder\TagOrId;
use FireflyIII\TransactionRules\Actions\AddTag; use FireflyIII\TransactionRules\Actions\AddTag;
@ -403,7 +403,7 @@ return [
'cliToken' => CLIToken::class, 'cliToken' => CLIToken::class,
'tagOrId' => TagOrId::class, 'tagOrId' => TagOrId::class,
'dynamicConfigKey' => DynamicConfigKey::class, 'dynamicConfigKey' => DynamicConfigKey::class,
'staticConfigKey' => StaticConfigKey::class, 'eitherConfigKey' => EitherConfigKey::class,
], ],
'rule-actions' => [ 'rule-actions' => [

View File

@ -488,7 +488,29 @@ Route::group(
} }
); );
// Dynamic configuration API routes
Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => 'configuration',
'as' => 'api.v1.configuration.',],
static function () {
Route::get('', ['uses' => 'ConfigurationController@index', 'as' => 'index']);
Route::get('{eitherConfigKey}', ['uses' => 'ConfigurationController@show', 'as' => 'show']);
Route::put('{dynamicConfigKey}', ['uses' => 'ConfigurationController@update', 'as' => 'update']);
}
);
// STATIC CONFIGURATION (NOT CHANGEABLE)
// TODO VERIFY API DOCS
//Route::group(
// ['namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => 'configuration/static',
// 'as' => 'api.v1.configuration.static.',],
// static function () {
//
// // Configuration API routes:
// Route::get('', ['uses' => 'StaticConfigController@index', 'as' => 'index']);
// Route::get('{staticConfigKey}', ['uses' => 'StaticConfigController@show', 'as' => 'show']);
// }
//);
@ -539,32 +561,7 @@ Route::group(
// DYNAMIC CONFIGURATION (CHANGEABLE)
//// TODO VERIFY API DOCS
//Route::group(
// ['namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => 'configuration/dynamic',
// 'as' => 'api.v1.configuration.dynamic.',],
// static function () {
//
// // Configuration API routes:
// Route::get('', ['uses' => 'DynamicConfigController@index', 'as' => 'index']);
// Route::get('{dynamicConfigKey}', ['uses' => 'DynamicConfigController@show', 'as' => 'show']);
// Route::post('{dynamicConfigKey}', ['uses' => 'DynamicConfigController@update', 'as' => 'update']);
// }
//);
// STATIC CONFIGURATION (NOT CHANGEABLE)
// TODO VERIFY API DOCS
//Route::group(
// ['namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => 'configuration/static',
// 'as' => 'api.v1.configuration.static.',],
// static function () {
//
// // Configuration API routes:
// Route::get('', ['uses' => 'StaticConfigController@index', 'as' => 'index']);
// Route::get('{staticConfigKey}', ['uses' => 'StaticConfigController@show', 'as' => 'show']);
// }
//);