1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-24 07:42:40 +01:00
phorge-phorge/src/applications/project/policyrule/PhabricatorProjectMembersPolicyRule.php
epriestley 1c572d1da5 Implement a "Project Members" object policy rule
Summary:
Fixes T9019. Pretty much ripped from D14467. I added the "policy hint" stuff so that you can create a project with this policy immediately.

I really dislike how the "hint" code works, but we //almost// never need to use it and the badness feels fairly well-contained.

Also pick up a quick feedback fix from D14863.

Test Plan:
  - Added test coverage, got it to pass.
  - Created a project with "Visible To: Project Members".

Reviewers: joshuaspence, chad

Reviewed By: chad

Maniphest Tasks: T9019

Differential Revision: https://secure.phabricator.com/D14869
2015-12-24 08:16:27 -08:00

97 lines
2.2 KiB
PHP

<?php
final class PhabricatorProjectMembersPolicyRule extends PhabricatorPolicyRule {
private $memberships = array();
public function getRuleDescription() {
return pht('members of project');
}
public function willApplyRules(
PhabricatorUser $viewer,
array $values,
array $objects) {
$viewer_phid = $viewer->getPHID();
if (!$viewer_phid) {
return;
}
if (empty($this->memberships[$viewer_phid])) {
$this->memberships[$viewer_phid] = array();
}
foreach ($objects as $key => $object) {
$cache = $this->getTransactionHint($object);
if ($cache === null) {
continue;
}
unset($objects[$key]);
if (isset($cache[$viewer_phid])) {
$this->memberships[$viewer_phid][$object->getPHID()] = true;
}
}
if (!$objects) {
return;
}
$object_phids = mpull($objects, 'getPHID');
$edge_query = id(new PhabricatorEdgeQuery())
->withSourcePHIDs(array($viewer_phid))
->withDestinationPHIDs($object_phids)
->withEdgeTypes(
array(
PhabricatorProjectMemberOfProjectEdgeType::EDGECONST,
));
$edge_query->execute();
$memberships = $edge_query->getDestinationPHIDs();
if (!$memberships) {
return;
}
$this->memberships[$viewer_phid] += array_fill_keys($memberships, true);
}
public function applyRule(
PhabricatorUser $viewer,
$value,
PhabricatorPolicyInterface $object) {
$viewer_phid = $viewer->getPHID();
if (!$viewer_phid) {
return false;
}
$memberships = idx($this->memberships, $viewer_phid);
return isset($memberships[$object->getPHID()]);
}
public function getValueControlType() {
return self::CONTROL_TYPE_NONE;
}
public function canApplyToObject(PhabricatorPolicyInterface $object) {
return ($object instanceof PhabricatorProject);
}
public function getObjectPolicyKey() {
return 'project.members';
}
public function getObjectPolicyName() {
return pht('Project Members');
}
public function getObjectPolicyIcon() {
return 'fa-users';
}
public function getPolicyExplanation() {
return pht('Project members can take this action.');
}
}