updaetd to roles and permissions

This commit is contained in:
Mark Gregory
2026-08-26 10:38:38 +01:00
parent af6ebe5730
commit 7fcef01be4
10 changed files with 456 additions and 21 deletions
+61 -7
View File
@@ -242,6 +242,7 @@ final class SettingsManager extends Component
public function saveTeam(): void
{
$this->resetValidation();
$this->normalizeNullableFormValues('teamForm', []);
$data = $this->validate([
@@ -254,27 +255,80 @@ final class SettingsManager extends Component
'teamForm.user_ids.*' => ['integer', 'distinct', 'exists:users,id'],
])['teamForm'];
$userIds = array_values(array_unique(array_map('intval', $data['user_ids'] ?? [])));
$teamId = filled($this->teamForm['id'] ?? null)
? (int) $this->teamForm['id']
: null;
$userIds = collect($data['user_ids'] ?? [])
->map(static fn ($id): int => (int) $id)
->unique()
->values()
->all();
unset($data['user_ids']);
$data['slug'] = $this->slug($data['slug'] ?: $data['name']);
$data = $this->guardDefaultColumn('helpdesk_teams', $data);
if ((bool) ($data['is_default'] ?? false)) {
$this->clearOtherDefaults(Team::class, $this->teamForm['id']);
$this->clearOtherDefaults(Team::class, $teamId);
}
/** @var Team $team */
$team = $this->persistSettingModel(Team::class, $this->teamForm['id'], $data);
$team->users()->sync($userIds);
try {
if ($teamId !== null) {
/** @var Team $team */
$team = Team::withTrashed()->findOrFail($teamId);
if ($team->trashed()) {
$team->restore();
}
$team->fill($data);
$team->save();
} else {
// Team slugs are unique even after soft deletion. Recreating a
// previously deleted team should restore that record instead
// of failing with a duplicate-key exception.
/** @var Team|null $team */
$team = Team::withTrashed()
->where('slug', $data['slug'])
->first();
if ($team !== null) {
if (! $team->trashed()) {
throw ValidationException::withMessages([
'teamForm.slug' => 'A team with this slug already exists.',
]);
}
$team->restore();
$team->fill($data);
$team->save();
} else {
$team = Team::query()->create($data);
}
}
if (Schema::hasTable('helpdesk_team_user')) {
$team->users()->sync($userIds);
}
} catch (ValidationException $exception) {
throw $exception;
} catch (QueryException $exception) {
throw ValidationException::withMessages([
'teamForm.slug' => 'The team could not be saved. Check that the name/slug is unique and try again.',
]);
}
$this->resetTeamForm();
session()->flash('success', 'Team saved.');
$this->section = 'teams';
session()->flash('success', 'Team saved successfully.');
}
public function editTeam(int $id): void
{
$team = Team::query()->with('users:id')->findOrFail($id);
$team = Team::withTrashed()->with('users:id')->findOrFail($id);
$this->teamForm = $team->only(['id', 'name', 'slug', 'color', 'description', 'is_default', 'archived_at']);
$this->teamForm['user_ids'] = $team->users->pluck('id')->map(static fn ($id): int => (int) $id)->all();
$this->teamForm['color'] = Color::normalise($this->teamForm['color'] ?? 'indigo');