appRoleService = $appRoleService; $this->roleService = $roleService; } 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; $this->rolePriority = $role->priority; }, onError: function (): void { $this->redirectRoute('access-manager.roles-and-apps.index', navigate: true); }, showSuccess: false, ); $this->appHeaders = TableHeader::collect([ new TableHeader(key: 'app_name', label: 'App'), new TableHeader(key: 'duration', label: 'Validity'), ], 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->validUpto = $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->assignForm->validate(); $this->appRoleService->assign(new AssignAppsToRoleData( roleId: $this->roleId, validUpto: $this->assignForm->isUnlimited ? now()->addYears(15) : $this->assignForm->validUpto, 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->assignForm->validate(); $this->appRoleService->assign(new AssignAppsToRoleData( roleId: $this->roleId, validUpto: $this->assignForm->isUnlimited ? now()->addYears(15) : $this->assignForm->validUpto, appIds: $this->assignForm->appIds, )); $this->assignForm->reset(); $this->showAddAppModal = false; unset($this->apps); }, successMessage: 'Apps updated successfully!', ); } public function detachApp(int $id): void { $this->attempt( action: function () use ($id): void { $this->appRoleService->detach($id); 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.', ); } public function openEditPriorityModal(): void { $this->newPriority = $this->rolePriority; $this->resetErrorBag(); $this->showEditPriorityModal = true; } public function savePriority(): void { $this->validate([ 'newPriority' => 'required|integer|min:0', ]); $this->attempt( action: function (): void { $this->roleService->updatePriority(new UpdateRolePriorityData( roleId: $this->roleId, priority: (int) $this->newPriority, )); $this->rolePriority = (int) $this->newPriority; $this->showEditPriorityModal = false; }, successMessage: 'Role priority updated successfully!', ); } }; ?>
No apps assigned to this role yet.
@endifNo users assigned to this role yet.
@endif