1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ConfigurationController.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Api\V1\Controllers; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Api\V1\Requests\ConfigurationRequest; |
27
|
|
|
use FireflyIII\Exceptions\FireflyException; |
28
|
|
|
use FireflyIII\Models\Configuration; |
29
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface; |
30
|
|
|
use FireflyIII\User; |
31
|
|
|
use Illuminate\Http\JsonResponse; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class ConfigurationController. |
35
|
|
|
* |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
*/ |
38
|
|
|
class ConfigurationController extends Controller |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** @var UserRepositoryInterface The user repository */ |
43
|
|
|
private $repository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ConfigurationController constructor. |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
parent::__construct(); |
52
|
|
|
$this->middleware( |
53
|
|
|
function ($request, $next) { |
54
|
|
|
/** @noinspection UnusedConstructorDependenciesInspection */ |
55
|
|
|
$this->repository = app(UserRepositoryInterface::class); |
56
|
|
|
/** @var User $admin */ |
57
|
|
|
$admin = auth()->user(); |
58
|
|
|
|
59
|
|
|
if (!$this->repository->hasRole($admin, 'owner')) { |
60
|
|
|
throw new FireflyException(trans('api.error_no_access')); // @codeCoverageIgnore |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $next($request); |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Show all configuration. |
70
|
|
|
* |
71
|
|
|
* @return JsonResponse |
72
|
|
|
*/ |
73
|
|
|
public function index(): JsonResponse |
74
|
|
|
{ |
75
|
|
|
$configData = $this->getConfigData(); |
76
|
|
|
|
77
|
|
|
return response()->json(['data' => $configData])->header('Content-Type', 'application/vnd.api+json'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Update the configuration. |
82
|
|
|
* |
83
|
|
|
* @param ConfigurationRequest $request |
84
|
|
|
* @param string $name |
85
|
|
|
* |
86
|
|
|
* @return JsonResponse |
87
|
|
|
*/ |
88
|
|
|
public function update(ConfigurationRequest $request, string $name): JsonResponse |
89
|
|
|
{ |
90
|
|
|
$data = $request->getAll(); |
91
|
|
|
app('fireflyconfig')->set($name, $data['value']); |
92
|
|
|
$configData = $this->getConfigData(); |
93
|
|
|
|
94
|
|
|
return response()->json(['data' => $configData])->header('Content-Type', 'application/vnd.api+json'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get all config values. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function getConfigData(): array |
103
|
|
|
{ |
104
|
|
|
/** @var Configuration $isDemoSite */ |
105
|
|
|
$isDemoSite = app('fireflyconfig')->get('is_demo_site'); |
106
|
|
|
/** @var Configuration $updateCheck */ |
107
|
|
|
$updateCheck = app('fireflyconfig')->get('permission_update_check'); |
108
|
|
|
/** @var Configuration $lastCheck */ |
109
|
|
|
$lastCheck = app('fireflyconfig')->get('last_update_check'); |
110
|
|
|
/** @var Configuration $singleUser */ |
111
|
|
|
$singleUser = app('fireflyconfig')->get('single_user_mode'); |
112
|
|
|
$data = [ |
113
|
|
|
'is_demo_site' => null === $isDemoSite ? null : $isDemoSite->data, |
114
|
|
|
'permission_update_check' => null === $updateCheck ? null : (int)$updateCheck->data, |
115
|
|
|
'last_update_check' => null === $lastCheck ? null : (int)$lastCheck->data, |
116
|
|
|
'single_user_mode' => null === $singleUser ? null : $singleUser->data, |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
return $data; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|