|
@@ -42,12 +42,13 @@ public function findById(int $id): ?User
|
|
|
|
|
|
|
|
public function create(array $data): User
|
|
public function create(array $data): User
|
|
|
{
|
|
{
|
|
|
|
|
+ $this->assertMatchingAccessScope($data);
|
|
|
// Suporta unit_ids[] (múltiplas) ou unit_id (retrocompatibilidade)
|
|
// Suporta unit_ids[] (múltiplas) ou unit_id (retrocompatibilidade)
|
|
|
$unitIds = $data['unit_ids'] ?? null;
|
|
$unitIds = $data['unit_ids'] ?? null;
|
|
|
if (empty($unitIds) && isset($data['unit_id'])) {
|
|
if (empty($unitIds) && isset($data['unit_id'])) {
|
|
|
$unitIds = array_filter([$data['unit_id']]);
|
|
$unitIds = array_filter([$data['unit_id']]);
|
|
|
}
|
|
}
|
|
|
- if (empty($unitIds)) {
|
|
|
|
|
|
|
+ if (empty($unitIds) && ($data['access_scope'] ?? 'unit') === 'unit') {
|
|
|
$fallback = Auth::user()->units->first()?->id;
|
|
$fallback = Auth::user()->units->first()?->id;
|
|
|
$unitIds = $fallback ? [$fallback] : [];
|
|
$unitIds = $fallback ? [$fallback] : [];
|
|
|
}
|
|
}
|
|
@@ -71,6 +72,8 @@ public function update(int $id, array $data): ?User
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ $this->assertMatchingAccessScope($data, $model);
|
|
|
|
|
+
|
|
|
// Suporta unit_ids[] (múltiplas) com fallback para unit_id (retrocompatibilidade)
|
|
// Suporta unit_ids[] (múltiplas) com fallback para unit_id (retrocompatibilidade)
|
|
|
$hasUnitIds = array_key_exists('unit_ids', $data);
|
|
$hasUnitIds = array_key_exists('unit_ids', $data);
|
|
|
$unitIds = $data['unit_ids'] ?? null;
|
|
$unitIds = $data['unit_ids'] ?? null;
|
|
@@ -81,7 +84,9 @@ public function update(int $id, array $data): ?User
|
|
|
$data = $this->handleAvatar($data, $model->avatar_url);
|
|
$data = $this->handleAvatar($data, $model->avatar_url);
|
|
|
$model->update($data);
|
|
$model->update($data);
|
|
|
|
|
|
|
|
- if ($hasUnitIds) {
|
|
|
|
|
|
|
+ if (($data['access_scope'] ?? $model->access_scope) === 'franchisor') {
|
|
|
|
|
+ $model->units()->sync([]);
|
|
|
|
|
+ } elseif ($hasUnitIds) {
|
|
|
$model->units()->sync(array_filter($unitIds ?? []));
|
|
$model->units()->sync(array_filter($unitIds ?? []));
|
|
|
} elseif ($hasUnitId) {
|
|
} elseif ($hasUnitId) {
|
|
|
$model->units()->sync($unitId ? [$unitId] : []);
|
|
$model->units()->sync($unitId ? [$unitId] : []);
|
|
@@ -107,7 +112,10 @@ public function delete(int $id): bool
|
|
|
|
|
|
|
|
public function getUserTypes(): \Illuminate\Database\Eloquent\Collection
|
|
public function getUserTypes(): \Illuminate\Database\Eloquent\Collection
|
|
|
{
|
|
{
|
|
|
- return \App\Models\UserType::orderBy('label')->get();
|
|
|
|
|
|
|
+ return \App\Models\UserType::query()
|
|
|
|
|
+ ->when(request()->input('access_scope'), fn ($query, $scope) => $query->where('access_scope', $scope))
|
|
|
|
|
+ ->orderBy('label')
|
|
|
|
|
+ ->get();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
|
|
private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
|
|
@@ -131,4 +139,13 @@ private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
|
|
|
unset($data['avatar']);
|
|
unset($data['avatar']);
|
|
|
return $data;
|
|
return $data;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private function assertMatchingAccessScope(array $data, ?User $user = null): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $slug = $data['user_type'] ?? $user?->user_type;
|
|
|
|
|
+ $scope = $data['access_scope'] ?? $user?->access_scope;
|
|
|
|
|
+ $type = \App\Models\UserType::where('slug', $slug)->first();
|
|
|
|
|
+
|
|
|
|
|
+ abort_if($type && $scope && $type->access_scope !== $scope, 422, 'O perfil não pertence ao tipo de acesso selecionado.');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|