UserController.php 3.9 KB

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