feat(roles/users): add-user form on role view + per-user direct permissions form

1. Role view (Details tab): a small form to add users to the role (select of
   users not yet in the role + Add) and a per-row remove button. Uses
   assignRole/removeRole.
2. User view (Permissions tab): the same grouped, collapsible permissions form
   with switches — operating on the user's DIRECT permissions
   (givePermissionTo/revokePermissionTo). Permissions inherited from a role show
   as checked+disabled with a 'from role' tag; per-group All/None too.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-17 18:51:59 +02:00
parent 7f20399337
commit 0120c4bfb8
7 changed files with 188 additions and 33 deletions
+49 -1
View File
@@ -9,6 +9,8 @@ use App\Models\Project;
use App\Models\Inspection;
use App\Models\Issue;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;
#[Layout('layouts.app')]
class UserView extends Component
@@ -93,6 +95,36 @@ class UserView extends Component
$this->dispatch('notify', 'Proyecto desasignado.');
}
// ── Permissions (direct, per user) ─────────────────────────────────────────
public function togglePermission(string $name): void
{
if ($this->user->hasDirectPermission($name)) {
$this->user->revokePermissionTo($name);
} else {
$this->user->givePermissionTo($name);
}
app(PermissionRegistrar::class)->forgetCachedPermissions();
$this->user->load('roles', 'permissions');
$this->dispatch('notify', 'Permisos del usuario actualizados');
}
public function setUserGroup(string $group, bool $enabled): void
{
foreach (Permission::where('group', $group)->pluck('name') as $name) {
if ($enabled) {
if (! $this->user->hasPermissionTo($name)) {
$this->user->givePermissionTo($name);
}
} elseif ($this->user->hasDirectPermission($name)) {
$this->user->revokePermissionTo($name);
}
}
app(PermissionRegistrar::class)->forgetCachedPermissions();
$this->user->load('roles', 'permissions');
$this->dispatch('notify', $enabled ? 'Permisos del grupo concedidos' : 'Permisos directos del grupo quitados');
}
// ── Notes ─────────────────────────────────────────────────────────────────
public function saveNotes(): void
@@ -105,6 +137,22 @@ class UserView extends Component
public function render()
{
return view('livewire.user-view');
$order = [
'Proyectos', 'Fases y progreso', 'Capas y elementos', 'Inspecciones',
'Incidencias', 'Empresas', 'Usuarios', 'Roles', 'Informes', 'Archivos', 'General',
];
$grouped = Permission::orderBy('name')->get()
->groupBy(fn ($perm) => $perm->group ?: 'General')
->sortBy(function ($perms, $section) use ($order) {
$i = array_search($section, $order, true);
return $i === false ? 999 : $i;
});
return view('livewire.user-view', [
'grouped' => $grouped,
'directPerms' => $this->user->getDirectPermissions()->pluck('name')->toArray(),
'rolePerms' => $this->user->getPermissionsViaRoles()->pluck('name')->toArray(),
]);
}
}