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
* and some previous / next periods.
*
* TODO unused and undocumented.
*
* @return JsonResponse
*/
public function dateRanges(): JsonResponse

View File

@@ -1,8 +1,8 @@
<?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).
*
@@ -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;

View File

@@ -1,7 +1,7 @@
<?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).
*
@@ -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 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();

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\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),

View File

@@ -1,7 +1,7 @@
<?php
/**
* ConfigurationName.php
* Copyright (c) 2019 james@firefly-iii.org
/*
* DynamicConfigKey.php
* Copyright (c) 2021 james@firefly-iii.org
*
* 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;
}
}
}

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;
}
}