UserController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Concerns\ResolvesActiveUnit;
  4. use App\Services\UserService;
  5. use App\Http\Requests\UserRequest;
  6. use App\Http\Resources\UserResource;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Support\Facades\Auth;
  9. class UserController extends Controller
  10. {
  11. use ResolvesActiveUnit;
  12. public function __construct(protected UserService $service) {}
  13. public function authUser(): JsonResponse
  14. {
  15. $user = $this->service->authUser();
  16. return $this->successResponse(payload: new UserResource($user));
  17. }
  18. public function index(): JsonResponse
  19. {
  20. $items = $this->service->getAll();
  21. return $this->successResponse(
  22. payload: UserResource::collection($items),
  23. );
  24. }
  25. public function indexByUnit(): JsonResponse
  26. {
  27. $items = $this->service->getAllByUnit();
  28. return $this->successResponse(
  29. payload: UserResource::collection($items),
  30. );
  31. }
  32. public function store(UserRequest $request): JsonResponse
  33. {
  34. $data = $request->validated();
  35. if ($this->isFranchiseeUsersRequest()) {
  36. $data['unit_id'] = $this->activeUnitId();
  37. $data['access_scope'] = 'unit';
  38. }
  39. $item = $this->service->create($data);
  40. return $this->successResponse(
  41. payload: new UserResource($item),
  42. message: __("messages.created"),
  43. code: 201,
  44. );
  45. }
  46. public function show(int $id): JsonResponse
  47. {
  48. $item = $this->service->findById($id);
  49. $this->authorizeFranchiseeUser($item);
  50. return $this->successResponse(payload: new UserResource($item));
  51. }
  52. public function update(UserRequest $request, int $id): JsonResponse
  53. {
  54. $current = $this->service->findById($id);
  55. $this->authorizeFranchiseeUser($current);
  56. $data = $request->validated();
  57. if ($this->isFranchiseeUsersRequest()) {
  58. $data['unit_id'] = $this->activeUnitId();
  59. $data['access_scope'] = 'unit';
  60. }
  61. $item = $this->service->update($id, $data);
  62. return $this->successResponse(
  63. payload: new UserResource($item),
  64. message: __("messages.updated"),
  65. );
  66. }
  67. public function destroy(int $id): JsonResponse
  68. {
  69. $this->authorizeFranchiseeUser($this->service->findById($id));
  70. $this->service->delete($id);
  71. return $this->successResponse(
  72. message: __("messages.deleted"), code: 204,
  73. );
  74. }
  75. public function updateMe(UserRequest $request): JsonResponse
  76. {
  77. $user = Auth::user();
  78. $user->update($request->safe()->only(['password']));
  79. return $this->successResponse(
  80. payload: new UserResource($user->fresh(['state', 'units'])),
  81. message: __("messages.updated"),
  82. );
  83. }
  84. private function isFranchiseeUsersRequest(): bool
  85. {
  86. return request()->is('api/franchisee/users*');
  87. }
  88. private function authorizeFranchiseeUser(?\App\Models\User $user): void
  89. {
  90. if (!$this->isFranchiseeUsersRequest()) {
  91. return;
  92. }
  93. abort_unless($user, 404);
  94. $unitId = request()->input('active_unit_id');
  95. abort_unless($unitId && $user->units()->where('units.id', $unitId)->exists(), 404);
  96. $role = \App\Models\UnitUser::query()
  97. ->where('unit_id', $unitId)
  98. ->where('user_id', $user->id)
  99. ->with('userType')
  100. ->first()?->userType;
  101. abort_if($role?->system_key === 'ADMIN_FRANCHISEE', 403, 'Administradores do sistema não podem ser editados.');
  102. }
  103. }