5092896a1e
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>
80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
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 RoleForm extends Component
|
|
{
|
|
public ?Role $role = null;
|
|
|
|
public string $name = '';
|
|
public string $description = '';
|
|
public array $rolePermissions = [];
|
|
|
|
private const PROTECTED_ROLES = ['Admin'];
|
|
private const CORE_PERMISSION = 'manage all';
|
|
|
|
public function mount(?Role $role = null): void
|
|
{
|
|
abort_unless(Auth::user()?->can(self::CORE_PERMISSION), 403);
|
|
|
|
if ($role && $role->exists) {
|
|
$this->role = $role;
|
|
$this->name = $role->name;
|
|
$this->description = $role->description ?? '';
|
|
$this->rolePermissions = $role->permissions->pluck('name')->toArray();
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate([
|
|
'name' => 'required|string|max:50|unique:roles,name' . ($this->role ? ',' . $this->role->id : ''),
|
|
'description' => 'nullable|string|max:255',
|
|
], [], ['name' => 'nombre', 'description' => 'descripción']);
|
|
|
|
if ($this->role) {
|
|
$isProtected = in_array($this->role->name, self::PROTECTED_ROLES, true);
|
|
if (! $isProtected) {
|
|
$this->role->name = $this->name;
|
|
}
|
|
$this->role->description = $this->description ?: null;
|
|
$this->role->save();
|
|
|
|
$perms = $this->rolePermissions;
|
|
if ($this->role->name === 'Admin' && ! in_array(self::CORE_PERMISSION, $perms, true)) {
|
|
$perms[] = self::CORE_PERMISSION;
|
|
}
|
|
$this->role->syncPermissions($perms);
|
|
} else {
|
|
$role = Role::create([
|
|
'name' => $this->name,
|
|
'description' => $this->description ?: null,
|
|
]);
|
|
if (! empty($this->rolePermissions)) {
|
|
$role->syncPermissions($this->rolePermissions);
|
|
}
|
|
}
|
|
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
|
session()->flash('message', 'Rol guardado correctamente.');
|
|
|
|
return $this->redirect(route('admin.roles'), navigate: true);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.roles.role-form', [
|
|
'permissions' => Permission::orderBy('name')->get(),
|
|
'isProtected' => $this->role && in_array($this->role->name, self::PROTECTED_ROLES, true),
|
|
]);
|
|
}
|
|
}
|