refactor(roles): role create/edit as a full page instead of a modal
Per feedback, 'New role' (and Edit) now open a dedicated page instead of a
modal:
- New RoleForm full-page component + view at /admin/roles/create and
/admin/roles/{role}/edit (name, description, permission checkboxes; saves
and redirects back to the list).
- RoleManager trimmed: the create/edit modal and its logic removed; 'New role'
and the per-row/view-modal Edit are now links to the new pages.
- Kept the read-only View modal, single + bulk delete, and protections.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,19 +6,11 @@ use Livewire\Component;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class RoleManager extends Component
|
||||
{
|
||||
// Create / edit modal
|
||||
public bool $showForm = false;
|
||||
public ?int $editingRole = null;
|
||||
public string $name = '';
|
||||
public string $description = '';
|
||||
public array $rolePermissions = []; // selected permission names
|
||||
|
||||
// View modal
|
||||
public ?int $viewingRole = null;
|
||||
|
||||
@@ -46,77 +38,6 @@ class RoleManager extends Component
|
||||
: [];
|
||||
}
|
||||
|
||||
// ── Create / Edit ────────────────────────────────────────────────────────
|
||||
|
||||
public function openCreate(): void
|
||||
{
|
||||
$this->resetForm();
|
||||
$this->showForm = true;
|
||||
}
|
||||
|
||||
public function openEdit(int $id): void
|
||||
{
|
||||
$role = Role::with('permissions')->findOrFail($id);
|
||||
$this->editingRole = $role->id;
|
||||
$this->name = $role->name;
|
||||
$this->description = $role->description ?? '';
|
||||
$this->rolePermissions = $role->permissions->pluck('name')->toArray();
|
||||
$this->resetErrorBag();
|
||||
$this->viewingRole = null; // close the view modal if open
|
||||
$this->showForm = true;
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$this->validate([
|
||||
'name' => 'required|string|max:50|unique:roles,name' . ($this->editingRole ? ',' . $this->editingRole : ''),
|
||||
'description' => 'nullable|string|max:255',
|
||||
], [], ['name' => 'nombre', 'description' => 'descripción']);
|
||||
|
||||
if ($this->editingRole) {
|
||||
$role = Role::findOrFail($this->editingRole);
|
||||
$isProtected = in_array($role->name, self::PROTECTED_ROLES, true);
|
||||
|
||||
// Protected roles can't be renamed
|
||||
if (! $isProtected) {
|
||||
$role->name = $this->name;
|
||||
}
|
||||
$role->description = $this->description ?: null;
|
||||
$role->save();
|
||||
|
||||
$perms = $this->rolePermissions;
|
||||
// Admin always keeps the core permission
|
||||
if ($role->name === 'Admin' && ! in_array(self::CORE_PERMISSION, $perms, true)) {
|
||||
$perms[] = self::CORE_PERMISSION;
|
||||
}
|
||||
$role->syncPermissions($perms);
|
||||
} else {
|
||||
$role = Role::create([
|
||||
'name' => $this->name,
|
||||
'description' => $this->description ?: null,
|
||||
]);
|
||||
if (! empty($this->rolePermissions)) {
|
||||
$role->syncPermissions($this->rolePermissions);
|
||||
}
|
||||
}
|
||||
|
||||
$this->flushCache();
|
||||
$this->closeForm();
|
||||
$this->dispatch('notify', 'Rol guardado correctamente');
|
||||
}
|
||||
|
||||
public function closeForm(): void
|
||||
{
|
||||
$this->showForm = false;
|
||||
$this->resetForm();
|
||||
}
|
||||
|
||||
private function resetForm(): void
|
||||
{
|
||||
$this->reset(['editingRole', 'name', 'description', 'rolePermissions']);
|
||||
$this->resetErrorBag();
|
||||
}
|
||||
|
||||
// ── View ─────────────────────────────────────────────────────────────────
|
||||
|
||||
public function openView(int $id): void
|
||||
@@ -155,6 +76,7 @@ class RoleManager extends Component
|
||||
$deleted++;
|
||||
}
|
||||
$this->selected = [];
|
||||
$this->selectAll = false;
|
||||
$this->flushCache();
|
||||
$msg = "{$deleted} rol(es) eliminados";
|
||||
if ($skipped) $msg .= " ({$skipped} protegido(s) omitido(s))";
|
||||
@@ -164,9 +86,8 @@ class RoleManager extends Component
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.role-manager', [
|
||||
'roles' => Role::with('permissions')->withCount('users')->orderBy('name')->get(),
|
||||
'permissions' => Permission::orderBy('name')->get(),
|
||||
'viewing' => $this->viewingRole
|
||||
'roles' => Role::with('permissions')->withCount('users')->orderBy('name')->get(),
|
||||
'viewing' => $this->viewingRole
|
||||
? Role::with('permissions')->withCount('users')->find($this->viewingRole)
|
||||
: null,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user