@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Resolve an author or committer name, like
5 * `"Abraham Lincoln <alincoln@logcab.in>"`, into a valid Phabricator user
6 * account, like `@alincoln`.
7 */
8final class DiffusionResolveUserQuery extends Phobject {
9
10 private $name;
11
12 public function withName($name) {
13 $this->name = $name;
14 return $this;
15 }
16
17 public function execute() {
18 return $this->findUserPHID($this->name);
19 }
20
21 private function findUserPHID($user_name) {
22 if (!strlen($user_name)) {
23 return null;
24 }
25
26 $phid = $this->findUserByUserName($user_name);
27 if ($phid) {
28 return $phid;
29 }
30
31 $phid = $this->findUserByEmailAddress($user_name);
32 if ($phid) {
33 return $phid;
34 }
35
36 $phid = $this->findUserByRealName($user_name);
37 if ($phid) {
38 return $phid;
39 }
40
41 // No hits yet, try to parse it as an email address.
42
43 $email = new PhutilEmailAddress($user_name);
44
45 $phid = $this->findUserByEmailAddress($email->getAddress());
46 if ($phid) {
47 return $phid;
48 }
49
50 $display_name = $email->getDisplayName();
51 if ($display_name) {
52 $phid = $this->findUserByUserName($display_name);
53 if ($phid) {
54 return $phid;
55 }
56
57 $phid = $this->findUserByRealName($display_name);
58 if ($phid) {
59 return $phid;
60 }
61 }
62
63 return null;
64 }
65
66
67 private function findUserByUserName($user_name) {
68 $by_username = id(new PhabricatorUser())->loadOneWhere(
69 'userName = %s',
70 $user_name);
71
72 if ($by_username) {
73 return $by_username->getPHID();
74 }
75
76 return null;
77 }
78
79
80 private function findUserByRealName($real_name) {
81 // Note, real names are not guaranteed unique, which is why we do it this
82 // way.
83 $by_realname = id(new PhabricatorUser())->loadAllWhere(
84 'realName = %s',
85 $real_name);
86
87 if (count($by_realname) == 1) {
88 return head($by_realname)->getPHID();
89 }
90
91 return null;
92 }
93
94
95 private function findUserByEmailAddress($email_address) {
96 $by_email =
97 PhabricatorUser::loadOneWithVerifiedEmailAddress($email_address);
98
99 if ($by_email) {
100 return $by_email->getPHID();
101 }
102
103 return null;
104 }
105
106}