SettingController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 'key' => $key,
  34. 'value' => $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 $settingName
  44. *
  45. * @return \App\Http\Resources\TwoFAccountReadResource
  46. */
  47. public function show($settingName)
  48. {
  49. $setting = $this->settingService->get($settingName);
  50. if (!$setting) {
  51. abort(404);
  52. }
  53. return response()->json([
  54. 'key' => $settingName,
  55. 'value' => $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['key'], $validated['value']);
  66. return response()->json([
  67. 'key' => $validated['key'],
  68. 'value' => $validated['value']
  69. ], 201);
  70. }
  71. /**
  72. * Save options
  73. * @return [type] [description]
  74. */
  75. public function update(SettingUpdateRequest $request, $settingName)
  76. {
  77. $validated = $request->validated();
  78. $this->settingService->set($settingName, $validated['value']);
  79. return response()->json([
  80. 'key' => $settingName,
  81. 'value' => $validated['value']
  82. ], 200);
  83. // The useEncryption option impacts the [existing] content of the database.
  84. // Encryption/Decryption of the data is done only if the user change the value of the option
  85. // to prevent successive encryption
  86. if( $request->has('useEncryption'))
  87. {
  88. if( $request->useEncryption && !$this->settingService->get('useEncryption') ) {
  89. // user enabled the encryption
  90. if( !DbProtection::enable() ) {
  91. return response()->json(['message' => __('errors.error_during_encryption')], 400);
  92. }
  93. }
  94. else if( !$request->useEncryption && $this->settingService->get('useEncryption') ) {
  95. // user disabled the encryption
  96. if( !DbProtection::disable() ) {
  97. return response()->json(['message' => __('errors.error_during_decryption')], 400);
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Save options
  104. * @return [type] [description]
  105. */
  106. public function destroy($settingName)
  107. {
  108. $setting = $this->settingService->get($settingName);
  109. if (is_null($setting)) {
  110. abort(404);
  111. }
  112. $optionsConfig = config('2fauth.options');
  113. if(array_key_exists($settingName, $optionsConfig)) {
  114. return response()->json(
  115. ['message' => 'bad request',
  116. 'reason' => [__('errors.delete_user_setting_only')]
  117. ], 400);
  118. }
  119. $this->settingService->delete($settingName);
  120. return response()->json(null, 204);
  121. }
  122. }