|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
+use App\Http\Controllers\Concerns\ResolvesActiveUnit;
|
|
|
use App\Services\UserService;
|
|
use App\Services\UserService;
|
|
|
use App\Http\Requests\UserRequest;
|
|
use App\Http\Requests\UserRequest;
|
|
|
use App\Http\Resources\UserResource;
|
|
use App\Http\Resources\UserResource;
|
|
@@ -11,6 +12,8 @@
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
class UserController extends Controller
|
|
|
{
|
|
{
|
|
|
|
|
+ use ResolvesActiveUnit;
|
|
|
|
|
+
|
|
|
public function __construct(protected UserService $service) {}
|
|
public function __construct(protected UserService $service) {}
|
|
|
|
|
|
|
|
public function authUser(): JsonResponse
|
|
public function authUser(): JsonResponse
|
|
@@ -39,7 +42,12 @@ public function indexByUnit(): JsonResponse
|
|
|
|
|
|
|
|
public function store(UserRequest $request): JsonResponse
|
|
public function store(UserRequest $request): JsonResponse
|
|
|
{
|
|
{
|
|
|
- $item = $this->service->create($request->validated());
|
|
|
|
|
|
|
+ $data = $request->validated();
|
|
|
|
|
+ if ($this->isFranchiseeUsersRequest()) {
|
|
|
|
|
+ $data['unit_id'] = $this->activeUnitId();
|
|
|
|
|
+ $data['access_scope'] = 'unit';
|
|
|
|
|
+ }
|
|
|
|
|
+ $item = $this->service->create($data);
|
|
|
|
|
|
|
|
return $this->successResponse(
|
|
return $this->successResponse(
|
|
|
payload: new UserResource($item),
|
|
payload: new UserResource($item),
|
|
@@ -51,12 +59,20 @@ public function store(UserRequest $request): JsonResponse
|
|
|
public function show(int $id): JsonResponse
|
|
public function show(int $id): JsonResponse
|
|
|
{
|
|
{
|
|
|
$item = $this->service->findById($id);
|
|
$item = $this->service->findById($id);
|
|
|
|
|
+ $this->authorizeFranchiseeUser($item);
|
|
|
return $this->successResponse(payload: new UserResource($item));
|
|
return $this->successResponse(payload: new UserResource($item));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function update(UserRequest $request, int $id): JsonResponse
|
|
public function update(UserRequest $request, int $id): JsonResponse
|
|
|
{
|
|
{
|
|
|
- $item = $this->service->update($id, $request->validated());
|
|
|
|
|
|
|
+ $current = $this->service->findById($id);
|
|
|
|
|
+ $this->authorizeFranchiseeUser($current);
|
|
|
|
|
+ $data = $request->validated();
|
|
|
|
|
+ if ($this->isFranchiseeUsersRequest()) {
|
|
|
|
|
+ $data['unit_id'] = $this->activeUnitId();
|
|
|
|
|
+ $data['access_scope'] = 'unit';
|
|
|
|
|
+ }
|
|
|
|
|
+ $item = $this->service->update($id, $data);
|
|
|
return $this->successResponse(
|
|
return $this->successResponse(
|
|
|
payload: new UserResource($item),
|
|
payload: new UserResource($item),
|
|
|
message: __("messages.updated"),
|
|
message: __("messages.updated"),
|
|
@@ -65,6 +81,7 @@ public function update(UserRequest $request, int $id): JsonResponse
|
|
|
|
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
public function destroy(int $id): JsonResponse
|
|
|
{
|
|
{
|
|
|
|
|
+ $this->authorizeFranchiseeUser($this->service->findById($id));
|
|
|
$this->service->delete($id);
|
|
$this->service->delete($id);
|
|
|
|
|
|
|
|
return $this->successResponse(
|
|
return $this->successResponse(
|
|
@@ -94,4 +111,27 @@ public function updateMe(UserRequest $request): JsonResponse
|
|
|
message: __("messages.updated"),
|
|
message: __("messages.updated"),
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private function isFranchiseeUsersRequest(): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ return request()->is('api/franchisee/users*');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function authorizeFranchiseeUser(?\App\Models\User $user): void
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!$this->isFranchiseeUsersRequest()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ abort_unless($user, 404);
|
|
|
|
|
+ $unitId = request()->input('active_unit_id');
|
|
|
|
|
+ abort_unless($unitId && $user->units()->where('units.id', $unitId)->exists(), 404);
|
|
|
|
|
+
|
|
|
|
|
+ $role = \App\Models\UnitUser::query()
|
|
|
|
|
+ ->where('unit_id', $unitId)
|
|
|
|
|
+ ->where('user_id', $user->id)
|
|
|
|
|
+ ->with('userType')
|
|
|
|
|
+ ->first()?->userType;
|
|
|
|
|
+ abort_if($role?->system_key === 'ADMIN_FRANCHISEE', 403, 'Administradores do sistema não podem ser editados.');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|