Refactor some API routes.

This commit is contained in:
James Cole
2021-02-17 06:17:48 +01:00
parent 25e4d78119
commit e04245de96
9 changed files with 206 additions and 47 deletions

View File

@@ -41,6 +41,8 @@ class IndexController extends Controller
* Return users preferred date range settings, the current period * Return users preferred date range settings, the current period
* and some previous / next periods. * and some previous / next periods.
* *
* TODO unused and undocumented.
*
* @return JsonResponse * @return JsonResponse
*/ */
public function dateRanges(): JsonResponse public function dateRanges(): JsonResponse

View File

@@ -1,8 +1,8 @@
<?php <?php
/** /*
* AboutController.php * AboutController.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2021 james@firefly-iii.org
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *
@@ -22,9 +22,10 @@
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers; namespace FireflyIII\Api\V1\Controllers\System;
use DB; use DB;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Transformers\UserTransformer; use FireflyIII\Transformers\UserTransformer;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /*
* ConfigurationController.php * ConfigurationController.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2021 james@firefly-iii.org
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *
@@ -21,25 +21,23 @@
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers; namespace FireflyIII\Api\V1\Controllers\System;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\ConfigurationRequest; use FireflyIII\Api\V1\Requests\ConfigurationRequest;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Configuration; use FireflyIII\Models\Configuration;
use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
/** /**
* Class ConfigurationController. * Class DynamicConfigController.
* *
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
class ConfigurationController extends Controller class DynamicConfigController extends Controller
{ {
/** @var UserRepositoryInterface The user repository */ private UserRepositoryInterface $repository;
private $repository;
/** /**
* ConfigurationController constructor. * ConfigurationController constructor.
@@ -50,12 +48,6 @@ class ConfigurationController extends Controller
$this->middleware( $this->middleware(
function ($request, $next) { function ($request, $next) {
$this->repository = app(UserRepositoryInterface::class); $this->repository = app(UserRepositoryInterface::class);
/** @var User $admin */
$admin = auth()->user();
if (!$this->repository->hasRole($admin, 'owner')) {
throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore
}
return $next($request); return $next($request);
} }
@@ -73,6 +65,18 @@ class ConfigurationController extends Controller
return response()->json(['data' => $configData])->header('Content-Type', self::CONTENT_TYPE); return response()->json(['data' => $configData])->header('Content-Type', self::CONTENT_TYPE);
} }
/**
* 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);
}
/** /**
* Get all config values. * Get all config values.
@@ -92,8 +96,8 @@ class ConfigurationController extends Controller
return [ return [
'is_demo_site' => null === $isDemoSite ? null : $isDemoSite->data, 'is_demo_site' => null === $isDemoSite ? null : $isDemoSite->data,
'permission_update_check' => null === $updateCheck ? null : (int) $updateCheck->data, 'permission_update_check' => null === $updateCheck ? null : (int)$updateCheck->data,
'last_update_check' => null === $lastCheck ? null : (int) $lastCheck->data, 'last_update_check' => null === $lastCheck ? null : (int)$lastCheck->data,
'single_user_mode' => null === $singleUser ? null : $singleUser->data, 'single_user_mode' => null === $singleUser ? null : $singleUser->data,
]; ];
} }
@@ -108,6 +112,9 @@ class ConfigurationController extends Controller
*/ */
public function update(ConfigurationRequest $request, string $name): JsonResponse public function update(ConfigurationRequest $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(); $data = $request->getAll();
app('fireflyconfig')->set($name, $data['value']); app('fireflyconfig')->set($name, $data['value']);
$configData = $this->getConfigData(); $configData = $this->getConfigData();

View File

@@ -0,0 +1,74 @@
<?php
/*
* EnvController.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\Support\Binder\StaticConfigKey;
use Illuminate\Http\JsonResponse;
/**
* Class StaticConfigController
*
* Show specific Firefly III configuration and/or ENV vars.
*/
class StaticConfigController extends Controller
{
private array $list;
/**
* EnvController constructor.
*/
public function __construct()
{
parent::__construct();
$this->list = StaticConfigKey::$accepted;
}
/**
* Show all available env variables.
*
* @return JsonResponse
*/
public function index(): JsonResponse
{
$vars = [];
// show all Firefly III config vars.
foreach ($this->list as $key) {
$vars[$key] = config($key);
}
return response()->json($vars);
}
/**
* @param string $staticKey
*
* @return JsonResponse
*/
public function show(string $staticKey): JsonResponse
{
$response = [$staticKey => config($staticKey)];
return response()->json($response);
}
}

View File

@@ -28,6 +28,7 @@ use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean; use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsDateOrTime; use FireflyIII\Rules\IsDateOrTime;
use FireflyIII\Support\NullArrayObject; use FireflyIII\Support\NullArrayObject;
use FireflyIII\Support\Request\AppendsLocationData;
use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Validation\CurrencyValidation; use FireflyIII\Validation\CurrencyValidation;
@@ -42,7 +43,7 @@ use Log;
*/ */
class TransactionStoreRequest extends FormRequest class TransactionStoreRequest extends FormRequest
{ {
use TransactionValidation, GroupValidation, CurrencyValidation, ConvertsDataTypes, ChecksLogin; use TransactionValidation, GroupValidation, CurrencyValidation, ConvertsDataTypes, ChecksLogin, AppendsLocationData;
/** /**
* Get all data. Is pretty complex because of all the ??-statements. * Get all data. Is pretty complex because of all the ??-statements.
@@ -52,8 +53,7 @@ class TransactionStoreRequest extends FormRequest
public function getAll(): array public function getAll(): array
{ {
Log::debug('get all data in TransactionStoreRequest'); Log::debug('get all data in TransactionStoreRequest');
$data = [
return [
'group_title' => $this->string('group_title'), 'group_title' => $this->string('group_title'),
'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'), 'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'),
'apply_rules' => $this->boolean('apply_rules', true), 'apply_rules' => $this->boolean('apply_rules', true),

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /*
* ConfigurationName.php * DynamicConfigKey.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2021 james@firefly-iii.org
* *
* This file is part of Firefly III (https://github.com/firefly-iii). * This file is part of Firefly III (https://github.com/firefly-iii).
* *
@@ -19,20 +19,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1);
namespace FireflyIII\Support\Binder; namespace FireflyIII\Support\Binder;
use Illuminate\Routing\Route; use Illuminate\Routing\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Class ConfigurationName * Class DynamicConfigKey
*/ */
class ConfigurationName implements BinderInterface class DynamicConfigKey
{ {
/** /**
* @param string $value * @param string $value
* @param Route $route * @param Route $route
@@ -48,4 +44,5 @@ class ConfigurationName implements BinderInterface
} }
throw new NotFoundHttpException; throw new NotFoundHttpException;
} }
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* StaticConfigKey.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\Support\Binder;
use Illuminate\Routing\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class StaticConfigKey
*/
class StaticConfigKey
{
public static array $accepted = [
'firefly.version',
'firefly.api_version',
'firefly.default_location'
];
/**
* @param string $value
* @param Route $route
*
* @return string
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public static function routeBinder(string $value, Route $route): string
{
if (in_array($value, self::$accepted, true)) {
return $value;
}
throw new NotFoundHttpException;
}
}

View File

@@ -49,10 +49,11 @@ use FireflyIII\Support\Binder\AccountList;
use FireflyIII\Support\Binder\BudgetList; use FireflyIII\Support\Binder\BudgetList;
use FireflyIII\Support\Binder\CategoryList; use FireflyIII\Support\Binder\CategoryList;
use FireflyIII\Support\Binder\CLIToken; use FireflyIII\Support\Binder\CLIToken;
use FireflyIII\Support\Binder\ConfigurationName;
use FireflyIII\Support\Binder\CurrencyCode; use FireflyIII\Support\Binder\CurrencyCode;
use FireflyIII\Support\Binder\Date; use FireflyIII\Support\Binder\Date;
use FireflyIII\Support\Binder\DynamicConfigKey;
use FireflyIII\Support\Binder\JournalList; use FireflyIII\Support\Binder\JournalList;
use FireflyIII\Support\Binder\StaticConfigKey;
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;
@@ -401,7 +402,8 @@ return [
'toCurrencyCode' => CurrencyCode::class, 'toCurrencyCode' => CurrencyCode::class,
'cliToken' => CLIToken::class, 'cliToken' => CLIToken::class,
'tagOrId' => TagOrId::class, 'tagOrId' => TagOrId::class,
'configName' => ConfigurationName::class, 'dynamicConfigKey' => DynamicConfigKey::class,
'staticConfigKey' => StaticConfigKey::class,
], ],
'rule-actions' => [ 'rule-actions' => [

View File

@@ -25,10 +25,15 @@ declare(strict_types=1);
use FireflyIII\Http\Middleware\IsAdmin; use FireflyIII\Http\Middleware\IsAdmin;
/**
* System and configuration controllers
*/
// ABOUT FIREFLY III
// TODO VERIFY API DOCS
Route::group( Route::group(
[ [
'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'about', 'namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => 'about',
'as' => 'api.v1.about.'], 'as' => 'api.v1.about.'],
static function () { static function () {
@@ -38,6 +43,36 @@ 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']);
}
);
Route::group( Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'accounts', ['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'accounts',
'as' => 'api.v1.accounts.',], 'as' => 'api.v1.accounts.',],
@@ -240,17 +275,6 @@ Route::group(
} }
); );
// Configuration
Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'configuration',
'as' => 'api.v1.configuration.',],
static function () {
// Configuration API routes:
Route::get('', ['uses' => 'ConfigurationController@index', 'as' => 'index']);
Route::post('{configName}', ['uses' => 'ConfigurationController@update', 'as' => 'update']);
}
);
Route::group( Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'cer', ['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'cer',