2021-09-26 22:06:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-10-11 23:11:52 +02:00
|
|
|
use App\Exceptions\DbEncryptionException;
|
|
|
|
use App\Services\DbEncryptionService;
|
|
|
|
use App\Services\SettingServiceInterface;
|
2021-09-26 22:06:49 +02:00
|
|
|
use App\Http\Requests\SettingStoreRequest;
|
|
|
|
use App\Http\Requests\SettingUpdateRequest;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
|
|
|
|
|
|
class SettingController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Settings Service instance.
|
|
|
|
*/
|
|
|
|
protected SettingServiceInterface $settingService;
|
|
|
|
|
2021-10-11 23:11:52 +02:00
|
|
|
/**
|
|
|
|
* The Settings Service instance.
|
|
|
|
*/
|
|
|
|
protected DbEncryptionService $dbEncryptionService;
|
|
|
|
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
*/
|
2021-10-11 23:11:52 +02:00
|
|
|
public function __construct(SettingServiceInterface $SettingServiceInterface, DbEncryptionService $dbEncryptionService)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
|
|
|
$this->settingService = $SettingServiceInterface;
|
2021-10-11 23:11:52 +02:00
|
|
|
$this->dbEncryptionService = $dbEncryptionService;
|
2021-09-26 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all settings
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
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
|
2021-09-26 22:06:49 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return SettingResource::collection($tata);
|
|
|
|
return response()->json($settingsResources->all(), 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a resource
|
|
|
|
*
|
2021-10-03 11:35:09 +02:00
|
|
|
* @param string $settingName
|
2021-09-26 22:06:49 +02:00
|
|
|
*
|
|
|
|
* @return \App\Http\Resources\TwoFAccountReadResource
|
|
|
|
*/
|
2021-10-03 11:35:09 +02:00
|
|
|
public function show($settingName)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
2021-10-03 11:35:09 +02:00
|
|
|
$setting = $this->settingService->get($settingName);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
if (!$setting) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
2021-10-03 11:35:09 +02:00
|
|
|
'key' => $settingName,
|
|
|
|
'value' => $setting
|
2021-09-26 22:06:49 +02:00
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save options
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
|
|
|
public function store(SettingStoreRequest $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
2021-10-03 11:35:09 +02:00
|
|
|
$this->settingService->set($validated['key'], $validated['value']);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return response()->json([
|
2021-10-03 11:35:09 +02:00
|
|
|
'key' => $validated['key'],
|
|
|
|
'value' => $validated['value']
|
2021-09-26 22:06:49 +02:00
|
|
|
], 201);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save options
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
2021-10-03 11:35:09 +02:00
|
|
|
public function update(SettingUpdateRequest $request, $settingName)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
2021-10-11 23:11:52 +02:00
|
|
|
// The useEncryption setting impacts records in DB so we delegate the work to the
|
|
|
|
// dedicated db encryption service
|
|
|
|
if( $settingName === 'useEncryption')
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->dbEncryptionService->setTo($validated['value']);
|
|
|
|
}
|
|
|
|
catch(DbEncryptionException $ex) {
|
|
|
|
return response()->json([
|
|
|
|
'message' => $ex->getMessage()
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else $this->settingService->set($settingName, $validated['value']);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return response()->json([
|
2021-10-03 11:35:09 +02:00
|
|
|
'key' => $settingName,
|
|
|
|
'value' => $validated['value']
|
2021-09-26 22:06:49 +02:00
|
|
|
], 200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save options
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
2021-10-03 11:35:09 +02:00
|
|
|
public function destroy($settingName)
|
2021-09-26 22:06:49 +02:00
|
|
|
{
|
2021-10-03 11:35:09 +02:00
|
|
|
$setting = $this->settingService->get($settingName);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
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)) {
|
2021-09-26 22:06:49 +02:00
|
|
|
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);
|
2021-09-26 22:06:49 +02:00
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|