/** * Allowed end-points /people/{userId}+/{groupId} /people/{userId}/{groupId}/{optionalPersonId}+ * * examples: /people/john.doe/@all /people/john.doe/@friends /people/john.doe/@self */ public function handleGet(RequestItem $request) { $request->applyUrlTemplate(self::$PEOPLE_PATH); $groupId = $request->getGroup(); $optionalPersonId = $request->getListParameter("personId"); $fields = $request->getFields(self::$DEFAULT_FIELDS); $userIds = $request->getUsers(); // Preconditions if (count($userIds) < 1) { throw new IllegalArgumentException("No userId specified"); } elseif (count($userIds) > 1 && count($optionalPersonId) != 0) { throw new IllegalArgumentException("Cannot fetch personIds for multiple userIds"); } $options = new CollectionOptions(); $options->setSortBy($request->getSortBy()); $options->setSortOrder($request->getSortOrder()); $options->setFilterBy($request->getFilterBy()); $options->setFilterOperation($request->getFilterOperation()); $options->setFilterValue($request->getFilterValue()); $options->setStartIndex($request->getStartIndex()); $options->setCount($request->getCount()); // personId: Array ( [0] => 8 ) if (count($userIds) == 1) { if (count($optionalPersonId) == 0) { if ($groupId->getType() == 'self') { return $this->personService->getPerson($userIds[0], $groupId, $fields, $request->getToken()); } else { return $this->personService->getPeople($userIds, $groupId, $options, $fields, $request->getToken()); } } elseif (count($optionalPersonId) == 1) { return $this->personService->getPerson($optionalPersonId[0], $groupId, $fields, $request->getToken()); } else { $personIds = array(); foreach ($optionalPersonId as $pid) { $personIds[] = new UserId('userId', $pid); } // Every other case is a collection response of optional person ids return $this->personService->getPeople($personIds, new GroupId('self', null), $options, $fields, $request->getToken()); } } // Every other case is a collection response. return $this->personService->getPeople($userIds, $groupId, $options, $fields, $request->getToken()); }
/** * Allowed end-points /people/{userId}+/{groupId} /people/{userId}/{groupId}/{optionalPersonId}+ * * examples: /people/john.doe/@all /people/john.doe/@friends /people/john.doe/@self */ public function handleGet(RequestItem $request) { $this->checkService(); $request->applyUrlTemplate(self::$PEOPLE_PATH); $groupId = $request->getGroup(); $optionalPersonId = $request->getListParameter("personId"); $fields = $request->getFields(self::$DEFAULT_FIELDS); $userIds = $request->getUsers(); // Preconditions if (count($userIds) < 1) { throw new IllegalArgumentException("No userId specified"); } elseif (count($userIds) > 1 && count($optionalPersonId) != 0) { throw new IllegalArgumentException("Cannot fetch personIds for multiple userIds"); } $options = new CollectionOptions(); $options->setSortBy($request->getSortBy()); $options->setSortOrder($request->getSortOrder()); $options->setFilterBy($request->getFilterBy()); $options->setFilterOperation($request->getFilterOperation()); $options->setFilterValue($request->getFilterValue()); $options->setStartIndex($request->getStartIndex()); $options->setCount($request->getCount()); $token = $request->getToken(); $groupType = $groupId->getType(); // handle Anonymous Viewer exceptions $containAnonymousUser = false; if ($token->isAnonymous()) { // Find out whether userIds contains // a) @viewer, b) @me, c) SecurityToken::$ANONYMOUS foreach ($userIds as $key => $id) { if (in_array($id->getType(), self::$ANONYMOUS_ID_TYPE) || $id->getType() == 'userId' && $id->getUserId($token) == SecurityToken::$ANONYMOUS) { $containAnonymousUser = true; unset($userIds[$key]); } } if ($containAnonymousUser) { $userIds = array_values($userIds); // Skip any requests if groupId is not @self or @all, since anonymous viewer won't have friends. if ($groupType != 'self' && $groupType != 'all') { throw new Exception("Can't get friend from an anonymous viewer."); } } } if ($containAnonymousUser && count($userIds) == 0) { return self::$ANONYMOUS_VIEWER; } $service = $this->service; $ret = null; if (count($userIds) == 1) { if (count($optionalPersonId) == 0) { if ($groupType == 'self') { $ret = $service->getPerson($userIds[0], $groupId, $fields, $token); } else { $ret = $service->getPeople($userIds, $groupId, $options, $fields, $token); } } elseif (count($optionalPersonId) == 1) { $ret = $service->getPerson($optionalPersonId[0], $groupId, $fields, $token); } else { $personIds = array(); foreach ($optionalPersonId as $pid) { $personIds[] = new UserId('userId', $pid); } // Every other case is a collection response of optional person ids $ret = $service->getPeople($personIds, new GroupId('self', null), $options, $fields, $token); } } else { // Every other case is a collection response. $ret = $service->getPeople($userIds, $groupId, $options, $fields, $token); } // Append anonymous viewer if ($containAnonymousUser) { if (is_array($ret)) { // Single user $people = array($ret, self::$ANONYMOUS_VIEWER); $ret = new RestfulCollection($people, $options->getStartIndex(), 2); $ret->setItemsPerPage($options->getCount()); } else { // Multiple users $ret->entry[] = self::$ANONYMOUS_VIEWER; $ret->totalResults += 1; } } return $ret; }