SettingController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Api\v1\Controllers;
  3. use App\Exceptions\DbEncryptionException;
  4. use App\Services\DbEncryptionService;
  5. use App\Services\SettingService;
  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 SettingService $settingService;
  15. /**
  16. * Create a new controller instance.
  17. */
  18. public function __construct(SettingService $settingService)
  19. {
  20. $this->settingService = $settingService;
  21. }
  22. /**
  23. * List all settings
  24. *
  25. * @return \Illuminate\Http\JsonResponse
  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 setting
  42. *
  43. * @param string $settingName
  44. * @return \Illuminate\Http\JsonResponse
  45. */
  46. public function show($settingName)
  47. {
  48. $setting = $this->settingService->get($settingName);
  49. if (is_null($setting)) {
  50. abort(404);
  51. }
  52. return response()->json([
  53. 'key' => $settingName,
  54. 'value' => $setting
  55. ], 200);
  56. }
  57. /**
  58. * Store a setting
  59. *
  60. * @param \App\Api\v1\Requests\SettingStoreRequest $request
  61. * @return \Illuminate\Http\JsonResponse
  62. */
  63. public function store(SettingStoreRequest $request)
  64. {
  65. $validated = $request->validated();
  66. $this->settingService->set($validated['key'], $validated['value']);
  67. return response()->json([
  68. 'key' => $validated['key'],
  69. 'value' => $validated['value']
  70. ], 201);
  71. }
  72. /**
  73. * Update a setting
  74. *
  75. * @param \App\Api\v1\Requests\SettingUpdateRequest $request
  76. * @return \Illuminate\Http\JsonResponse
  77. */
  78. public function update(SettingUpdateRequest $request, $settingName)
  79. {
  80. $validated = $request->validated();
  81. $this->settingService->set($settingName, $validated['value']);
  82. return response()->json([
  83. 'key' => $settingName,
  84. 'value' => $validated['value']
  85. ], 200);
  86. }
  87. /**
  88. * Delete a setting
  89. *
  90. * @param \App\Api\v1\Requests\SettingUpdateRequest $request
  91. * @return \Illuminate\Http\JsonResponse
  92. */
  93. public function destroy($settingName)
  94. {
  95. $setting = $this->settingService->get($settingName);
  96. if (is_null($setting)) {
  97. abort(404);
  98. }
  99. $optionsConfig = config('2fauth.options');
  100. if(array_key_exists($settingName, $optionsConfig)) {
  101. return response()->json(
  102. ['message' => 'bad request',
  103. 'reason' => [__('errors.delete_user_setting_only')]
  104. ], 400);
  105. }
  106. $this->settingService->delete($settingName);
  107. return response()->json(null, 204);
  108. }
  109. }