2FAuth/app/Api/v1/Controllers/SettingController.php

140 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Api\v1\Controllers;
use App\Exceptions\DbEncryptionException;
use App\Services\DbEncryptionService;
2021-12-01 13:47:20 +01:00
use App\Services\SettingService;
use App\Api\v1\Requests\SettingStoreRequest;
use App\Api\v1\Requests\SettingUpdateRequest;
use App\Http\Controllers\Controller;
class SettingController extends Controller
{
/**
* The Settings Service instance.
*/
2021-12-01 13:47:20 +01:00
protected SettingService $settingService;
/**
* Create a new controller instance.
*/
2021-12-01 13:47:20 +01:00
public function __construct(SettingService $settingService)
{
2021-12-01 13:47:20 +01:00
$this->settingService = $settingService;
}
/**
* List all settings
*
2021-11-26 11:18:58 +01:00
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$settings = $this->settingService->all();
$settingsResources = collect();
$settings->each(function ($item, $key) use ($settingsResources) {
$settingsResources->push([
2021-10-03 11:35:09 +02:00
'key' => $key,
'value' => $item
]);
});
// return SettingResource::collection($tata);
return response()->json($settingsResources->all(), 200);
}
/**
2021-11-26 11:18:58 +01:00
* Display a setting
*
2021-10-03 11:35:09 +02:00
* @param string $settingName
2021-11-26 11:18:58 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2021-10-03 11:35:09 +02:00
public function show($settingName)
{
2021-10-03 11:35:09 +02:00
$setting = $this->settingService->get($settingName);
2021-11-22 01:12:37 +01:00
if (is_null($setting)) {
abort(404);
}
return response()->json([
2021-10-03 11:35:09 +02:00
'key' => $settingName,
'value' => $setting
], 200);
}
/**
2021-11-26 11:18:58 +01:00
* Store a setting
*
* @param \App\Api\v1\Requests\SettingStoreRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(SettingStoreRequest $request)
{
$validated = $request->validated();
2021-10-03 11:35:09 +02:00
$this->settingService->set($validated['key'], $validated['value']);
return response()->json([
2021-10-03 11:35:09 +02:00
'key' => $validated['key'],
'value' => $validated['value']
], 201);
}
/**
2021-11-26 11:18:58 +01:00
* Update a setting
*
* @param \App\Api\v1\Requests\SettingUpdateRequest $request
* @return \Illuminate\Http\JsonResponse
*/
2021-10-03 11:35:09 +02:00
public function update(SettingUpdateRequest $request, $settingName)
{
$validated = $request->validated();
2021-11-26 11:21:57 +01:00
$this->settingService->set($settingName, $validated['value']);
return response()->json([
2021-10-03 11:35:09 +02:00
'key' => $settingName,
'value' => $validated['value']
], 200);
}
/**
2021-11-26 11:18:58 +01:00
* Delete a setting
*
* @param \App\Api\v1\Requests\SettingUpdateRequest $request
* @return \Illuminate\Http\JsonResponse
*/
2021-10-03 11:35:09 +02:00
public function destroy($settingName)
{
2021-10-03 11:35:09 +02:00
$setting = $this->settingService->get($settingName);
if (is_null($setting)) {
abort(404);
}
2021-10-08 23:27:15 +02:00
$optionsConfig = config('2fauth.options');
2021-10-03 11:35:09 +02:00
if(array_key_exists($settingName, $optionsConfig)) {
return response()->json(
['message' => 'bad request',
'reason' => [__('errors.delete_user_setting_only')]
], 400);
}
2021-10-03 11:35:09 +02:00
$this->settingService->delete($settingName);
return response()->json(null, 204);
}
}