UserController.php 3.6 KB

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