| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  | /* | 
					
						
							|  |  |  |  * ConfigurationController.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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  | namespace FireflyIII\Api\V1\Controllers\System; | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Requests\System\UpdateRequest; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							|  |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Support\Binder\EitherConfigKey; | 
					
						
							|  |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class ConfigurationController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ConfigurationController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private UserRepositoryInterface $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * ConfigurationController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 $this->repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-19 17:22:16 +02:00
										 |  |  |      * This endpoint is documented at: | 
					
						
							| 
									
										
										
										
											2023-02-12 06:53:36 +01:00
										 |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/configuration/getConfiguration
 | 
					
						
							| 
									
										
										
										
											2021-09-19 17:22:16 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function index(): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $dynamicData = $this->getDynamicConfiguration(); | 
					
						
							|  |  |  |         } catch (FireflyException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |             app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |             throw new FireflyException('200030: Could not load config variables.', 0, $e); | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $staticData = $this->getStaticConfiguration(); | 
					
						
							|  |  |  |         $return     = []; | 
					
						
							|  |  |  |         foreach ($dynamicData as $key => $value) { | 
					
						
							|  |  |  |             $return[] = [ | 
					
						
							|  |  |  |                 'title'    => sprintf('configuration.%s', $key), | 
					
						
							|  |  |  |                 'value'    => $value, | 
					
						
							|  |  |  |                 'editable' => true, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         foreach ($staticData as $key => $value) { | 
					
						
							|  |  |  |             $return[] = [ | 
					
						
							|  |  |  |                 'title'    => $key, | 
					
						
							|  |  |  |                 'value'    => $value, | 
					
						
							|  |  |  |                 'editable' => false, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($return); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all config values. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getDynamicConfiguration(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $isDemoSite  = app('fireflyconfig')->get('is_demo_site'); | 
					
						
							|  |  |  |         $updateCheck = app('fireflyconfig')->get('permission_update_check'); | 
					
						
							|  |  |  |         $lastCheck   = app('fireflyconfig')->get('last_update_check'); | 
					
						
							|  |  |  |         $singleUser  = app('fireflyconfig')->get('single_user_mode'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'is_demo_site'            => $isDemoSite?->data, | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'permission_update_check' => null === $updateCheck ? null : (int) $updateCheck->data, | 
					
						
							|  |  |  |             'last_update_check'       => null === $lastCheck ? null : (int) $lastCheck->data, | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'single_user_mode'        => $singleUser?->data, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getStaticConfiguration(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $list   = EitherConfigKey::$static; | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         foreach ($list as $key) { | 
					
						
							|  |  |  |             $return[$key] = config($key); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-19 17:22:16 +02:00
										 |  |  |      * This endpoint is documented at: | 
					
						
							| 
									
										
										
										
											2023-02-12 06:53:36 +01:00
										 |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/configuration/getSingleConfiguration
 | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function show(string $configKey): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data     = []; | 
					
						
							|  |  |  |         $dynamic  = $this->getDynamicConfiguration(); | 
					
						
							|  |  |  |         $shortKey = str_replace('configuration.', '', $configKey); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |         if (str_starts_with($configKey, 'configuration.')) { | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |             $data = [ | 
					
						
							|  |  |  |                 'title'    => $configKey, | 
					
						
							|  |  |  |                 'value'    => $dynamic[$shortKey], | 
					
						
							|  |  |  |                 'editable' => true, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |         if (!str_starts_with($configKey, 'configuration.')) { | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |             $data = [ | 
					
						
							|  |  |  |                 'title'    => $configKey, | 
					
						
							|  |  |  |                 'value'    => config($configKey), | 
					
						
							|  |  |  |                 'editable' => false, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json(['data' => $data])->header('Content-Type', self::CONTENT_TYPE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-19 17:22:16 +02:00
										 |  |  |      * This endpoint is documented at: | 
					
						
							| 
									
										
										
										
											2023-02-12 06:53:36 +01:00
										 |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/configuration/setConfiguration
 | 
					
						
							| 
									
										
										
										
											2021-09-19 17:22:16 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |      * Update the configuration. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function update(UpdateRequest $request, string $name): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $rules     = ['value' => 'required']; | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |         if (!$this->repository->hasRole(auth()->user(), 'owner')) { | 
					
						
							| 
									
										
										
										
											2023-01-29 07:10:11 +01:00
										 |  |  |             $messages = ['value' => '200005: You need the "owner" role to do this.']; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             \Validator::make([], $rules, $messages)->validate(); | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $data      = $request->getAll(); | 
					
						
							|  |  |  |         $shortName = str_replace('configuration.', '', $name); | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app('fireflyconfig')->set($shortName, $data['value']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get updated config:
 | 
					
						
							|  |  |  |         $newConfig = $this->getDynamicConfiguration(); | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  |         $data      = [ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             'title'    => $name, | 
					
						
							|  |  |  |             'value'    => $newConfig[$shortName], | 
					
						
							|  |  |  |             'editable' => true, | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-03-07 07:43:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return response()->json(['data' => $data])->header('Content-Type', self::CONTENT_TYPE); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  | } |