Changeset View
Changeset View
Standalone View
Standalone View
src/applications/people/storage/PhabricatorExternalAccount.php
Show All 17 Lines | final class PhabricatorExternalAccount | ||||
protected $emailVerified = 0; | protected $emailVerified = 0; | ||||
protected $accountURI; | protected $accountURI; | ||||
protected $profileImagePHID; | protected $profileImagePHID; | ||||
protected $properties = array(); | protected $properties = array(); | ||||
protected $providerConfigPHID; | protected $providerConfigPHID; | ||||
private $profileImageFile = self::ATTACHABLE; | private $profileImageFile = self::ATTACHABLE; | ||||
private $providerConfig = self::ATTACHABLE; | private $providerConfig = self::ATTACHABLE; | ||||
private $accountIdentifiers = self::ATTACHABLE; | |||||
public function getProfileImageFile() { | public function getProfileImageFile() { | ||||
return $this->assertAttached($this->profileImageFile); | return $this->assertAttached($this->profileImageFile); | ||||
} | } | ||||
public function attachProfileImageFile(PhabricatorFile $file) { | public function attachProfileImageFile(PhabricatorFile $file) { | ||||
$this->profileImageFile = $file; | $this->profileImageFile = $file; | ||||
return $this; | return $this; | ||||
Show All 39 Lines | final class PhabricatorExternalAccount | ||||
public function getProviderKey() { | public function getProviderKey() { | ||||
return $this->getAccountType().':'.$this->getAccountDomain(); | return $this->getAccountType().':'.$this->getAccountDomain(); | ||||
} | } | ||||
public function save() { | public function save() { | ||||
if (!$this->getAccountSecret()) { | if (!$this->getAccountSecret()) { | ||||
$this->setAccountSecret(Filesystem::readRandomCharacters(32)); | $this->setAccountSecret(Filesystem::readRandomCharacters(32)); | ||||
} | } | ||||
return parent::save(); | |||||
$this->openTransaction(); | |||||
$result = parent::save(); | |||||
$account_phid = $this->getPHID(); | |||||
$config_phid = $this->getProviderConfigPHID(); | |||||
if ($this->accountIdentifiers !== self::ATTACHABLE) { | |||||
foreach ($this->getAccountIdentifiers() as $identifier) { | |||||
$identifier | |||||
->setExternalAccountPHID($account_phid) | |||||
->setProviderConfigPHID($config_phid) | |||||
->save(); | |||||
} | |||||
} | |||||
$this->saveTransaction(); | |||||
return $result; | |||||
} | |||||
public function unlinkAccount() { | |||||
// When unlinking an account, we disassociate it from the user and | |||||
// remove all the identifying information. We retain the PHID, the | |||||
// object itself, and the "ExternalAccountIdentifier" objects in the | |||||
// external table. | |||||
// TODO: This unlinks (but does not destroy) any profile image. | |||||
return $this | |||||
->setUserPHID(null) | |||||
->setDisplayName(null) | |||||
->setUsername(null) | |||||
->setRealName(null) | |||||
->setEmail(null) | |||||
->setEmailVerified(0) | |||||
->setProfileImagePHID(null) | |||||
->setAccountURI(null) | |||||
->setProperties(array()) | |||||
->save(); | |||||
} | } | ||||
public function setProperty($key, $value) { | public function setProperty($key, $value) { | ||||
$this->properties[$key] = $value; | $this->properties[$key] = $value; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getProperty($key, $default = null) { | public function getProperty($key, $default = null) { | ||||
Show All 36 Lines | public function attachProviderConfig(PhabricatorAuthProviderConfig $config) { | ||||
$this->providerConfig = $config; | $this->providerConfig = $config; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getProviderConfig() { | public function getProviderConfig() { | ||||
return $this->assertAttached($this->providerConfig); | return $this->assertAttached($this->providerConfig); | ||||
} | } | ||||
public function getAccountIdentifiers() { | |||||
$raw = $this->assertAttached($this->accountIdentifiers); | |||||
return array_values($raw); | |||||
} | |||||
public function attachAccountIdentifiers(array $identifiers) { | |||||
assert_instances_of($identifiers, 'PhabricatorExternalAccountIdentifier'); | |||||
$this->accountIdentifiers = mpull($identifiers, null, 'getIdentifierRaw'); | |||||
return $this; | |||||
} | |||||
public function appendIdentifier( | |||||
PhabricatorExternalAccountIdentifier $identifier) { | |||||
$this->assertAttached($this->accountIdentifiers); | |||||
$map = $this->accountIdentifiers; | |||||
$raw = $identifier->getIdentifierRaw(); | |||||
$old = idx($map, $raw); | |||||
$new = $identifier; | |||||
if ($old === null) { | |||||
$result = $new; | |||||
} else { | |||||
// Here, we already know about an identifier and have rediscovered it. | |||||
// We could copy properties from the new version of the identifier here, | |||||
// or merge them in some other way (for example, update a "last seen | |||||
// from the provider" timestamp), but no such properties currently exist. | |||||
$result = $old; | |||||
} | |||||
$this->accountIdentifiers[$raw] = $result; | |||||
return $this; | |||||
} | |||||
/* -( PhabricatorPolicyInterface )----------------------------------------- */ | /* -( PhabricatorPolicyInterface )----------------------------------------- */ | ||||
public function getCapabilities() { | public function getCapabilities() { | ||||
return array( | return array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
Show All 35 Lines | public function destroyObjectPermanently( | ||||
$identifiers = id(new PhabricatorExternalAccountIdentifierQuery()) | $identifiers = id(new PhabricatorExternalAccountIdentifierQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withExternalAccountPHIDs(array($this->getPHID())) | ->withExternalAccountPHIDs(array($this->getPHID())) | ||||
->newIterator(); | ->newIterator(); | ||||
foreach ($identifiers as $identifier) { | foreach ($identifiers as $identifier) { | ||||
$engine->destroyObject($identifier); | $engine->destroyObject($identifier); | ||||
} | } | ||||
// TODO: This may leave a profile image behind. | |||||
$this->delete(); | $this->delete(); | ||||
} | } | ||||
} | } |