1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 00:32:42 +01:00

Implement "PolicyInterface" on "UserEmail" so "EmailQuery" can load them properly

Summary:
See PHI1558. Ref T11860. Ref T13444. I partly implemented PHIDs for "UserEmail" objects, but they can't load on their own so you can't directly `bin/remove destroy` them yet.

Allow them to actually load by implementing "PolicyInterface".

Addresses are viewable and editable by the associated user, unless they are a bot/list address, in which case they are viewable and editable by administrators (in preparation for T11860). This has no real impact on anything today.

Test Plan:
  - Used `bin/remove destroy <phid>` to destroy an individual email address.
  - Before: error while loading the object by PHID in the query policy layer.
  - After: clean load and destroy.

Maniphest Tasks: T13444, T11860

Differential Revision: https://secure.phabricator.com/D20927
This commit is contained in:
epriestley 2019-11-25 15:01:45 -08:00
parent eb6df7a209
commit 1667acfa5d
4 changed files with 71 additions and 1 deletions

View file

@ -11690,6 +11690,7 @@ phutil_register_library_map(array(
'PhabricatorUserEmail' => array( 'PhabricatorUserEmail' => array(
'PhabricatorUserDAO', 'PhabricatorUserDAO',
'PhabricatorDestructibleInterface', 'PhabricatorDestructibleInterface',
'PhabricatorPolicyInterface',
), ),
'PhabricatorUserEmailTestCase' => 'PhabricatorTestCase', 'PhabricatorUserEmailTestCase' => 'PhabricatorTestCase',
'PhabricatorUserEmpowerTransaction' => 'PhabricatorUserTransactionType', 'PhabricatorUserEmpowerTransaction' => 'PhabricatorUserTransactionType',

View file

@ -29,6 +29,12 @@ final class PhabricatorPeopleUserEmailPHIDType
PhabricatorHandleQuery $query, PhabricatorHandleQuery $query,
array $handles, array $handles,
array $objects) { array $objects) {
foreach ($handles as $phid => $handle) {
$email = $objects[$phid];
$handle->setName($email->getAddress());
}
return null; return null;
} }

View file

@ -48,6 +48,32 @@ final class PhabricatorPeopleUserEmailQuery
return $where; return $where;
} }
protected function willLoadPage(array $page) {
$user_phids = mpull($page, 'getUserPHID');
$users = id(new PhabricatorPeopleQuery())
->setViewer($this->getViewer())
->setParentQuery($this)
->withPHIDs($user_phids)
->execute();
$users = mpull($users, null, 'getPHID');
foreach ($page as $key => $address) {
$user = idx($users, $address->getUserPHID());
if (!$user) {
unset($page[$key]);
$this->didRejectResult($address);
continue;
}
$address->attachUser($user);
}
return $page;
}
public function getQueryApplicationClass() { public function getQueryApplicationClass() {
return 'PhabricatorPeopleApplication'; return 'PhabricatorPeopleApplication';
} }

View file

@ -6,7 +6,9 @@
*/ */
final class PhabricatorUserEmail final class PhabricatorUserEmail
extends PhabricatorUserDAO extends PhabricatorUserDAO
implements PhabricatorDestructibleInterface { implements
PhabricatorDestructibleInterface,
PhabricatorPolicyInterface {
protected $userPHID; protected $userPHID;
protected $address; protected $address;
@ -14,6 +16,8 @@ final class PhabricatorUserEmail
protected $isPrimary; protected $isPrimary;
protected $verificationCode; protected $verificationCode;
private $user = self::ATTACHABLE;
const MAX_ADDRESS_LENGTH = 128; const MAX_ADDRESS_LENGTH = 128;
protected function getConfiguration() { protected function getConfiguration() {
@ -52,6 +56,15 @@ final class PhabricatorUserEmail
return parent::save(); return parent::save();
} }
public function attachUser(PhabricatorUser $user) {
$this->user = $user;
return $this;
}
public function getUser() {
return $this->assertAttached($this->user);
}
/* -( Domain Restrictions )------------------------------------------------ */ /* -( Domain Restrictions )------------------------------------------------ */
@ -287,4 +300,28 @@ final class PhabricatorUserEmail
$this->delete(); $this->delete();
} }
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
);
}
public function getPolicy($capability) {
$user = $this->getUser();
if ($this->getIsSystemAgent() || $this->getIsMailingList()) {
return PhabricatorPolicies::POLICY_ADMIN;
}
return $user->getPHID();
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return false;
}
} }