SettingController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\DbEncryptionException;
  4. use App\Services\DbEncryptionService;
  5. use App\Services\SettingServiceInterface;
  6. use App\Http\Requests\SettingStoreRequest;
  7. use App\Http\Requests\SettingUpdateRequest;
  8. use App\Http\Controllers\Controller;
  9. class SettingController extends Controller
  10. {
  11. /**
  12. * The Settings Service instance.
  13. */
  14. protected SettingServiceInterface $settingService;
  15. /**
  16. * The Settings Service instance.
  17. */
  18. protected DbEncryptionService $dbEncryptionService;
  19. /**
  20. * Create a new controller instance.
  21. *
  22. */
  23. public function __construct(SettingServiceInterface $SettingServiceInterface, DbEncryptionService $dbEncryptionService)
  24. {
  25. $this->settingService = $SettingServiceInterface;
  26. $this->dbEncryptionService = $dbEncryptionService;
  27. }
  28. /**
  29. * List all settings
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function index()
  34. {
  35. $settings = $this->settingService->all();
  36. $settingsResources = collect();
  37. $settings->each(function ($item, $key) use ($settingsResources) {
  38. $settingsResources->push([
  39. 'key' => $key,
  40. 'value' => $item
  41. ]);
  42. });
  43. // return SettingResource::collection($tata);
  44. return response()->json($settingsResources->all(), 200);
  45. }
  46. /**
  47. * Display a resource
  48. *
  49. * @param string $settingName
  50. *
  51. * @return \App\Http\Resources\TwoFAccountReadResource
  52. */
  53. public function show($settingName)
  54. {
  55. $setting = $this->settingService->get($settingName);
  56. if (!$setting) {
  57. abort(404);
  58. }
  59. return response()->json([
  60. 'key' => $settingName,
  61. 'value' => $setting
  62. ], 200);
  63. }
  64. /**
  65. * Save options
  66. * @return [type] [description]
  67. */
  68. public function store(SettingStoreRequest $request)
  69. {
  70. $validated = $request->validated();
  71. $this->settingService->set($validated['key'], $validated['value']);
  72. return response()->json([
  73. 'key' => $validated['key'],
  74. 'value' => $validated['value']
  75. ], 201);
  76. }
  77. /**
  78. * Save options
  79. * @return [type] [description]
  80. */
  81. public function update(SettingUpdateRequest $request, $settingName)
  82. {
  83. $validated = $request->validated();
  84. // The useEncryption setting impacts records in DB so we delegate the work to the
  85. // dedicated db encryption service
  86. if( $settingName === 'useEncryption')
  87. {
  88. try {
  89. $this->dbEncryptionService->setTo($validated['value']);
  90. }
  91. catch(DbEncryptionException $ex) {
  92. return response()->json([
  93. 'message' => $ex->getMessage()
  94. ], 400);
  95. }
  96. }
  97. else $this->settingService->set($settingName, $validated['value']);
  98. return response()->json([
  99. 'key' => $settingName,
  100. 'value' => $validated['value']
  101. ], 200);
  102. }
  103. /**
  104. * Save options
  105. * @return [type] [description]
  106. */
  107. public function destroy($settingName)
  108. {
  109. $setting = $this->settingService->get($settingName);
  110. if (is_null($setting)) {
  111. abort(404);
  112. }
  113. $optionsConfig = config('2fauth.options');
  114. if(array_key_exists($settingName, $optionsConfig)) {
  115. return response()->json(
  116. ['message' => 'bad request',
  117. 'reason' => [__('errors.delete_user_setting_only')]
  118. ], 400);
  119. }
  120. $this->settingService->delete($settingName);
  121. return response()->json(null, 204);
  122. }
  123. }