public function withIdentifiers(array $identifiers)
 {
     $ids = array();
     $callsigns = array();
     $phids = array();
     $monograms = array();
     foreach ($identifiers as $identifier) {
         if (ctype_digit($identifier)) {
             $ids[$identifier] = $identifier;
         } else {
             if (preg_match('/^(r[A-Z]+)|(R[1-9]\\d*)\\z/', $identifier)) {
                 $monograms[$identifier] = $identifier;
             } else {
                 $repository_type = PhabricatorRepositoryRepositoryPHIDType::TYPECONST;
                 if (phid_get_type($identifier) === $repository_type) {
                     $phids[$identifier] = $identifier;
                 } else {
                     $callsigns[$identifier] = $identifier;
                 }
             }
         }
     }
     $this->numericIdentifiers = $ids;
     $this->callsignIdentifiers = $callsigns;
     $this->phidIdentifiers = $phids;
     $this->monogramIdentifiers = $monograms;
     return $this;
 }
 protected function execute(ConduitAPIRequest $request)
 {
     $user = $request->getUser()->getPHID();
     $phid = $request->getValue('objectPHID');
     $new = false;
     $flag = id(new PhabricatorFlag())->loadOneWhere('objectPHID = %s AND ownerPHID = %s', $phid, $user);
     if ($flag) {
         $params = $request->getAllParameters();
         if (isset($params['color'])) {
             $flag->setColor($params['color']);
         }
         if (isset($params['note'])) {
             $flag->setNote($params['note']);
         }
     } else {
         $default_color = PhabricatorFlagColor::COLOR_BLUE;
         $flag = id(new PhabricatorFlag())->setOwnerPHID($user)->setType(phid_get_type($phid))->setObjectPHID($phid)->setReasonPHID($user)->setColor($request->getValue('color', $default_color))->setNote($request->getValue('note', ''));
         $new = true;
     }
     $this->attachHandleToFlag($flag, $request->getUser());
     $flag->save();
     $ret = $this->buildFlagInfoDictionary($flag);
     $ret['new'] = $new;
     return $ret;
 }
 public function markupHandle(array $matches)
 {
     $engine = $this->getEngine();
     $viewer = $engine->getConfig('viewer');
     if (!$this->isFlatText($matches[0])) {
         return $matches[0];
     }
     $phid_type = phid_get_type($matches[1]);
     if ($phid_type == PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) {
         return $matches[0];
     }
     $token = $engine->storeText($matches[0]);
     if ($engine->isTextMode()) {
         return $token;
     }
     $original_key = self::KEY_RULE_HANDLE_ORIGINAL;
     $original = $engine->getTextMetadata($original_key, array());
     $original[$token] = $matches[0];
     $engine->setTextMetadata($original_key, $original);
     $metadata_key = self::KEY_RULE_HANDLE;
     $metadata = $engine->getTextMetadata($metadata_key, array());
     $phid = $matches[1];
     if (empty($metadata[$phid])) {
         $metadata[$phid] = array();
     }
     $metadata[$phid][] = $token;
     $engine->setTextMetadata($metadata_key, $metadata);
     return $token;
 }
 public function renderView()
 {
     $view = $this->newStoryView();
     $handle = $this->getHandle($this->getPrimaryObjectPHID());
     $view->setHref($handle->getURI());
     $type = phid_get_type($handle->getPHID());
     $phid_types = PhabricatorPHIDType::getAllTypes();
     $icon = null;
     if (!empty($phid_types[$type])) {
         $phid_type = $phid_types[$type];
         $class = $phid_type->getPHIDTypeApplicationClass();
         if ($class) {
             $application = PhabricatorApplication::getByClass($class);
             $icon = $application->getIcon();
         }
     }
     $view->setAppIcon($icon);
     $xaction_phids = $this->getValue('transactionPHIDs');
     $xaction = $this->getPrimaryTransaction();
     $xaction->setHandles($this->getHandles());
     $view->setTitle($xaction->getTitleForFeed());
     foreach ($xaction_phids as $xaction_phid) {
         $secondary_xaction = $this->getObject($xaction_phid);
         $secondary_xaction->setHandles($this->getHandles());
         $body = $secondary_xaction->getBodyForFeed($this);
         if (nonempty($body)) {
             $view->appendChild($body);
         }
     }
     $view->setImage($this->getHandle($xaction->getAuthorPHID())->getImageURI());
     return $view;
 }
 public static function loadAffiliatedUserPHIDs(array $package_ids)
 {
     if (!$package_ids) {
         return array();
     }
     $owners = id(new PhabricatorOwnersOwner())->loadAllWhere('packageID IN (%Ls)', $package_ids);
     $type_user = PhabricatorPeopleUserPHIDType::TYPECONST;
     $type_project = PhabricatorProjectProjectPHIDType::TYPECONST;
     $user_phids = array();
     $project_phids = array();
     foreach ($owners as $owner) {
         $owner_phid = $owner->getUserPHID();
         switch (phid_get_type($owner_phid)) {
             case PhabricatorPeopleUserPHIDType::TYPECONST:
                 $user_phids[] = $owner_phid;
                 break;
             case PhabricatorProjectProjectPHIDType::TYPECONST:
                 $project_phids[] = $owner_phid;
                 break;
         }
     }
     if ($project_phids) {
         $projects = id(new PhabricatorProjectQuery())->setViewer(PhabricatorUser::getOmnipotentUser())->withPHIDs($project_phids)->needMembers(true)->execute();
         foreach ($projects as $project) {
             foreach ($project->getMemberPHIDs() as $member_phid) {
                 $user_phids[] = $member_phid;
             }
         }
     }
     $user_phids = array_fuse($user_phids);
     return array_values($user_phids);
 }
 public function execute()
 {
     $phids = array_fuse($this->phids);
     $actors = array();
     $type_map = array();
     foreach ($phids as $phid) {
         $type_map[phid_get_type($phid)][] = $phid;
     }
     // TODO: Generalize this somewhere else.
     $results = array();
     foreach ($type_map as $type => $phids) {
         switch ($type) {
             case PhabricatorProjectProjectPHIDType::TYPECONST:
                 // NOTE: We're loading the projects here in order to respect policies.
                 $projects = id(new PhabricatorProjectQuery())->setViewer($this->getViewer())->withPHIDs($phids)->execute();
                 $subscribers = id(new PhabricatorSubscribersQuery())->withObjectPHIDs($phids)->execute();
                 $projects = mpull($projects, null, 'getPHID');
                 foreach ($phids as $phid) {
                     $project = idx($projects, $phid);
                     if (!$project) {
                         $results[$phid] = array();
                     } else {
                         $results[$phid] = idx($subscribers, $phid, array());
                     }
                 }
                 break;
             default:
                 break;
         }
     }
     return $results;
 }
 protected function getValueFromRequest(AphrontRequest $request, $key)
 {
     $list = $this->getListFromRequest($request, $key);
     $phids = array();
     $slugs = array();
     $project_type = PhabricatorProjectProjectPHIDType::TYPECONST;
     foreach ($list as $item) {
         $type = phid_get_type($item);
         if ($type == $project_type) {
             $phids[] = $item;
         } else {
             if (PhabricatorTypeaheadDatasource::isFunctionToken($item)) {
                 // If this is a function, pass it through unchanged; we'll evaluate
                 // it later.
                 $phids[] = $item;
             } else {
                 $slugs[] = $item;
             }
         }
     }
     if ($slugs) {
         $projects = id(new PhabricatorProjectQuery())->setViewer($this->requireViewer())->withSlugs($slugs)->execute();
         foreach ($projects as $project) {
             $phids[] = $project->getPHID();
         }
         $phids = array_unique($phids);
     }
     return $phids;
 }
 public function execute()
 {
     $phids = array_fuse($this->phids);
     $actors = array();
     $type_map = array();
     foreach ($phids as $phid) {
         $type_map[phid_get_type($phid)][] = $phid;
         $actors[$phid] = id(new PhabricatorMetaMTAActor())->setPHID($phid);
     }
     // TODO: Move this to PhabricatorPHIDType, or the objects, or some
     // interface.
     foreach ($type_map as $type => $phids) {
         switch ($type) {
             case PhabricatorPeopleUserPHIDType::TYPECONST:
                 $this->loadUserActors($actors, $phids);
                 break;
             case PhabricatorPeopleExternalPHIDType::TYPECONST:
                 $this->loadExternalUserActors($actors, $phids);
                 break;
             default:
                 $this->loadUnknownActors($actors, $phids);
                 break;
         }
     }
     return $actors;
 }
 protected function loadPropertyObject()
 {
     $viewer = $this->getViewer();
     $request = $this->getRequest();
     $object_phid = $request->getStr('objectPHID');
     switch (phid_get_type($object_phid)) {
         case AlmanacBindingPHIDType::TYPECONST:
             $query = new AlmanacBindingQuery();
             break;
         case AlmanacDevicePHIDType::TYPECONST:
             $query = new AlmanacDeviceQuery();
             break;
         case AlmanacServicePHIDType::TYPECONST:
             $query = new AlmanacServiceQuery();
             break;
         default:
             return new Aphront404Response();
     }
     $object = $query->setViewer($viewer)->withPHIDs(array($object_phid))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->needProperties(true)->executeOne();
     if (!$object) {
         return new Aphront404Response();
     }
     if (!$object instanceof AlmanacPropertyInterface) {
         return new Aphront404Response();
     }
     $this->propertyObject = $object;
     return null;
 }
 public function withIdentifiers(array $identifiers)
 {
     $identifiers = array_fuse($identifiers);
     $ids = array();
     $callsigns = array();
     $phids = array();
     $monograms = array();
     $slugs = array();
     foreach ($identifiers as $identifier) {
         if (ctype_digit((string) $identifier)) {
             $ids[$identifier] = $identifier;
             continue;
         }
         if (preg_match('/^(r[A-Z]+|R[1-9]\\d*)\\z/', $identifier)) {
             $monograms[$identifier] = $identifier;
             continue;
         }
         $repository_type = PhabricatorRepositoryRepositoryPHIDType::TYPECONST;
         if (phid_get_type($identifier) === $repository_type) {
             $phids[$identifier] = $identifier;
             continue;
         }
         if (preg_match('/^[A-Z]+\\z/', $identifier)) {
             $callsigns[$identifier] = $identifier;
             continue;
         }
         $slugs[$identifier] = $identifier;
     }
     $this->numericIdentifiers = $ids;
     $this->callsignIdentifiers = $callsigns;
     $this->phidIdentifiers = $phids;
     $this->monogramIdentifiers = $monograms;
     $this->slugIdentifiers = $slugs;
     return $this;
 }
 private function resolvePHIDs(array $phids)
 {
     // If we have a function like `projects(alincoln)`, try to resolve the
     // username first. This won't happen normally, but can be passed in from
     // the query string.
     // The user might also give us an invalid username. In this case, we
     // preserve it and return it in-place so we get an "invalid" token rendered
     // in the UI. This shows the user where the issue is and  best represents
     // the user's input.
     $usernames = array();
     foreach ($phids as $key => $phid) {
         if (phid_get_type($phid) != PhabricatorPeopleUserPHIDType::TYPECONST) {
             $usernames[$key] = $phid;
         }
     }
     if ($usernames) {
         $users = id(new PhabricatorPeopleQuery())->setViewer($this->getViewer())->withUsernames($usernames)->execute();
         $users = mpull($users, null, 'getUsername');
         foreach ($usernames as $key => $username) {
             $user = idx($users, $username);
             if ($user) {
                 $phids[$key] = $user->getPHID();
             }
         }
     }
     return $phids;
 }
 protected function getUsersFromRequest(AphrontRequest $request, $key, array $allow_types = array())
 {
     $list = $this->getListFromRequest($request, $key);
     $phids = array();
     $names = array();
     $allow_types = array_fuse($allow_types);
     $user_type = PhabricatorPeopleUserPHIDType::TYPECONST;
     foreach ($list as $item) {
         $type = phid_get_type($item);
         if ($type == $user_type) {
             $phids[] = $item;
         } else {
             if (isset($allow_types[$type])) {
                 $phids[] = $item;
             } else {
                 if (PhabricatorTypeaheadDatasource::isFunctionToken($item)) {
                     // If this is a function, pass it through unchanged; we'll evaluate
                     // it later.
                     $phids[] = $item;
                 } else {
                     $names[] = $item;
                 }
             }
         }
     }
     if ($names) {
         $users = id(new PhabricatorPeopleQuery())->setViewer($this->getViewer())->withUsernames($names)->execute();
         foreach ($users as $user) {
             $phids[] = $user->getPHID();
         }
         $phids = array_unique($phids);
     }
     return $phids;
 }
 protected function loadITem(PhutilArgumentParser $argv, $key)
 {
     $item = $argv->getArg($key);
     if (!strlen($item)) {
         throw new PhutilArgumentUsageException(pht('Specify a item with %s.', '--' . $key));
     }
     $query = id(new NuanceItemQuery())->setViewer($this->getViewer())->setRaisePolicyExceptions(true);
     $type_unknown = PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN;
     if (ctype_digit($item)) {
         $kind = 'id';
         $query->withIDs(array($item));
     } else {
         if (phid_get_type($item) !== $type_unknown) {
             $kind = 'phid';
             $query->withPHIDs($item);
         } else {
             throw new PhutilArgumentUsageException(pht('Specify the ID or PHID of an item to update. Parameter "%s" ' . 'is not an ID or PHID.', $item));
         }
     }
     $items = $query->execute();
     if (!$items) {
         switch ($kind) {
             case 'id':
                 $message = pht('No item exists with ID "%s".', $item);
                 break;
             case 'phid':
                 $message = pht('No item exists with PHID "%s".', $item);
                 break;
         }
         throw new PhutilArgumentUsageException($message);
     }
     return head($items);
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $phid = $request->getURIData('phid');
     $action = $request->getURIData('action');
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     switch ($action) {
         case 'add':
             $is_add = true;
             break;
         case 'delete':
             $is_add = false;
             break;
         default:
             return new Aphront400Response();
     }
     $handle = id(new PhabricatorHandleQuery())->setViewer($viewer)->withPHIDs(array($phid))->executeOne();
     if (phid_get_type($phid) == PhabricatorProjectProjectPHIDType::TYPECONST) {
         // TODO: This is a big hack, but a weak argument for adding some kind
         // of "load for role" feature to ObjectQuery, and also not a really great
         // argument for adding some kind of "load extra stuff" feature to
         // SubscriberInterface. Do this for now and wait for the best way forward
         // to become more clear?
         $object = id(new PhabricatorProjectQuery())->setViewer($viewer)->withPHIDs(array($phid))->needWatchers(true)->executeOne();
     } else {
         $object = id(new PhabricatorObjectQuery())->setViewer($viewer)->withPHIDs(array($phid))->executeOne();
     }
     if (!$object instanceof PhabricatorSubscribableInterface) {
         return $this->buildErrorResponse(pht('Bad Object'), pht('This object is not subscribable.'), $handle->getURI());
     }
     if ($object->isAutomaticallySubscribed($viewer->getPHID())) {
         return $this->buildErrorResponse(pht('Automatically Subscribed'), pht('You are automatically subscribed to this object.'), $handle->getURI());
     }
     if ($object instanceof PhabricatorApplicationTransactionInterface) {
         if ($is_add) {
             $xaction_value = array('+' => array($viewer->getPHID()));
         } else {
             $xaction_value = array('-' => array($viewer->getPHID()));
         }
         $xaction = id($object->getApplicationTransactionTemplate())->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)->setNewValue($xaction_value);
         $editor = id($object->getApplicationTransactionEditor())->setActor($viewer)->setContinueOnNoEffect(true)->setContinueOnMissingFields(true)->setContentSourceFromRequest($request);
         $editor->applyTransactions($object->getApplicationTransactionObject(), array($xaction));
     } else {
         // TODO: Eventually, get rid of this once everything implements
         // PhabriatorApplicationTransactionInterface.
         $editor = id(new PhabricatorSubscriptionsEditor())->setActor($viewer)->setObject($object);
         if ($is_add) {
             $editor->subscribeExplicit(array($viewer->getPHID()), $explicit = true);
         } else {
             $editor->unsubscribe(array($viewer->getPHID()));
         }
         $editor->save();
     }
     // TODO: We should just render the "Unsubscribe" action and swap it out
     // in the document for Ajax requests.
     return id(new AphrontReloadResponse())->setURI($handle->getURI());
 }
 public function getAttachmentForObject($object, $data, $spec)
 {
     $uris = array();
     foreach ($object->getURIs() as $uri) {
         $uris[] = array('id' => $uri->getID(), 'type' => phid_get_type($uri->getPHID()), 'phid' => $uri->getPHID(), 'fields' => $uri->getFieldValuesForConduit());
     }
     return array('uris' => $uris);
 }
Example #16
0
/**
 * Group a list of phids by type.
 *
 * @param   phids array of phids
 * @return  map of phid type => list of phids
 */
function phid_group_by_type($phids)
{
    $result = array();
    foreach ($phids as $phid) {
        $type = phid_get_type($phid);
        $result[$type][] = $phid;
    }
    return $result;
}
 public function computeRawFactsForObject(PhabricatorLiskDAO $object)
 {
     $facts = array();
     $phid = $object->getPHID();
     $type = phid_get_type($phid);
     foreach (array('N:*', 'N:' . $type) as $fact_type) {
         $facts[] = id(new PhabricatorFactRaw())->setFactType($fact_type)->setObjectPHID($phid)->setValueX(1)->setEpoch($object->getDateCreated());
     }
     return $facts;
 }
 public function loadObjects()
 {
     $types = array();
     foreach ($this->phids as $phid) {
         $type = phid_get_type($phid);
         $types[$type][] = $phid;
     }
     $objects = array();
     foreach ($types as $type => $phids) {
         switch ($type) {
             case PhabricatorPHIDConstants::PHID_TYPE_USER:
                 $user_dao = new PhabricatorUser();
                 $users = $user_dao->loadAllWhere('phid in (%Ls)', $phids);
                 foreach ($users as $user) {
                     $objects[$user->getPHID()] = $user;
                 }
                 break;
             case PhabricatorPHIDConstants::PHID_TYPE_CMIT:
                 $commit_dao = new PhabricatorRepositoryCommit();
                 $commits = $commit_dao->loadAllWhere('phid IN (%Ls)', $phids);
                 $commit_data = array();
                 if ($commits) {
                     $data_dao = new PhabricatorRepositoryCommitData();
                     $commit_data = $data_dao->loadAllWhere('commitID IN (%Ld)', mpull($commits, 'getID'));
                     $commit_data = mpull($commit_data, null, 'getCommitID');
                 }
                 foreach ($commits as $commit) {
                     $data = idx($commit_data, $commit->getID());
                     if ($data) {
                         $commit->attachCommitData($data);
                         $objects[$commit->getPHID()] = $commit;
                     } else {
                         // If we couldn't load the commit data, just act as though we
                         // couldn't load the object at all so we don't load half an object.
                     }
                 }
                 break;
             case PhabricatorPHIDConstants::PHID_TYPE_TASK:
                 $task_dao = new ManiphestTask();
                 $tasks = $task_dao->loadAllWhere('phid IN (%Ls)', $phids);
                 foreach ($tasks as $task) {
                     $objects[$task->getPHID()] = $task;
                 }
                 break;
             case PhabricatorPHIDConstants::PHID_TYPE_DREV:
                 $revision_dao = new DifferentialRevision();
                 $revisions = $revision_dao->loadAllWhere('phid IN (%Ls)', $phids);
                 foreach ($revisions as $revision) {
                     $objects[$revision->getPHID()] = $revision;
                 }
                 break;
         }
     }
     return $objects;
 }
 public function handleEvent(PhutilEvent $event)
 {
     $phid = $event->getValue('phid');
     $type = phid_get_type($phid);
     // For now, we only index projects.
     if ($type != PhabricatorProjectProjectPHIDType::TYPECONST) {
         return;
     }
     $document = $event->getValue('document');
     ManiphestNameIndex::updateIndex($phid, $document->getDocumentTitle());
 }
 public function applyEffect($object, HeraldEffect $effect)
 {
     $phid = $this->getAdapter()->getPHID();
     $rule = $effect->getRule();
     $author = $rule->getAuthor();
     $flag = PhabricatorFlagQuery::loadUserFlag($author, $phid);
     if ($flag) {
         $this->logEffect(self::DO_IGNORE, $flag->getColor());
         return;
     }
     $flag = id(new PhabricatorFlag())->setOwnerPHID($author->getPHID())->setType(phid_get_type($phid))->setObjectPHID($phid)->setReasonPHID($rule->getPHID())->setColor($effect->getTarget())->setNote('')->save();
     $this->logEffect(self::DO_FLAG, $flag->getColor());
 }
 protected static function queryByOwner($owner)
 {
     $is_valid_phid = phid_get_type($owner) == PhabricatorPeopleUserPHIDType::TYPECONST || phid_get_type($owner) == PhabricatorProjectProjectPHIDType::TYPECONST;
     if (!$is_valid_phid) {
         throw id(new ConduitException('ERR-INVALID-PARAMETER'))->setErrorDescription(pht('Expected user/project PHID for owner, got %s.', $owner));
     }
     $owners = id(new PhabricatorOwnersOwner())->loadAllWhere('userPHID = %s', $owner);
     $package_ids = mpull($owners, 'getPackageID');
     $packages = array();
     foreach ($package_ids as $id) {
         $packages[] = id(new PhabricatorOwnersPackage())->load($id);
     }
     return $packages;
 }
 protected static function queryByAffiliatedUser($owner)
 {
     $is_valid_phid = phid_get_type($owner) == PhabricatorPHIDConstants::PHID_TYPE_USER;
     if (!$is_valid_phid) {
         throw id(new ConduitException('ERR-INVALID-PARAMETER'))->setErrorDescription('Expected user PHID for affiliation, got ' . $owner);
     }
     $owners = PhabricatorOwnersOwner::loadAffiliatedPackages($owner);
     $package_ids = mpull($owners, 'getPackageID');
     $packages = array();
     foreach ($package_ids as $id) {
         $packages[] = id(new PhabricatorOwnersPackage())->load($id);
     }
     return $packages;
 }
 public function execute()
 {
     $phids = array_fuse($this->phids);
     $actors = array();
     $type_map = array();
     foreach ($phids as $phid) {
         $type_map[phid_get_type($phid)][] = $phid;
     }
     // TODO: Generalize this somewhere else.
     $results = array();
     foreach ($type_map as $type => $phids) {
         switch ($type) {
             case PhabricatorProjectProjectPHIDType::TYPECONST:
                 // NOTE: We're loading the projects here in order to respect policies.
                 $projects = id(new PhabricatorProjectQuery())->setViewer($this->getViewer())->withPHIDs($phids)->needMembers(true)->needWatchers(true)->execute();
                 $edge_type = PhabricatorProjectSilencedEdgeType::EDGECONST;
                 $edge_query = id(new PhabricatorEdgeQuery())->withSourcePHIDs($phids)->withEdgeTypes(array($edge_type));
                 $edge_query->execute();
                 $projects = mpull($projects, null, 'getPHID');
                 foreach ($phids as $phid) {
                     $project = idx($projects, $phid);
                     if (!$project) {
                         $results[$phid] = array();
                         continue;
                     }
                     // Recipients are members who haven't silenced the project, plus
                     // watchers.
                     $members = $project->getMemberPHIDs();
                     $members = array_fuse($members);
                     $watchers = $project->getWatcherPHIDs();
                     $watchers = array_fuse($watchers);
                     $silenced = $edge_query->getDestinationPHIDs(array($phid), array($edge_type));
                     $silenced = array_fuse($silenced);
                     $result_map = array_diff_key($members, $silenced);
                     $result_map = $result_map + $watchers;
                     $results[$phid] = array_values($result_map);
                 }
                 break;
             default:
                 // For other types, just map the PHID to itself without modification.
                 // This allows callers to do less work.
                 foreach ($phids as $phid) {
                     $results[$phid] = array($phid);
                 }
                 break;
         }
     }
     return $results;
 }
 protected function loadPage()
 {
     $types = PhabricatorPHIDType::getAllTypes();
     $phids = array_unique($this->phids);
     if (!$phids) {
         return array();
     }
     $object_query = id(new PhabricatorObjectQuery())->withPHIDs($phids)->setParentQuery($this)->requireCapabilities($this->getRequiredObjectCapabilities())->setViewer($this->getViewer());
     // We never want the subquery to raise policy exceptions, even if this
     // query is being executed via executeOne(). Policy exceptions are not
     // meaningful or relevant for handles, which load in an "Unknown" or
     // "Restricted" state after encountering a policy violation.
     $object_query->setRaisePolicyExceptions(false);
     $objects = $object_query->execute();
     $filtered = $object_query->getPolicyFilteredPHIDs();
     $groups = array();
     foreach ($phids as $phid) {
         $type = phid_get_type($phid);
         $groups[$type][] = $phid;
     }
     $results = array();
     foreach ($groups as $type => $phid_group) {
         $handles = array();
         foreach ($phid_group as $key => $phid) {
             if (isset($handles[$phid])) {
                 unset($phid_group[$key]);
                 // The input had a duplicate PHID; just skip it.
                 continue;
             }
             $handles[$phid] = id(new PhabricatorObjectHandle())->setType($type)->setPHID($phid);
             if (isset($objects[$phid])) {
                 $handles[$phid]->setComplete(true);
             } else {
                 if (isset($filtered[$phid])) {
                     $handles[$phid]->setPolicyFiltered(true);
                 }
             }
         }
         if (isset($types[$type])) {
             $type_objects = array_select_keys($objects, $phid_group);
             if ($type_objects) {
                 $have_object_phids = array_keys($type_objects);
                 $types[$type]->loadHandles($this, array_select_keys($handles, $have_object_phids), $type_objects);
             }
         }
         $results += $handles;
     }
     return $results;
 }
 public function loadPage()
 {
     if ($this->object && $this->phids) {
         throw new Exception('You can not issue a policy query with both setObject() and ' . 'setPHIDs().');
     } else {
         if ($this->object) {
             $phids = $this->loadObjectPolicyPHIDs();
         } else {
             $phids = $this->phids;
         }
     }
     $phids = array_fuse($phids);
     $results = array();
     // First, load global policies.
     foreach ($this->getGlobalPolicies() as $phid => $policy) {
         if (isset($phids[$phid])) {
             $results[$phid] = $policy;
             unset($phids[$phid]);
         }
     }
     // If we still need policies, we're going to have to fetch data. Bucket
     // the remaining policies into rule-based policies and handle-based
     // policies.
     if ($phids) {
         $rule_policies = array();
         $handle_policies = array();
         foreach ($phids as $phid) {
             $phid_type = phid_get_type($phid);
             if ($phid_type == PhabricatorPolicyPHIDTypePolicy::TYPECONST) {
                 $rule_policies[$phid] = $phid;
             } else {
                 $handle_policies[$phid] = $phid;
             }
         }
         if ($handle_policies) {
             $handles = id(new PhabricatorHandleQuery())->setViewer($this->getViewer())->withPHIDs($handle_policies)->execute();
             foreach ($handle_policies as $phid) {
                 $results[$phid] = PhabricatorPolicy::newFromPolicyAndHandle($phid, $handles[$phid]);
             }
         }
         if ($rule_policies) {
             $rules = id(new PhabricatorPolicy())->loadAllWhere('phid IN (%Ls)', $rule_policies);
             $results += mpull($rules, null, 'getPHID');
         }
     }
     $results = msort($results, 'getSortKey');
     return $results;
 }
 private function loadPHIDsByTypes($type)
 {
     $indexers = id(new PhutilClassMapQuery())->setAncestorClass('PhabricatorSearchDocumentIndexer')->execute();
     $phids = array();
     foreach ($indexers as $indexer) {
         $indexer_phid = $indexer->getIndexableObject()->generatePHID();
         $indexer_type = phid_get_type($indexer_phid);
         if ($type && strcasecmp($indexer_type, $type)) {
             continue;
         }
         $iterator = $indexer->getIndexIterator();
         foreach ($iterator as $object) {
             $phids[] = $object->getPHID();
         }
     }
     return $phids;
 }
 public static function expandInvitees(PhabricatorUser $viewer, array $values)
 {
     $phids = array();
     foreach ($values as $value) {
         if (phid_get_type($value) == PhabricatorPeopleUserPHIDType::TYPECONST) {
             $phids[] = $value;
         }
     }
     if (!$phids) {
         return $values;
     }
     $projects = id(new PhabricatorProjectQuery())->setViewer($viewer)->withMemberPHIDs($phids)->execute();
     foreach ($projects as $project) {
         $values[] = $project->getPHID();
     }
     return $values;
 }
 public function validateOption(PhabricatorConfigOption $option, $value)
 {
     if (phid_get_type($value) != PhabricatorFileFilePHIDType::TYPECONST) {
         throw new Exception(pht('%s is not a valid file PHID.', $value));
     }
     $file = id(new PhabricatorFileQuery())->setViewer(PhabricatorUser::getOmnipotentUser())->withPHIDs(array($value))->executeOne();
     if (!$file) {
         throw new Exception(pht('%s is not a valid file PHID.', $value));
     }
     $most_open_policy = PhabricatorPolicies::getMostOpenPolicy();
     if ($file->getViewPolicy() != $most_open_policy) {
         throw new Exception(pht('Specified file %s has policy "%s" but should have policy "%s".', $value, $file->getViewPolicy(), $most_open_policy));
     }
     if (!$file->isViewableImage()) {
         throw new Exception(pht('Specified file %s is not a viewable image.', $value));
     }
 }
Example #29
0
 public function renderConditionValue(PhabricatorUser $viewer, $value)
 {
     // TODO: While this is less of a mess than it used to be, it would still
     // be nice to push this down into individual fields better eventually and
     // stop guessing which values are PHIDs and which aren't.
     if (!is_array($value)) {
         return $value;
     }
     $type_unknown = PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN;
     foreach ($value as $key => $val) {
         if (is_string($val)) {
             if (phid_get_type($val) !== $type_unknown) {
                 $value[$key] = $viewer->renderHandle($val);
             }
         }
     }
     return phutil_implode_html(', ', $value);
 }
 public function loadPage()
 {
     $types = PhabricatorPHIDType::getAllTypes();
     $phids = array_unique($this->phids);
     if (!$phids) {
         return array();
     }
     $object_query = id(new PhabricatorObjectQuery())->withPHIDs($phids)->setViewer($this->getViewer());
     $objects = $object_query->execute();
     $filtered = $object_query->getPolicyFilteredPHIDs();
     $groups = array();
     foreach ($phids as $phid) {
         $type = phid_get_type($phid);
         $groups[$type][] = $phid;
     }
     $results = array();
     foreach ($groups as $type => $phid_group) {
         $handles = array();
         foreach ($phid_group as $key => $phid) {
             if (isset($handles[$phid])) {
                 unset($phid_group[$key]);
                 // The input had a duplicate PHID; just skip it.
                 continue;
             }
             $handles[$phid] = id(new PhabricatorObjectHandle())->setType($type)->setPHID($phid);
             if (isset($objects[$phid])) {
                 $handles[$phid]->setComplete(true);
             } else {
                 if (isset($filtered[$phid])) {
                     $handles[$phid]->setPolicyFiltered(true);
                 }
             }
         }
         if (isset($types[$type])) {
             $type_objects = array_select_keys($objects, $phid_group);
             if ($type_objects) {
                 $have_object_phids = array_keys($type_objects);
                 $types[$type]->loadHandles($this, array_select_keys($handles, $have_object_phids), $type_objects);
             }
         }
         $results += $handles;
     }
     return $results;
 }