userService = $userService; $this->roleService = $roleService; } public function mount(): void { $this->headers = TableHeader::collect([ new TableHeader(key: 'name', label: 'User'), new TableHeader(key: 'roles', label: 'Roles'), new TableHeader(key: 'permissions', label: 'Permissions'), new TableHeader(key: 'apps', label: 'Connected Apps'), ], DataCollection::class); } #[Computed] public function getUsers(): PaginatedDataCollection { return $this->userService->getAll(); } public function delete(int $userId): void { $this->attempt( action: function () use ($userId): void { $this->userService->delete($userId); unset($this->getUsers); }, successMessage: 'User deleted successfully!', ); } public function openAssignRolesModal(int $userId): void { $this->attempt( action: function () use ($userId) { $this->currentUserId = $userId; $this->form->reset(); $this->searchRoles(); $this->form->roleIds = User::findOrFail($userId)->roles()->pluck('id')->toArray(); $this->hydratePermissionInForm(); $this->showAssignRolesModal = true; }, showSuccess: false ); } private function hydratePermissionInForm(): void { $this->roleWithPermission = $this->roleService->getPermissionsGroupedByRole($this->form->roleIds); $allPermissionIds = []; if ($this->roleWithPermission) { foreach ($this->roleWithPermission->items() as $role) { foreach ($role->permissions->items() as $permission) { $allPermissionIds[] = $permission->id; } } } $restrictedIds = $this->userService->getRestrictedPermissionIds($this->currentUserId); $this->form->permissions = array_values(array_diff($allPermissionIds, $restrictedIds)); } /** * Refresh the permission list whenever the roles selection changes. * This method is automatically hooked by livewire cause of naming convention. */ public function updatedFormRoleIds(): void { $this->hydratePermissionInForm(); } public function searchRoles(string $value = ''): void { $this->rolesSearchable = $this->roleService->searchRolesForSelect($value)->toArray(); } public function selectAllRoles(): void { $this->form->roleIds = $this->roleService->getAllRoleIds(); $this->searchRoles(); $this->hydratePermissionInForm(); } public function clearRoles(): void { $this->form->roleIds = []; $this->hydratePermissionInForm(); } public function selectAllPermissions(): void { $allPermissionIds = []; if ($this->roleWithPermission) { foreach ($this->roleWithPermission->items() as $role) { foreach ($role->permissions->items() as $permission) { $allPermissionIds[] = $permission->id; } } } $this->form->permissions = array_values(array_unique($allPermissionIds)); } public function revokeAllPermissions(): void { $this->form->permissions = []; } /** * Get the deactivated permissions represented as DTOs containing the role ID and permission ID. * * @return DataCollection */ public function getDeactivatedPermissions(): DataCollection { $deactivated = []; $activePermissions = array_map('intval', $this->form->permissions); if ($this->roleWithPermission) { foreach ($this->roleWithPermission->items() as $role) { foreach ($role->permissions->items() as $permission) { if (!in_array($permission->id, $activePermissions, true)) { $deactivated[] = new DeactivatedPermissionData( roleId: $role->id, permissionId: $permission->id ); } } } } return DeactivatedPermissionData::collect($deactivated, DataCollection::class); } public function saveRoles(): void { $this->attempt( action: function () { $this->form->validate(); $deactivatedPermissions = $this->getDeactivatedPermissions(); $this->userService->assignRoles($this->currentUserId, $this->form->roleIds); $this->userService->syncRestrictedPermissions($this->currentUserId, $deactivatedPermissions); $this->showAssignRolesModal = false; $this->roleWithPermission = null; $this->form->reset(); unset($this->getUsers); }, onError: function () { $this->showAssignRolesModal = false; $this->roleWithPermission = null; $this->form->reset(); }, successMessage: 'Roles updated successfully!' ); } }; ?>
@scope('cell_name', $row)
{{ $row->name }}
{{ $row->email }}
@endscope @scope('cell_roles', $row)
@foreach($row->roles as $role) @endforeach
@endscope @scope('cell_permissions', $row)
@php $displayedPermissions = []; @endphp @foreach($row->roles as $role) @if($role->permissions) @foreach($role->permissions as $permission) @if(!in_array($permission->id, $displayedPermissions)) @php $displayedPermissions[] = $permission->id; $isRestricted = in_array($permission->id, $row->restrictedPermissionIds ?? []); @endphp @if($isRestricted) @else @endif @endif @endforeach @endif @endforeach
@endscope @scope('cell_apps', $row)
@foreach($row->roles as $role) @if($role->apps) @foreach ($role->apps as $app) @endforeach @endif @endforeach
@endscope @scope('actions', $row)
@endscope
@if ($roleWithPermission)

Permissions

@foreach ($roleWithPermission?->items() as $role)
{{ $role->name }} {{$role->permissions->count()}} {{ $role->permissions->count() >= 2 ? 'Permissions' : 'Permission' }}
@foreach ($role->permissions as $permission) @endforeach
@endforeach
@endif Cancel Save Roles