appRoleService = $appRoleService; } public function mount(int $id): void { $this->attempt( action: function () use ($id): void { $role = Role::query()->findOrFail($id); $this->roleId = $role->id; $this->roleName = $role->name; }, onError: function (): void { $this->redirectRoute('access-manager.roles-and-apps', navigate: true); }, showSuccess: false, ); $this->appHeaders = TableHeader::collect([ new TableHeader(key: 'app_name', label: 'App'), new TableHeader(key: 'duration', label: 'Duration (days)'), ], DataCollection::class); $this->userHeaders = TableHeader::collect([ new TableHeader(key: 'name', label: 'Name'), ], DataCollection::class); } #[Computed] public function apps(): DataCollection { return $this->appRoleService->getAppsForRole($this->roleId); } /** * Get users assigned to this role via spatie/laravel-permission. */ #[Computed] public function getRoleUsers(): DataCollection { return $this->appRoleService->getUsersForRole($this->roleId); } public function openAddAppModal(): void { $this->isEditingApp = false; $this->editingAssignmentId = null; $this->editingAppName = ''; $this->assignForm->reset(); $this->resetErrorBag(); $this->searchApps(''); $this->showAddAppModal = true; } public function openEditAppModal(int $assignmentId): void { $this->isEditingApp = true; $this->editingAssignmentId = $assignmentId; $this->resetErrorBag(); $assignment = collect($this->apps())->firstWhere('id', $assignmentId); if ($assignment) { $this->assignForm->appIds = [$assignment['appId'] ?? $assignment['id']]; $this->assignForm->duration = $assignment['duration']; $this->editingAppName = $assignment['appName']; } $this->showAddAppModal = true; } public function saveApp(): void { $this->assignForm->validate(); if ($this->isEditingApp) { $this->updateApp(); } else { $this->assignApp(); } } public function searchApps(string $value = ''): void { $this->appsSearchable = $this ->appRoleService ->searchAppsForSelect($value) ->toArray(); } public function selectAllApps(): void { $this->assignForm->appIds = $this->appRoleService->getAllAppIds(); $this->appsSearchable = $this->appRoleService->searchAppsForSelect('')->toArray(); } public function clearApps(): void { $this->assignForm->appIds = []; } public function assignApp(): void { $this->attempt( action: function (): void { $this->appRoleService->assign(new AssignAppsToRoleData( roleId: $this->roleId, duration: $this->assignForm->duration, appIds: $this->assignForm->appIds, )); $this->assignForm->reset(); $this->showAddAppModal = false; unset($this->apps); }, successMessage: 'Apps assigned successfully!', ); } public function updateApp(): void { $this->attempt( action: function (): void { $this->appRoleService->assign(new AssignAppsToRoleData( roleId: $this->roleId, duration: $this->assignForm->duration, appIds: $this->assignForm->appIds, )); $this->assignForm->reset(); $this->showAddAppModal = false; unset($this->apps); }, successMessage: 'Apps updated successfully!', ); } public function detachApp(int $assignmentId): void { $this->attempt( action: function () use ($assignmentId): void { $this->appRoleService->detach($assignmentId); unset($this->apps); }, successMessage: 'App removed from role.', ); } public function openAddUserModal(): void { $this->reset('selectedUserIds'); $this->resetErrorBag(); $this->searchUsers(''); $this->showAddUserModal = true; } public function searchUsers(string $value = ''): void { $this->usersSearchable = $this ->appRoleService ->searchUsersForSelect($value) ->toArray(); } public function assignUser(): void { $this->validate([ 'selectedUserIds' => 'required|array|min:1', 'selectedUserIds.*' => 'integer|exists:users,id', ]); $this->attempt( action: function (): void { $this->appRoleService->assignUsersToRole(new AssignUsersToRoleData( roleId: $this->roleId, userIds: $this->selectedUserIds, )); $this->reset('selectedUserIds'); $this->showAddUserModal = false; unset($this->getRoleUsers); }, successMessage: 'Users assigned successfully!', ); } public function detachUser(int $userId): void { $this->attempt( action: function () use ($userId): void { $this->appRoleService->detachUserFromRole($this->roleId, $userId); unset($this->getRoleUsers); }, successMessage: 'User removed from role.', ); } }; ?>
No apps assigned to this role yet.
@endifNo users assigned to this role yet.
@endif