2021-09-26 22:06:49 +02:00
|
|
|
<?php
|
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
namespace App\Api\v1\Controllers;
|
2021-09-26 22:06:49 +02:00
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Requests\SettingStoreRequest;
|
|
|
|
use App\Api\v1\Requests\SettingUpdateRequest;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Settings;
|
2021-09-26 22:06:49 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
|
|
class SettingController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List all settings
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-26 22:06:49 +02:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
$settings = Settings::all();
|
2022-11-21 11:16:43 +01:00
|
|
|
$settingsResources = collect([]);
|
2022-09-07 17:54:27 +02:00
|
|
|
$settings->each(function (mixed $item, string $key) use ($settingsResources) {
|
2021-09-26 22:06:49 +02:00
|
|
|
$settingsResources->push([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => $key,
|
|
|
|
'value' => $item,
|
2021-09-26 22:06:49 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return response()->json($settingsResources->all(), 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Display a setting
|
2021-09-26 22:06:49 +02:00
|
|
|
*
|
2022-11-22 15:15:52 +01:00
|
|
|
* @param string $settingName
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-26 22:06:49 +02:00
|
|
|
*/
|
2021-10-03 11:35:09 +02:00
|
|
|
public function show($settingName)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
$setting = Settings::get($settingName);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
2021-11-22 01:12:37 +01:00
|
|
|
if (is_null($setting)) {
|
2021-09-26 22:06:49 +02:00
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => $settingName,
|
|
|
|
'value' => $setting,
|
2021-09-26 22:06:49 +02:00
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Store a setting
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
|
|
|
* @param \App\Api\v1\Requests\SettingStoreRequest $request
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-26 22:06:49 +02:00
|
|
|
*/
|
|
|
|
public function store(SettingStoreRequest $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set($validated['key'], $validated['value']);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return response()->json([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => $validated['key'],
|
|
|
|
'value' => $validated['value'],
|
2021-09-26 22:06:49 +02:00
|
|
|
], 201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Update a setting
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
|
|
|
* @param \App\Api\v1\Requests\SettingUpdateRequest $request
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-26 22:06:49 +02:00
|
|
|
*/
|
2022-08-26 15:57:18 +02:00
|
|
|
public function update(SettingUpdateRequest $request, string $settingName)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set($settingName, $validated['value']);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return response()->json([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => $settingName,
|
|
|
|
'value' => $validated['value'],
|
2021-09-26 22:06:49 +02:00
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Delete a setting
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
|
|
|
* @param string $settingName
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-26 22:06:49 +02:00
|
|
|
*/
|
2022-08-26 15:57:18 +02:00
|
|
|
public function destroy(string $settingName)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
$setting = Settings::get($settingName);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
if (is_null($setting)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
2023-02-17 17:12:53 +01:00
|
|
|
$appSettings = config('2fauth.settings');
|
|
|
|
if (array_key_exists($settingName, $appSettings)) {
|
2021-09-26 22:06:49 +02:00
|
|
|
return response()->json(
|
2022-11-22 15:15:52 +01:00
|
|
|
['message' => 'bad request',
|
|
|
|
'reason' => [__('errors.delete_user_setting_only')],
|
|
|
|
], 400);
|
2021-09-26 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::delete($settingName);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
|
|
|
}
|