$this->isOrganizationFolderAdmin($user, $organizationFolder), 'UPDATE' => $this->isOrganizationFolderAdmin($user, $organizationFolder), 'DELETE' => $this->isOrganizationFolderAdmin($user, $organizationFolder), 'UPDATE_MEMBERS' => $this->isOrganizationFolderAdmin($user, $organizationFolder), 'MANAGE_ALL_RESOURCES' => $this->isOrganizationFolderAdmin($user, $organizationFolder), // At least Manager permissions required 'READ_LIMITED' => $this->isOrganizationFolderAdminOrManager($user, $organizationFolder), 'CREATE_RESOURCE' => $this->isOrganizationFolderAdminOrManager($user, $organizationFolder), 'MANAGE_TOP_LEVEL_RESOURCES' => $this->isOrganizationFolderAdminOrManager($user, $organizationFolder), default => throw new \LogicException('This code should not be reached!') }; } /** * @param IUser $user * @param OrganizationFolder $organizationFolder * @return bool */ private function isOrganizationFolderMember(IUser $user, OrganizationFolder $organizationFolder): bool { $organizationFolderMembers = $this->organizationFolderMemberService->findAll($organizationFolder->getId()); foreach($organizationFolderMembers as $organizationFolderMember) { if($this->userIsPrincipal($user, $organizationFolderMember->getPrincipal())) { return true; } } return false; } /** * @param IUser $user * @param OrganizationFolder $organizationFolder * @return bool */ private function isOrganizationFolderAdmin(IUser $user, OrganizationFolder $organizationFolder): bool { $organizationFolderMembers = $this->organizationFolderMemberService->findAll($organizationFolder->getId(), [ "permissionLevel" => OrganizationFolderMemberPermissionLevel::ADMIN, ]); foreach($organizationFolderMembers as $organizationFolderMember) { // should be true for all returned members because of the filter, double check because of the big security implications if($organizationFolderMember->getPermissionLevel() === OrganizationFolderMemberPermissionLevel::ADMIN->value) { if($this->userIsPrincipal($user, $organizationFolderMember->getPrincipal())) { return true; } } } return false; } /** * @param IUser $user * @param OrganizationFolder $organizationFolder * @return bool */ private function isOrganizationFolderAdminOrManager(IUser $user, OrganizationFolder $organizationFolder): bool { $organizationFolderMembers = $this->organizationFolderMemberService->findAll($organizationFolder->getId()); foreach($organizationFolderMembers as $organizationFolderMember) { $permissionLevel = $organizationFolderMember->getPermissionLevel(); if($permissionLevel === OrganizationFolderMemberPermissionLevel::ADMIN->value || $permissionLevel === OrganizationFolderMemberPermissionLevel::MANAGER->value) { if($this->userIsPrincipal($user, $organizationFolderMember->getPrincipal())) { return true; } } } return false; } private function userIsInGroup(IUser $user, string $groupId): bool { return $this->groupManager->isInGroup($user->getUID(), $groupId); } private function userHasRole(IUser $user, string $organizationProviderId, string $roleId): bool { $organizationProvider = $this->organizationProviderManager->getOrganizationProvider($organizationProviderId); $role = $organizationProvider->getRole($roleId); return $this->userIsInGroup($user, $role->getMembersGroup()); } private function userIsPrincipal(IUser $user, Principal $principal): bool { if($principal->getType() === PrincipalType::GROUP) { return $this->userIsInGroup($user, $principal->getId()); } else if($principal->getType() === PrincipalType::ROLE) { [$organizationProviderId, $roleId] = explode(":", $principal->getId(), 2); return $this->userHasRole($user, $organizationProviderId, $roleId); } else { // user principals are not supported by Organization Folder Members and // a principal object with that type should have never been put into this function return false; } } }