SettingController.php 4.0 KB

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