SettingController.php 4.0 KB

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