diff --git a/app/Api/V1/Controllers/Preferences/IndexController.php b/app/Api/V1/Controllers/Preferences/IndexController.php index 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(): JsonResponse diff --git a/app/Api/V1/Controllers/AboutController.php b/app/Api/V1/Controllers/System/AboutController.php similarity index 94% rename from app/Api/V1/Controllers/AboutController.php rename to app/Api/V1/Controllers/System/AboutController.php index 2646e30867..aec3865e3d 100644 --- a/app/Api/V1/Controllers/AboutController.php +++ b/app/Api/V1/Controllers/System/AboutController.php @@ -1,8 +1,8 @@ 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 Controller 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. @@ -92,8 +96,8 @@ class ConfigurationController extends Controller 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, + '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.php new file mode 100644 index 0000000000..7361697e8f --- /dev/null +++ b/app/Api/V1/Controllers/System/StaticConfigController.php @@ -0,0 +1,74 @@ +. + */ + +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.php index 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 FormRequest public 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.php similarity index 88% rename from app/Support/Binder/ConfigurationName.php rename to app/Support/Binder/DynamicConfigKey.php index 379c50c6e3..07b105eda1 100644 --- a/app/Support/Binder/ConfigurationName.php +++ b/app/Support/Binder/DynamicConfigKey.php @@ -1,7 +1,7 @@ . */ -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 file diff --git a/app/Support/Binder/StaticConfigKey.php b/app/Support/Binder/StaticConfigKey.php new file mode 100644 index 0000000000..761769449e --- /dev/null +++ b/app/Support/Binder/StaticConfigKey.php @@ -0,0 +1,52 @@ +. + */ + +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 file diff --git a/config/firefly.php b/config/firefly.php index 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.php index 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 DOCS Route::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',