[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/query/ -> DiffusionResolveUserQuery.php (source)

   1  <?php
   2  
   3  /**
   4   * Resolve an author or committer name, like
   5   * `"Abraham Lincoln <[email protected]>"`, into a valid Phabricator user
   6   * account, like `@alincoln`.
   7   */
   8  final class DiffusionResolveUserQuery extends Phobject {
   9  
  10    private $name;
  11    private $commit;
  12  
  13    public function withName($name) {
  14      $this->name = $name;
  15      return $this;
  16    }
  17  
  18    public function withCommit($commit) {
  19      $this->commit = $commit;
  20      return $this;
  21    }
  22  
  23    public function execute() {
  24      $user_name = $this->name;
  25  
  26      $phid = $this->findUserPHID($this->name);
  27      $phid = $this->fireLookupEvent($phid);
  28  
  29      return $phid;
  30    }
  31  
  32    private function findUserPHID($user_name) {
  33      if (!strlen($user_name)) {
  34        return null;
  35      }
  36  
  37      $phid = $this->findUserByUserName($user_name);
  38      if ($phid) {
  39        return $phid;
  40      }
  41  
  42      $phid = $this->findUserByEmailAddress($user_name);
  43      if ($phid) {
  44        return $phid;
  45      }
  46  
  47      $phid = $this->findUserByRealName($user_name);
  48      if ($phid) {
  49        return $phid;
  50      }
  51  
  52      // No hits yet, try to parse it as an email address.
  53  
  54      $email = new PhutilEmailAddress($user_name);
  55  
  56      $phid = $this->findUserByEmailAddress($email->getAddress());
  57      if ($phid) {
  58        return $phid;
  59      }
  60  
  61      $display_name = $email->getDisplayName();
  62      if ($display_name) {
  63        $phid = $this->findUserByUserName($display_name);
  64        if ($phid) {
  65          return $phid;
  66        }
  67  
  68        $phid = $this->findUserByRealName($display_name);
  69        if ($phid) {
  70          return $phid;
  71        }
  72      }
  73  
  74      return null;
  75    }
  76  
  77  
  78    /**
  79     * Emit an event so installs can do custom lookup of commit authors who may
  80     * not be naturally resolvable.
  81     */
  82    private function fireLookupEvent($guess) {
  83  
  84      $type = PhabricatorEventType::TYPE_DIFFUSION_LOOKUPUSER;
  85      $data = array(
  86        'commit'  => $this->commit,
  87        'query'   => $this->name,
  88        'result'  => $guess,
  89      );
  90  
  91      $event = new PhabricatorEvent($type, $data);
  92      PhutilEventEngine::dispatchEvent($event);
  93  
  94      return $event->getValue('result');
  95    }
  96  
  97  
  98    private function findUserByUserName($user_name) {
  99      $by_username = id(new PhabricatorUser())->loadOneWhere(
 100        'userName = %s',
 101        $user_name);
 102      if ($by_username) {
 103        return $by_username->getPHID();
 104      }
 105      return null;
 106    }
 107  
 108  
 109    private function findUserByRealName($real_name) {
 110      // Note, real names are not guaranteed unique, which is why we do it this
 111      // way.
 112      $by_realname = id(new PhabricatorUser())->loadAllWhere(
 113        'realName = %s',
 114        $real_name);
 115      if (count($by_realname) == 1) {
 116        return reset($by_realname)->getPHID();
 117      }
 118      return null;
 119    }
 120  
 121  
 122    private function findUserByEmailAddress($email_address) {
 123      $by_email = PhabricatorUser::loadOneWithEmailAddress($email_address);
 124      if ($by_email) {
 125        return $by_email->getPHID();
 126      }
 127      return null;
 128    }
 129  
 130  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1