Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diffusion/query/DiffusionResolveUserQuery.php
12242 views
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
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 = PhabricatorUser::loadOneWithEmailAddress($email_address);
97
98
if ($by_email) {
99
return $by_email->getPHID();
100
}
101
102
return null;
103
}
104
105
}
106
107