Provide formal Users/Projects/Mailable fields for EditEngine
Summary: Ref T9132. This allows you to prefill EditEngine forms with stuff like `?subscribers=epriestley`, and we'll figure out what you mean.
Test Plan:
- Did `/?subscribers=...` with various values (good, bad, mis-capitalized).
- Did `/?projects=...` with various values (good, bad, mis-capitalized).
- Reviewed documentation.
- Reviewed {nav Config > HTTP Parameter Types}.
Reviewers: chad
Reviewed By: chad
Maniphest Tasks: T9132
Differential Revision: https://secure.phabricator.com/D14404
This commit is contained in:
46
src/applications/phid/resolver/PhabricatorPHIDResolver.php
Normal file
46
src/applications/phid/resolver/PhabricatorPHIDResolver.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Resolve a list of identifiers into PHIDs.
|
||||
*
|
||||
* This class simplifies the process of convering a list of mixed token types
|
||||
* (like some PHIDs and some usernames) into a list of just PHIDs.
|
||||
*/
|
||||
abstract class PhabricatorPHIDResolver extends Phobject {
|
||||
|
||||
private $viewer;
|
||||
|
||||
final public function setViewer(PhabricatorUser $viewer) {
|
||||
$this->viewer = $viewer;
|
||||
return $this;
|
||||
}
|
||||
|
||||
final public function getViewer() {
|
||||
return $this->viewer;
|
||||
}
|
||||
|
||||
final public function resolvePHIDs(array $phids) {
|
||||
$type_unknown = PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN;
|
||||
|
||||
$names = array();
|
||||
foreach ($phids as $key => $phid) {
|
||||
if (phid_get_type($phid) == $type_unknown) {
|
||||
$names[$key] = $phid;
|
||||
}
|
||||
}
|
||||
|
||||
if ($names) {
|
||||
$map = $this->getResolutionMap($names);
|
||||
foreach ($names as $key => $name) {
|
||||
if (isset($map[$name])) {
|
||||
$phids[$key] = $map[$name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $phids;
|
||||
}
|
||||
|
||||
abstract protected function getResolutionMap(array $names);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
final class PhabricatorProjectPHIDResolver
|
||||
extends PhabricatorPHIDResolver {
|
||||
|
||||
protected function getResolutionMap(array $names) {
|
||||
// This is a little awkward but we want to pick up the normalization
|
||||
// rules from the PHIDType. This flow could perhaps be made cleaner.
|
||||
|
||||
foreach ($names as $key => $name) {
|
||||
$names[$key] = '#'.$name;
|
||||
}
|
||||
|
||||
$query = id(new PhabricatorObjectQuery())
|
||||
->setViewer($this->getViewer());
|
||||
|
||||
$projects = id(new PhabricatorProjectProjectPHIDType())
|
||||
->loadNamedObjects($query, $names);
|
||||
|
||||
$results = array();
|
||||
foreach ($projects as $hashtag => $project) {
|
||||
$results[substr($hashtag, 1)] = $project->getPHID();
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
final class PhabricatorUserPHIDResolver
|
||||
extends PhabricatorPHIDResolver {
|
||||
|
||||
protected function getResolutionMap(array $names) {
|
||||
// Pick up the normalization and case rules from the PHID type query.
|
||||
|
||||
foreach ($names as $key => $name) {
|
||||
$names[$key] = '@'.$name;
|
||||
}
|
||||
|
||||
$query = id(new PhabricatorObjectQuery())
|
||||
->setViewer($this->getViewer());
|
||||
|
||||
$users = id(new PhabricatorPeopleUserPHIDType())
|
||||
->loadNamedObjects($query, $names);
|
||||
|
||||
$results = array();
|
||||
foreach ($users as $at_username => $user) {
|
||||
$results[substr($at_username, 1)] = $user->getPHID();
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user