@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
3final class PhabricatorRepositoryIdentity
4 extends PhabricatorRepositoryDAO
5 implements
6 PhabricatorPolicyInterface,
7 PhabricatorApplicationTransactionInterface {
8
9 protected $authorPHID;
10 protected $identityNameHash;
11 protected $identityNameRaw;
12 protected $identityNameEncoding;
13 protected $automaticGuessedUserPHID;
14 protected $manuallySetUserPHID;
15 protected $currentEffectiveUserPHID;
16 protected $emailAddress;
17
18 protected function getConfiguration() {
19 return array(
20 self::CONFIG_AUX_PHID => true,
21 self::CONFIG_BINARY => array(
22 'identityNameRaw' => true,
23 ),
24 self::CONFIG_COLUMN_SCHEMA => array(
25 'identityNameHash' => 'bytes12',
26 'identityNameEncoding' => 'text16?',
27 'automaticGuessedUserPHID' => 'phid?',
28 'manuallySetUserPHID' => 'phid?',
29 'currentEffectiveUserPHID' => 'phid?',
30 'emailAddress' => 'sort255?',
31 ),
32 self::CONFIG_KEY_SCHEMA => array(
33 'key_identity' => array(
34 'columns' => array('identityNameHash'),
35 'unique' => true,
36 ),
37 'key_email' => array(
38 'columns' => array('emailAddress(64)'),
39 ),
40 ),
41 ) + parent::getConfiguration();
42 }
43
44 public function getPHIDType() {
45 return PhabricatorRepositoryIdentityPHIDType::TYPECONST;
46 }
47
48 public function setIdentityName($name_raw) {
49 $this->setIdentityNameRaw($name_raw);
50 $this->setIdentityNameHash(PhabricatorHash::digestForIndex($name_raw));
51 $this->setIdentityNameEncoding($this->detectEncodingForStorage($name_raw));
52
53 return $this;
54 }
55
56 public function getIdentityName() {
57 return $this->getUTF8StringFromStorage(
58 $this->getIdentityNameRaw(),
59 $this->getIdentityNameEncoding());
60 }
61
62 public function getIdentityEmailAddress() {
63 $address = new PhutilEmailAddress($this->getIdentityName());
64 return $address->getAddress();
65 }
66
67 public function getIdentityDisplayName() {
68 $address = new PhutilEmailAddress($this->getIdentityName());
69 return $address->getDisplayName();
70 }
71
72 public function getIdentityShortName() {
73 // TODO
74 return $this->getIdentityName();
75 }
76
77 public function getObjectName() {
78 return pht('Identity %d', $this->getID());
79 }
80
81 public function getURI() {
82 return '/diffusion/identity/view/'.$this->getID().'/';
83 }
84
85 public function hasEffectiveUser() {
86 return ($this->currentEffectiveUserPHID != null);
87 }
88
89 public function getIdentityDisplayPHID() {
90 if ($this->hasEffectiveUser()) {
91 return $this->getCurrentEffectiveUserPHID();
92 } else {
93 return $this->getPHID();
94 }
95 }
96
97 public function save() {
98 if ($this->manuallySetUserPHID) {
99 $unassigned = DiffusionIdentityUnassignedDatasource::FUNCTION_TOKEN;
100 if ($this->manuallySetUserPHID === $unassigned) {
101 $effective_phid = null;
102 } else {
103 $effective_phid = $this->manuallySetUserPHID;
104 }
105 } else {
106 $effective_phid = $this->automaticGuessedUserPHID;
107 }
108
109 $this->setCurrentEffectiveUserPHID($effective_phid);
110
111 $email_address = $this->getIdentityEmailAddress();
112
113 // Raw identities are unrestricted binary data, and may consequently
114 // have arbitrarily long, binary email address information. We can't
115 // store this kind of information in the "emailAddress" column, which
116 // has column type "sort255".
117
118 // This kind of address almost certainly not legitimate and users can
119 // manually set the target of the identity, so just discard it rather
120 // than trying especially hard to make it work.
121
122 $byte_limit = $this->getColumnMaximumByteLength('emailAddress');
123 $email_address = phutil_utf8ize($email_address);
124 if (strlen($email_address) > $byte_limit) {
125 $email_address = null;
126 }
127
128 $this->setEmailAddress($email_address);
129
130 return parent::save();
131 }
132
133
134/* -( PhabricatorPolicyInterface )----------------------------------------- */
135
136
137 public function getCapabilities() {
138 return array(
139 PhabricatorPolicyCapability::CAN_VIEW,
140 PhabricatorPolicyCapability::CAN_EDIT,
141 );
142 }
143
144 public function getPolicy($capability) {
145 $app = PhabricatorApplication::getByClass(
146 PhabricatorDiffusionApplication::class);
147 return $app->getPolicy(
148 PhabricatorRepositoryIdentityEditViewCapability::CAPABILITY);
149 }
150
151 public function hasAutomaticCapability(
152 $capability,
153 PhabricatorUser $viewer) {
154 return false;
155 }
156
157
158/* -( PhabricatorApplicationTransactionInterface )------------------------- */
159
160
161 public function getApplicationTransactionEditor() {
162 return new DiffusionRepositoryIdentityEditor();
163 }
164
165 public function getApplicationTransactionTemplate() {
166 return new PhabricatorRepositoryIdentityTransaction();
167 }
168
169}