3 final class DiffusionURIEditor
4 extends PhabricatorApplicationTransactionEditor
{
7 private $repositoryPHID;
9 public function getEditorApplicationClass() {
10 return 'PhabricatorDiffusionApplication';
13 public function getEditorObjectsDescription() {
14 return pht('Diffusion URIs');
17 public function getTransactionTypes() {
18 $types = parent
::getTransactionTypes();
20 $types[] = PhabricatorRepositoryURITransaction
::TYPE_REPOSITORY
;
21 $types[] = PhabricatorRepositoryURITransaction
::TYPE_URI
;
22 $types[] = PhabricatorRepositoryURITransaction
::TYPE_IO
;
23 $types[] = PhabricatorRepositoryURITransaction
::TYPE_DISPLAY
;
24 $types[] = PhabricatorRepositoryURITransaction
::TYPE_CREDENTIAL
;
25 $types[] = PhabricatorRepositoryURITransaction
::TYPE_DISABLE
;
30 protected function getCustomTransactionOldValue(
31 PhabricatorLiskDAO
$object,
32 PhabricatorApplicationTransaction
$xaction) {
34 switch ($xaction->getTransactionType()) {
35 case PhabricatorRepositoryURITransaction
::TYPE_URI
:
36 return $object->getURI();
37 case PhabricatorRepositoryURITransaction
::TYPE_IO
:
38 return $object->getIOType();
39 case PhabricatorRepositoryURITransaction
::TYPE_DISPLAY
:
40 return $object->getDisplayType();
41 case PhabricatorRepositoryURITransaction
::TYPE_REPOSITORY
:
42 return $object->getRepositoryPHID();
43 case PhabricatorRepositoryURITransaction
::TYPE_CREDENTIAL
:
44 return $object->getCredentialPHID();
45 case PhabricatorRepositoryURITransaction
::TYPE_DISABLE
:
46 return (int)$object->getIsDisabled();
49 return parent
::getCustomTransactionOldValue($object, $xaction);
52 protected function getCustomTransactionNewValue(
53 PhabricatorLiskDAO
$object,
54 PhabricatorApplicationTransaction
$xaction) {
56 switch ($xaction->getTransactionType()) {
57 case PhabricatorRepositoryURITransaction
::TYPE_URI
:
58 case PhabricatorRepositoryURITransaction
::TYPE_IO
:
59 case PhabricatorRepositoryURITransaction
::TYPE_DISPLAY
:
60 case PhabricatorRepositoryURITransaction
::TYPE_REPOSITORY
:
61 case PhabricatorRepositoryURITransaction
::TYPE_CREDENTIAL
:
62 return $xaction->getNewValue();
63 case PhabricatorRepositoryURITransaction
::TYPE_DISABLE
:
64 return (int)$xaction->getNewValue();
67 return parent
::getCustomTransactionNewValue($object, $xaction);
70 protected function applyCustomInternalTransaction(
71 PhabricatorLiskDAO
$object,
72 PhabricatorApplicationTransaction
$xaction) {
74 switch ($xaction->getTransactionType()) {
75 case PhabricatorRepositoryURITransaction
::TYPE_URI
:
76 if (!$this->getIsNewObject()) {
77 $old_uri = $object->getEffectiveURI();
81 // When creating a URI via the API, we may not have processed the
82 // repository transaction yet. Attach the repository here to make
83 // sure we have it for the calls below.
84 if ($this->repository
) {
85 $object->attachRepository($this->repository
);
89 $object->setURI($xaction->getNewValue());
91 // If we've changed the domain or protocol of the URI, remove the
92 // current credential. This improves behavior in several cases:
94 // If a user switches between protocols with different credential
95 // types, like HTTP and SSH, the old credential won't be valid anyway.
96 // It's cleaner to remove it than leave a bad credential in place.
98 // If a user switches hosts, the old credential is probably not
99 // correct (and potentially confusing/misleading). Removing it forces
100 // users to double check that they have the correct credentials.
102 // If an attacker can't see a symmetric credential like a username and
103 // password, they could still potentially capture it by changing the
104 // host for a URI that uses it to `evil.com`, a server they control,
105 // then observing the requests. Removing the credential prevents this
106 // kind of escalation.
108 // Since port and path changes are less likely to fall among these
109 // cases, they don't trigger a credential wipe.
111 $new_uri = $object->getEffectiveURI();
113 $new_proto = ($old_uri->getProtocol() != $new_uri->getProtocol());
114 $new_domain = ($old_uri->getDomain() != $new_uri->getDomain());
115 if ($new_proto ||
$new_domain) {
116 $object->setCredentialPHID(null);
120 case PhabricatorRepositoryURITransaction
::TYPE_IO
:
121 $object->setIOType($xaction->getNewValue());
123 case PhabricatorRepositoryURITransaction
::TYPE_DISPLAY
:
124 $object->setDisplayType($xaction->getNewValue());
126 case PhabricatorRepositoryURITransaction
::TYPE_REPOSITORY
:
127 $object->setRepositoryPHID($xaction->getNewValue());
128 $object->attachRepository($this->repository
);
130 case PhabricatorRepositoryURITransaction
::TYPE_CREDENTIAL
:
131 $object->setCredentialPHID($xaction->getNewValue());
133 case PhabricatorRepositoryURITransaction
::TYPE_DISABLE
:
134 $object->setIsDisabled($xaction->getNewValue());
139 protected function applyCustomExternalTransaction(
140 PhabricatorLiskDAO
$object,
141 PhabricatorApplicationTransaction
$xaction) {
143 switch ($xaction->getTransactionType()) {
144 case PhabricatorRepositoryURITransaction
::TYPE_URI
:
145 case PhabricatorRepositoryURITransaction
::TYPE_IO
:
146 case PhabricatorRepositoryURITransaction
::TYPE_DISPLAY
:
147 case PhabricatorRepositoryURITransaction
::TYPE_REPOSITORY
:
148 case PhabricatorRepositoryURITransaction
::TYPE_CREDENTIAL
:
149 case PhabricatorRepositoryURITransaction
::TYPE_DISABLE
:
153 return parent
::applyCustomExternalTransaction($object, $xaction);
156 protected function validateTransaction(
157 PhabricatorLiskDAO
$object,
161 $errors = parent
::validateTransaction($object, $type, $xactions);
164 case PhabricatorRepositoryURITransaction
::TYPE_REPOSITORY
:
165 // Save this, since we need it to validate TYPE_IO transactions.
166 $this->repositoryPHID
= $object->getRepositoryPHID();
168 $missing = $this->validateIsEmptyTextField(
169 $object->getRepositoryPHID(),
172 // NOTE: This isn't being marked as a missing field error because
173 // it's a fundamental, required property of the URI.
174 $errors[] = new PhabricatorApplicationTransactionValidationError(
178 'When creating a repository URI, you must specify which '.
179 'repository the URI will belong to.'),
180 nonempty(last($xactions), null));
184 $viewer = $this->getActor();
186 foreach ($xactions as $xaction) {
187 $repository_phid = $xaction->getNewValue();
189 // If this isn't changing anything, let it through as-is.
190 if ($repository_phid == $object->getRepositoryPHID()) {
194 if (!$this->getIsNewObject()) {
195 $errors[] = new PhabricatorApplicationTransactionValidationError(
199 'The repository a URI is associated with is immutable, and '.
200 'can not be changed after the URI is created.'),
205 $repository = id(new PhabricatorRepositoryQuery())
207 ->withPHIDs(array($repository_phid))
208 ->requireCapabilities(
210 PhabricatorPolicyCapability
::CAN_VIEW
,
211 PhabricatorPolicyCapability
::CAN_EDIT
,
215 $errors[] = new PhabricatorApplicationTransactionValidationError(
219 'To create a URI for a repository ("%s"), it must exist and '.
220 'you must have permission to edit it.',
226 $this->repository
= $repository;
227 $this->repositoryPHID
= $repository_phid;
230 case PhabricatorRepositoryURITransaction
::TYPE_CREDENTIAL
:
231 $viewer = $this->getActor();
232 foreach ($xactions as $xaction) {
233 $credential_phid = $xaction->getNewValue();
235 if ($credential_phid == $object->getCredentialPHID()) {
239 // Anyone who can edit a URI can remove the credential.
240 if ($credential_phid === null) {
244 $credential = id(new PassphraseCredentialQuery())
246 ->withPHIDs(array($credential_phid))
249 $errors[] = new PhabricatorApplicationTransactionValidationError(
253 'You can only associate a credential ("%s") with a repository '.
254 'URI if it exists and you have permission to see it.',
261 case PhabricatorRepositoryURITransaction
::TYPE_URI
:
262 $missing = $this->validateIsEmptyTextField(
267 $error = new PhabricatorApplicationTransactionValidationError(
270 pht('A repository URI must have a nonempty URI.'),
271 nonempty(last($xactions), null));
273 $error->setIsMissingFieldError(true);
278 foreach ($xactions as $xaction) {
279 $new_uri = $xaction->getNewValue();
280 if ($new_uri == $object->getURI()) {
285 PhabricatorRepository
::assertValidRemoteURI($new_uri);
286 } catch (Exception
$ex) {
287 $errors[] = new PhabricatorApplicationTransactionValidationError(
297 case PhabricatorRepositoryURITransaction
::TYPE_IO
:
298 $available = $object->getAvailableIOTypeOptions();
299 foreach ($xactions as $xaction) {
300 $new = $xaction->getNewValue();
302 if (empty($available[$new])) {
303 $errors[] = new PhabricatorApplicationTransactionValidationError(
307 'Value "%s" is not a valid IO setting for this URI. '.
308 'Available types for this URI are: %s.',
310 implode(', ', array_keys($available))),
315 // If we are setting this URI to use "Observe", we must have no
316 // other "Observe" URIs and must also have no "Read/Write" URIs.
318 // If we are setting this URI to "Read/Write", we must have no
319 // other "Observe" URIs. It's OK to have other "Read/Write" URIs.
321 $no_observers = false;
322 $no_readwrite = false;
324 case PhabricatorRepositoryURI
::IO_OBSERVE
:
325 $no_readwrite = true;
326 $no_observers = true;
328 case PhabricatorRepositoryURI
::IO_READWRITE
:
329 $no_observers = true;
333 if ($no_observers ||
$no_readwrite) {
334 $repository = id(new PhabricatorRepositoryQuery())
335 ->setViewer(PhabricatorUser
::getOmnipotentUser())
336 ->withPHIDs(array($this->repositoryPHID
))
339 $uris = $repository->getURIs();
341 $observe_conflict = null;
342 $readwrite_conflict = null;
343 foreach ($uris as $uri) {
344 // If this is the URI being edited, it can not conflict with
346 if ($uri->getID() == $object->getID()) {
350 $io_type = $uri->getEffectiveIOType();
352 if ($io_type == PhabricatorRepositoryURI
::IO_READWRITE
) {
354 $readwrite_conflict = $uri;
359 if ($io_type == PhabricatorRepositoryURI
::IO_OBSERVE
) {
361 $observe_conflict = $uri;
367 if ($observe_conflict) {
368 if ($new == PhabricatorRepositoryURI
::IO_OBSERVE
) {
370 'You can not set this URI to use Observe IO because '.
371 'another URI for this repository is already configured '.
372 'in Observe IO mode. A repository can not observe two '.
373 'different remotes simultaneously. Turn off IO for the '.
377 'You can not set this URI to use Read/Write IO because '.
378 'another URI for this repository is already configured '.
379 'in Observe IO mode. An observed repository can not be '.
380 'made writable. Turn off IO for the other URI first.');
383 $errors[] = new PhabricatorApplicationTransactionValidationError(
391 if ($readwrite_conflict) {
393 'You can not set this URI to use Observe IO because '.
394 'another URI for this repository is already configured '.
395 'in Read/Write IO mode. A repository can not simultaneously '.
396 'be writable and observe a remote. Turn off IO for the '.
399 $errors[] = new PhabricatorApplicationTransactionValidationError(
410 case PhabricatorRepositoryURITransaction
::TYPE_DISPLAY
:
411 $available = $object->getAvailableDisplayTypeOptions();
412 foreach ($xactions as $xaction) {
413 $new = $xaction->getNewValue();
415 if (empty($available[$new])) {
416 $errors[] = new PhabricatorApplicationTransactionValidationError(
420 'Value "%s" is not a valid display setting for this URI. '.
421 'Available types for this URI are: %s.',
423 implode(', ', array_keys($available))));
428 case PhabricatorRepositoryURITransaction
::TYPE_DISABLE
:
429 $old = $object->getIsDisabled();
430 foreach ($xactions as $xaction) {
431 $new = $xaction->getNewValue();
437 if (!$object->isBuiltin()) {
441 $errors[] = new PhabricatorApplicationTransactionValidationError(
444 pht('You can not manually disable builtin URIs.'));
452 protected function applyFinalEffects(
453 PhabricatorLiskDAO
$object,
456 // Synchronize the repository state based on the presence of an "Observe"
458 $repository = $object->getRepository();
460 $uris = id(new PhabricatorRepositoryURIQuery())
461 ->setViewer(PhabricatorUser
::getOmnipotentUser())
462 ->withRepositories(array($repository))
465 // Reattach the current URIs to the repository: we're going to rebuild
466 // the index explicitly below, and want to include any changes made to
467 // this URI in the index update.
468 $repository->attachURIs($uris);
471 foreach ($uris as $uri) {
472 if ($uri->getIoType() != PhabricatorRepositoryURI
::IO_OBSERVE
) {
480 $was_hosted = $repository->isHosted();
485 ->setDetail('remote-uri', (string)$observe_uri->getEffectiveURI())
486 ->setCredentialPHID($observe_uri->getCredentialPHID());
490 ->setDetail('remote-uri', null)
491 ->setCredentialPHID(null);
496 // Explicitly update the URI index.
497 $repository->updateURIIndex();
499 $is_hosted = $repository->isHosted();
501 // If we've swapped the repository from hosted to observed or vice versa,
502 // reset all the cluster version clocks.
503 if ($was_hosted != $is_hosted) {
504 $cluster_engine = id(new DiffusionRepositoryClusterEngine())
505 ->setViewer($this->getActor())
506 ->setRepository($repository)
507 ->synchronizeWorkingCopyAfterHostingChange();
510 $repository->writeStatusMessage(
511 PhabricatorRepositoryStatusMessage
::TYPE_NEEDS_UPDATE
,