mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-01 18:30:59 +01:00
Add a "Thread Members" object policy and some unit tests
Summary: Ref T8488. Ref T5681. Now you can just use `id(new ConpherenceThreadMembersPolicyRule())->getObjectPolicyFullKey()` as a policy. Added tests for TaskAuthor + ThreadMembers. Test Plan: - Ran tests. - Set a thread policy to "Members of thread'. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T5681, T8488 Differential Revision: https://secure.phabricator.com/D13258
This commit is contained in:
parent
466755476a
commit
893c7a26c1
5 changed files with 118 additions and 1 deletions
|
@ -256,6 +256,7 @@ phutil_register_library_map(array(
|
|||
'ConpherenceThreadIndexer' => 'applications/conpherence/search/ConpherenceThreadIndexer.php',
|
||||
'ConpherenceThreadListView' => 'applications/conpherence/view/ConpherenceThreadListView.php',
|
||||
'ConpherenceThreadMailReceiver' => 'applications/conpherence/mail/ConpherenceThreadMailReceiver.php',
|
||||
'ConpherenceThreadMembersPolicyRule' => 'applications/conpherence/policyrule/ConpherenceThreadMembersPolicyRule.php',
|
||||
'ConpherenceThreadQuery' => 'applications/conpherence/query/ConpherenceThreadQuery.php',
|
||||
'ConpherenceThreadRemarkupRule' => 'applications/conpherence/remarkup/ConpherenceThreadRemarkupRule.php',
|
||||
'ConpherenceThreadSearchEngine' => 'applications/conpherence/query/ConpherenceThreadSearchEngine.php',
|
||||
|
@ -3542,6 +3543,7 @@ phutil_register_library_map(array(
|
|||
'ConpherenceThreadIndexer' => 'PhabricatorSearchDocumentIndexer',
|
||||
'ConpherenceThreadListView' => 'AphrontView',
|
||||
'ConpherenceThreadMailReceiver' => 'PhabricatorObjectMailReceiver',
|
||||
'ConpherenceThreadMembersPolicyRule' => 'PhabricatorPolicyRule',
|
||||
'ConpherenceThreadQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
|
||||
'ConpherenceThreadRemarkupRule' => 'PhabricatorObjectRemarkupRule',
|
||||
'ConpherenceThreadSearchEngine' => 'PhabricatorApplicationSearchEngine',
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
final class ConpherenceThreadMembersPolicyRule
|
||||
extends PhabricatorPolicyRule {
|
||||
|
||||
public function getObjectPolicyKey() {
|
||||
return 'conpherence.members';
|
||||
}
|
||||
|
||||
public function getObjectPolicyName() {
|
||||
return pht('Thread Members');
|
||||
}
|
||||
|
||||
public function getPolicyExplanation() {
|
||||
return pht('Members of this thread can take this action.');
|
||||
}
|
||||
|
||||
public function getRuleDescription() {
|
||||
return pht('thread members');
|
||||
}
|
||||
|
||||
public function canApplyToObject(PhabricatorPolicyInterface $object) {
|
||||
return ($object instanceof ConpherenceThread);
|
||||
}
|
||||
|
||||
public function applyRule(
|
||||
PhabricatorUser $viewer,
|
||||
$value,
|
||||
PhabricatorPolicyInterface $object) {
|
||||
$viewer_phid = $viewer->getPHID();
|
||||
if (!$viewer_phid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$object->getParticipantIfExists($viewer_phid);
|
||||
}
|
||||
|
||||
public function getValueControlType() {
|
||||
return self::CONTROL_TYPE_NONE;
|
||||
}
|
||||
|
||||
}
|
|
@ -150,4 +150,63 @@ final class PhabricatorPolicyDataTestCase extends PhabricatorTestCase {
|
|||
unset($time_b);
|
||||
}
|
||||
|
||||
public function testObjectPolicyRuleTaskAuthor() {
|
||||
$author = $this->generateNewTestUser();
|
||||
$viewer = $this->generateNewTestUser();
|
||||
|
||||
$rule = new ManiphestTaskAuthorPolicyRule();
|
||||
|
||||
$task = ManiphestTask::initializeNewTask($author);
|
||||
$task->setViewPolicy($rule->getObjectPolicyFullKey());
|
||||
$task->save();
|
||||
|
||||
$this->assertTrue(
|
||||
PhabricatorPolicyFilter::hasCapability(
|
||||
$author,
|
||||
$task,
|
||||
PhabricatorPolicyCapability::CAN_VIEW));
|
||||
|
||||
$this->assertFalse(
|
||||
PhabricatorPolicyFilter::hasCapability(
|
||||
$viewer,
|
||||
$task,
|
||||
PhabricatorPolicyCapability::CAN_VIEW));
|
||||
}
|
||||
|
||||
public function testObjectPolicyRuleThreadMembers() {
|
||||
$author = $this->generateNewTestUser();
|
||||
$viewer = $this->generateNewTestUser();
|
||||
|
||||
$rule = new ConpherenceThreadMembersPolicyRule();
|
||||
|
||||
$thread = ConpherenceThread::initializeNewRoom($author);
|
||||
$thread->setViewPolicy($rule->getObjectPolicyFullKey());
|
||||
$thread->save();
|
||||
|
||||
$this->assertFalse(
|
||||
PhabricatorPolicyFilter::hasCapability(
|
||||
$author,
|
||||
$thread,
|
||||
PhabricatorPolicyCapability::CAN_VIEW));
|
||||
|
||||
$this->assertFalse(
|
||||
PhabricatorPolicyFilter::hasCapability(
|
||||
$viewer,
|
||||
$thread,
|
||||
PhabricatorPolicyCapability::CAN_VIEW));
|
||||
|
||||
$participant = id(new ConpherenceParticipant())
|
||||
->setParticipantPHID($viewer->getPHID())
|
||||
->setConpherencePHID($thread->getPHID());
|
||||
|
||||
$thread->attachParticipants(array($viewer->getPHID() => $participant));
|
||||
|
||||
$this->assertTrue(
|
||||
PhabricatorPolicyFilter::hasCapability(
|
||||
$viewer,
|
||||
$thread,
|
||||
PhabricatorPolicyCapability::CAN_VIEW));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -316,7 +316,7 @@ final class PhabricatorPolicyQuery
|
|||
continue;
|
||||
}
|
||||
|
||||
$full_key = self::OBJECT_POLICY_PREFIX.$key;
|
||||
$full_key = $rule->getObjectPolicyFullKey();
|
||||
if (isset($results[$full_key])) {
|
||||
throw new Exception(
|
||||
pht(
|
||||
|
|
|
@ -114,6 +114,20 @@ abstract class PhabricatorPolicyRule {
|
|||
return null;
|
||||
}
|
||||
|
||||
public function getObjectPolicyFullKey() {
|
||||
$key = $this->getObjectPolicyKey();
|
||||
|
||||
if (!$key) {
|
||||
throw new Exception(
|
||||
pht(
|
||||
'This policy rule (of class "%s") does not have an associated '.
|
||||
'object policy key.',
|
||||
get_class($this)));
|
||||
}
|
||||
|
||||
return PhabricatorPolicyQuery::OBJECT_POLICY_PREFIX.$key;
|
||||
}
|
||||
|
||||
public function getObjectPolicyName() {
|
||||
throw new PhutilMethodNotImplementedException();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue