Files
construprogress/app/Livewire/RoleForm.php
T
javier 5587026446 feat(roles): Rappasoft list, slim create form, and 2-tab role view
1. Roles list now uses a Rappasoft table (RoleTable): search/sort, per-row
   view/edit/delete, and built-in bulk selection + 'Delete selected'. The
   /admin/roles page is a plain view embedding <livewire:role-table />.
   RoleForm create/edit now only has Name + Description (permissions removed).
2. New RoleView page (/admin/roles/{role}) with two tabs:
   - 'Details': header with role name + Back button; description with Edit/Delete
     buttons; table of users holding the role (avatar+name | last name | status).
   - 'Permissions': all permissions grouped by section (by resource), each with a
     toggle switch to grant/revoke for this role (Admin keeps 'manage all').
Removed the old RoleManager component/view (superseded).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 17:21:16 +02:00

67 lines
2.0 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\PermissionRegistrar;
#[Layout('layouts.app')]
class RoleForm extends Component
{
public ?Role $role = null;
public string $name = '';
public string $description = '';
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 ?? '';
}
}
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) {
// Protected roles can't be renamed
if (! in_array($this->role->name, self::PROTECTED_ROLES, true)) {
$this->role->name = $this->name;
}
$this->role->description = $this->description ?: null;
$this->role->save();
} else {
Role::create([
'name' => $this->name,
'description' => $this->description ?: null,
]);
}
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', [
'isProtected' => $this->role && in_array($this->role->name, self::PROTECTED_ROLES, true),
]);
}
}