SettingController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Api\v1\Controllers;
  3. use App\Exceptions\DbEncryptionException;
  4. use App\Services\DbEncryptionService;
  5. use App\Services\SettingServiceInterface;
  6. use App\Api\v1\Requests\SettingStoreRequest;
  7. use App\Api\v1\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\JsonResponse
  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 setting
  48. *
  49. * @param string $settingName
  50. * @return \Illuminate\Http\JsonResponse
  51. */
  52. public function show($settingName)
  53. {
  54. $setting = $this->settingService->get($settingName);
  55. if (is_null($setting)) {
  56. abort(404);
  57. }
  58. return response()->json([
  59. 'key' => $settingName,
  60. 'value' => $setting
  61. ], 200);
  62. }
  63. /**
  64. * Store a setting
  65. *
  66. * @param \App\Api\v1\Requests\SettingStoreRequest $request
  67. * @return \Illuminate\Http\JsonResponse
  68. */
  69. public function store(SettingStoreRequest $request)
  70. {
  71. $validated = $request->validated();
  72. $this->settingService->set($validated['key'], $validated['value']);
  73. return response()->json([
  74. 'key' => $validated['key'],
  75. 'value' => $validated['value']
  76. ], 201);
  77. }
  78. /**
  79. * Update a setting
  80. *
  81. * @param \App\Api\v1\Requests\SettingUpdateRequest $request
  82. * @return \Illuminate\Http\JsonResponse
  83. */
  84. public function update(SettingUpdateRequest $request, $settingName)
  85. {
  86. $validated = $request->validated();
  87. // The useEncryption setting impacts records in DB so we delegate the work to the
  88. // dedicated db encryption service
  89. if( $settingName === 'useEncryption')
  90. {
  91. try {
  92. $this->dbEncryptionService->setTo($validated['value']);
  93. }
  94. catch(DbEncryptionException $ex) {
  95. return response()->json([
  96. 'message' => $ex->getMessage()
  97. ], 400);
  98. }
  99. }
  100. else $this->settingService->set($settingName, $validated['value']);
  101. return response()->json([
  102. 'key' => $settingName,
  103. 'value' => $validated['value']
  104. ], 200);
  105. }
  106. /**
  107. * Delete a setting
  108. *
  109. * @param \App\Api\v1\Requests\SettingUpdateRequest $request
  110. * @return \Illuminate\Http\JsonResponse
  111. */
  112. public function destroy($settingName)
  113. {
  114. $setting = $this->settingService->get($settingName);
  115. if (is_null($setting)) {
  116. abort(404);
  117. }
  118. $optionsConfig = config('2fauth.options');
  119. if(array_key_exists($settingName, $optionsConfig)) {
  120. return response()->json(
  121. ['message' => 'bad request',
  122. 'reason' => [__('errors.delete_user_setting_only')]
  123. ], 400);
  124. }
  125. $this->settingService->delete($settingName);
  126. return response()->json(null, 204);
  127. }
  128. }