Differential D20914 Diff 49858 src/applications/repository/worker/PhabricatorRepositoryIdentityChangeWorker.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/repository/worker/PhabricatorRepositoryIdentityChangeWorker.php
<?php | <?php | ||||
final class PhabricatorRepositoryIdentityChangeWorker | final class PhabricatorRepositoryIdentityChangeWorker | ||||
extends PhabricatorWorker { | extends PhabricatorWorker { | ||||
protected function doWork() { | protected function doWork() { | ||||
$viewer = PhabricatorUser::getOmnipotentUser(); | $viewer = PhabricatorUser::getOmnipotentUser(); | ||||
$task_data = $this->getTaskData(); | $related_phids = $this->getTaskDataValue('relatedPHIDs'); | ||||
$user_phid = idx($task_data, 'userPHID'); | $email_addresses = $this->getTaskDataValue('emailAddresses'); | ||||
$user = id(new PhabricatorPeopleQuery()) | // Retain backward compatibility with older tasks which may still be in | ||||
->withPHIDs(array($user_phid)) | // queue. Previously, this worker accepted a single "userPHID". See | ||||
->setViewer($viewer) | // T13444. This can be removed in some future version of Phabricator once | ||||
->executeOne(); | // these tasks have likely flushed out of queue. | ||||
$legacy_phid = $this->getTaskDataValue('userPHID'); | |||||
if ($legacy_phid) { | |||||
if (!is_array($related_phids)) { | |||||
$related_phids = array(); | |||||
} | |||||
$related_phids[] = $legacy_phid; | |||||
} | |||||
$emails = id(new PhabricatorUserEmail())->loadAllWhere( | // Note that we may arrive in this worker after the associated objects | ||||
'userPHID = %s', | // have already been destroyed, so we can't (and shouldn't) verify that | ||||
$user->getPHID()); | // PHIDs correspond to real objects. If you "bin/remove destroy" a user, | ||||
// we'll end up here with a now-bogus user PHID that we should | |||||
// disassociate from identities. | |||||
$identity_engine = id(new DiffusionRepositoryIdentityEngine()) | $identity_map = array(); | ||||
->setViewer($viewer); | |||||
foreach ($emails as $email) { | if ($related_phids) { | ||||
$identities = id(new PhabricatorRepositoryIdentityQuery()) | $identities = id(new PhabricatorRepositoryIdentityQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withEmailAddresses(array($email->getAddress())) | ->withRelatedPHIDs($related_phids) | ||||
->execute(); | ->execute(); | ||||
$identity_map += mpull($identities, null, 'getPHID'); | |||||
} | |||||
foreach ($identities as $identity) { | if ($email_addresses) { | ||||
$identity_engine->newUpdatedIdentity($identity); | $identities = id(new PhabricatorRepositoryIdentityQuery()) | ||||
->setViewer($viewer) | |||||
->withEmailAddresses($email_addresses) | |||||
->execute(); | |||||
$identity_map += mpull($identities, null, 'getPHID'); | |||||
} | |||||
// If we didn't find any related identities, we're all set. | |||||
if (!$identity_map) { | |||||
return; | |||||
} | } | ||||
$identity_engine = id(new DiffusionRepositoryIdentityEngine()) | |||||
->setViewer($viewer); | |||||
foreach ($identity_map as $identity) { | |||||
$identity_engine->newUpdatedIdentity($identity); | |||||
} | } | ||||
} | } | ||||
} | } |