Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Refactor some API routes.
James Colee04245de968b
diff --git a/app/Api/V1/Controllers/Preferences/IndexController.php b/app/Api/V1/Controllers/Preferences/IndexController.phpindex 04d2867a3c..0562e4b382 100644--- a/app/Api/V1/Controllers/Preferences/IndexController.php+++ b/app/Api/V1/Controllers/Preferences/IndexController.php@@ -41,6 +41,8 @@ class IndexController extends Controller* Return users preferred date range settings, the current period* and some previous / next periods.*+ * TODO unused and undocumented.+ ** @return JsonResponse*/public function dateRanges(): JsonResponsediff --git a/app/Api/V1/Controllers/AboutController.php b/app/Api/V1/Controllers/System/AboutController.phpsimilarity index 94%rename from app/Api/V1/Controllers/AboutController.phprename to app/Api/V1/Controllers/System/AboutController.phpindex 2646e30867..aec3865e3d 100644--- a/app/Api/V1/Controllers/AboutController.php+++ b/app/Api/V1/Controllers/System/AboutController.php@@ -1,8 +1,8 @@<?php-/**+/** AboutController.php- * Copyright (c) 2019 [email protected]+ * Copyright (c) 2021 [email protected]** This file is part of Firefly III (https://github.com/firefly-iii).*@@ -22,9 +22,10 @@declare(strict_types=1);-namespace FireflyIII\Api\V1\Controllers;+namespace FireflyIII\Api\V1\Controllers\System;use DB;+use FireflyIII\Api\V1\Controllers\Controller;use FireflyIII\Transformers\UserTransformer;use Illuminate\Http\JsonResponse;use League\Fractal\Resource\Item;diff --git a/app/Api/V1/Controllers/ConfigurationController.php b/app/Api/V1/Controllers/System/DynamicConfigController.phpsimilarity index 78%rename from app/Api/V1/Controllers/ConfigurationController.phprename to app/Api/V1/Controllers/System/DynamicConfigController.phpindex efc79a3615..a4ad055ce0 100644--- a/app/Api/V1/Controllers/ConfigurationController.php+++ b/app/Api/V1/Controllers/System/DynamicConfigController.php@@ -1,7 +1,7 @@<?php-/**+/** ConfigurationController.php- * Copyright (c) 2019 [email protected]+ * Copyright (c) 2021 [email protected]** This file is part of Firefly III (https://github.com/firefly-iii).*@@ -21,25 +21,23 @@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\Exceptions\FireflyException;use FireflyIII\Models\Configuration;use FireflyIII\Repositories\User\UserRepositoryInterface;-use FireflyIII\User;use Illuminate\Http\JsonResponse;/**- * Class ConfigurationController.+ * Class DynamicConfigController.** @codeCoverageIgnore*/-class ConfigurationController extends Controller+class DynamicConfigController extends Controller{- /** @var UserRepositoryInterface The user repository */- private $repository;-+ private UserRepositoryInterface $repository;/*** ConfigurationController constructor.@@ -50,12 +48,6 @@ class ConfigurationController extends Controller$this->middleware(function ($request, $next) {$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);}@@ -73,6 +65,18 @@ class ConfigurationController extends Controllerreturn 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.@@ -92,8 +96,8 @@ class ConfigurationController extends Controllerreturn ['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,+ '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,];}@@ -108,6 +112,9 @@ class ConfigurationController extends Controller*/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();app('fireflyconfig')->set($name, $data['value']);$configData = $this->getConfigData();diff --git a/app/Api/V1/Controllers/System/StaticConfigController.php b/app/Api/V1/Controllers/System/StaticConfigController.phpnew file mode 100644index 0000000000..7361697e8f--- /dev/null+++ b/app/Api/V1/Controllers/System/StaticConfigController.php@@ -0,0 +1,74 @@+<?php+/*+ * EnvController.php+ * Copyright (c) 2021 [email protected]+ *+ * 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);+ }+}diff --git a/app/Api/V1/Requests/TransactionStoreRequest.php b/app/Api/V1/Requests/TransactionStoreRequest.phpindex dc029afe3f..208f229a02 100644--- a/app/Api/V1/Requests/TransactionStoreRequest.php+++ b/app/Api/V1/Requests/TransactionStoreRequest.php@@ -28,6 +28,7 @@ use FireflyIII\Rules\BelongsUser;use FireflyIII\Rules\IsBoolean;use FireflyIII\Rules\IsDateOrTime;use FireflyIII\Support\NullArrayObject;+use FireflyIII\Support\Request\AppendsLocationData;use FireflyIII\Support\Request\ChecksLogin;use FireflyIII\Support\Request\ConvertsDataTypes;use FireflyIII\Validation\CurrencyValidation;@@ -42,7 +43,7 @@ use Log;*/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.@@ -52,8 +53,7 @@ class TransactionStoreRequest extends FormRequestpublic function getAll(): array{Log::debug('get all data in TransactionStoreRequest');-- return [+ $data = ['group_title' => $this->string('group_title'),'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'),'apply_rules' => $this->boolean('apply_rules', true),diff --git a/app/Support/Binder/ConfigurationName.php b/app/Support/Binder/DynamicConfigKey.phpsimilarity index 88%rename from app/Support/Binder/ConfigurationName.phprename to app/Support/Binder/DynamicConfigKey.phpindex 379c50c6e3..07b105eda1 100644--- a/app/Support/Binder/ConfigurationName.php+++ b/app/Support/Binder/DynamicConfigKey.php@@ -1,7 +1,7 @@<?php-/**- * ConfigurationName.php- * Copyright (c) 2019 [email protected]+/*+ * DynamicConfigKey.php+ * Copyright (c) 2021 [email protected]** 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/>.*/-declare(strict_types=1);-namespace FireflyIII\Support\Binder;-use Illuminate\Routing\Route;use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;/**- * Class ConfigurationName+ * Class DynamicConfigKey*/-class ConfigurationName implements BinderInterface+class DynamicConfigKey{-/*** @param string $value* @param Route $route@@ -48,4 +44,5 @@ class ConfigurationName implements BinderInterface}throw new NotFoundHttpException;}-}++}\ No newline at end of filediff --git a/app/Support/Binder/StaticConfigKey.php b/app/Support/Binder/StaticConfigKey.phpnew file mode 100644index 0000000000..761769449e--- /dev/null+++ b/app/Support/Binder/StaticConfigKey.php@@ -0,0 +1,52 @@+<?php+/*+ * StaticConfigKey.php+ * Copyright (c) 2021 [email protected]+ *+ * 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;+ }+}\ No newline at end of filediff --git a/config/firefly.php b/config/firefly.phpindex 1f55e96adf..2cd144b5c7 100644--- a/config/firefly.php+++ b/config/firefly.php@@ -49,10 +49,11 @@ use FireflyIII\Support\Binder\AccountList;use FireflyIII\Support\Binder\BudgetList;use FireflyIII\Support\Binder\CategoryList;use FireflyIII\Support\Binder\CLIToken;-use FireflyIII\Support\Binder\ConfigurationName;use FireflyIII\Support\Binder\CurrencyCode;use FireflyIII\Support\Binder\Date;+use FireflyIII\Support\Binder\DynamicConfigKey;use FireflyIII\Support\Binder\JournalList;+use FireflyIII\Support\Binder\StaticConfigKey;use FireflyIII\Support\Binder\TagList;use FireflyIII\Support\Binder\TagOrId;use FireflyIII\TransactionRules\Actions\AddTag;@@ -401,7 +402,8 @@ return ['toCurrencyCode' => CurrencyCode::class,'cliToken' => CLIToken::class,'tagOrId' => TagOrId::class,- 'configName' => ConfigurationName::class,+ 'dynamicConfigKey' => DynamicConfigKey::class,+ 'staticConfigKey' => StaticConfigKey::class,],'rule-actions' => [diff --git a/routes/api.php b/routes/api.phpindex 471e648678..c1b10c62d0 100644--- a/routes/api.php+++ b/routes/api.php@@ -25,10 +25,15 @@ declare(strict_types=1);use FireflyIII\Http\Middleware\IsAdmin;+/**+ * System and configuration controllers+ */+// ABOUT FIREFLY III+// TODO VERIFY API DOCSRoute::group([- 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'about',+ 'namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => 'about','as' => 'api.v1.about.'],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(['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => '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(['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'cer',