/** * @param string $search */ protected function getGroups($search) { $this->result['groups'] = $this->result['exact']['groups'] = []; $groups = $this->groupManager->search($search, $this->limit, $this->offset); $groups = array_map(function (IGroup $group) { return $group->getGID(); }, $groups); if (sizeof($groups) < $this->limit) { $this->reachedEndFor[] = 'groups'; } $userGroups = []; if (!empty($groups) && $this->shareWithGroupOnly) { // Intersect all the groups that match with the groups this user is a member of $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser()); $userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups); $groups = array_intersect($groups, $userGroups); } foreach ($groups as $gid) { if (strtolower($gid) === $search) { $this->result['exact']['groups'][] = ['label' => $search, 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => $search]]; } else { $this->result['groups'][] = ['label' => $gid, 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => $gid]]; } } if ($this->offset === 0 && empty($this->result['exact']['groups'])) { // On page one we try if the search result has a direct hit on the // user id and if so, we add that to the exact match list $group = $this->groupManager->get($search); if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) { array_push($this->result['exact']['groups'], ['label' => $group->getGID(), 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => $group->getGID()]]); } } }
/** * Returns the list of groups a principal is a member of * * @param string $principal * @param bool $needGroups * @return array * @throws Exception */ public function getGroupMembership($principal, $needGroups = false) { list($prefix, $name) = URLUtil::splitPath($principal); if ($prefix === $this->principalPrefix) { $user = $this->userManager->get($name); if (!$user) { throw new Exception('Principal not found'); } if ($this->hasGroups || $needGroups) { $groups = $this->groupManager->getUserGroups($user); $groups = array_map(function ($group) { /** @var IGroup $group */ return 'principals/groups/' . $group->getGID(); }, $groups); return $groups; } } return []; }
/** * Returns the list of groups a principal is a member of * * @param string $principal * @return array * @throws Exception */ public function getGroupMembership($principal) { list($prefix, $name) = URLUtil::splitPath($principal); if ($prefix === 'principals/users') { $user = $this->userManager->get($name); if (!$user) { throw new Exception('Principal not found'); } $groups = $this->groupManager->getUserGroups($user); $groups = array_map(function ($group) { /** @var IGroup $group */ return 'principals/groups/' . $group->getGID(); }, $groups); $groups[] = 'principals/users/' . $name . '/calendar-proxy-read'; $groups[] = 'principals/users/' . $name . '/calendar-proxy-write'; return $groups; } return []; }
/** * @inheritdoc */ public function getSharedWith($userId, $shareType, $node, $limit, $offset) { /** @var Share[] $shares */ $shares = []; if ($shareType === \OCP\Share::SHARE_TYPE_USER) { //Get shares directly with this user $qb = $this->dbConn->getQueryBuilder(); $qb->select('*')->from('share'); // Order by id $qb->orderBy('id'); // Set limit and offset if ($limit !== -1) { $qb->setMaxResults($limit); } $qb->setFirstResult($offset); $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))); // Filter by node if provided if ($node !== null) { $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); } $cursor = $qb->execute(); while ($data = $cursor->fetch()) { $shares[] = $this->createShare($data); } $cursor->closeCursor(); } else { if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { $user = $this->userManager->get($userId); $allGroups = $this->groupManager->getUserGroups($user); /** @var Share[] $shares2 */ $shares2 = []; $start = 0; while (true) { $groups = array_slice($allGroups, $start, 100); $start += 100; if ($groups === []) { break; } $qb = $this->dbConn->getQueryBuilder(); $qb->select('*')->from('share')->orderBy('id')->setFirstResult(0); if ($limit !== -1) { $qb->setMaxResults($limit - count($shares)); } // Filter by node if provided if ($node !== null) { $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); } $groups = array_map(function (IGroup $group) { return $group->getGID(); }, $groups); $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))); $qb->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))); $cursor = $qb->execute(); while ($data = $cursor->fetch()) { if ($offset > 0) { $offset--; continue; } $shares2[] = $this->createShare($data); } $cursor->closeCursor(); } /* * Resolve all group shares to user specific shares * TODO: Optmize this! */ foreach ($shares2 as $share) { $shares[] = $this->resolveGroupShare($share, $userId); } } else { throw new BackendError('Invalid backend'); } } return $shares; }