From 893c7a26c196ecbe619233b8816ca87044e5779e Mon Sep 17 00:00:00 2001 From: epriestley Date: Sat, 13 Jun 2015 15:44:56 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 2 + .../ConpherenceThreadMembersPolicyRule.php | 42 +++++++++++++ .../PhabricatorPolicyDataTestCase.php | 59 +++++++++++++++++++ .../policy/query/PhabricatorPolicyQuery.php | 2 +- .../policy/rule/PhabricatorPolicyRule.php | 14 +++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/applications/conpherence/policyrule/ConpherenceThreadMembersPolicyRule.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 70eb4103fc..63e0916c34 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/applications/conpherence/policyrule/ConpherenceThreadMembersPolicyRule.php b/src/applications/conpherence/policyrule/ConpherenceThreadMembersPolicyRule.php new file mode 100644 index 0000000000..658ee42bdb --- /dev/null +++ b/src/applications/conpherence/policyrule/ConpherenceThreadMembersPolicyRule.php @@ -0,0 +1,42 @@ +getPHID(); + if (!$viewer_phid) { + return false; + } + + return (bool)$object->getParticipantIfExists($viewer_phid); + } + + public function getValueControlType() { + return self::CONTROL_TYPE_NONE; + } + +} diff --git a/src/applications/policy/__tests__/PhabricatorPolicyDataTestCase.php b/src/applications/policy/__tests__/PhabricatorPolicyDataTestCase.php index e9e9d21e0b..2348ba13b1 100644 --- a/src/applications/policy/__tests__/PhabricatorPolicyDataTestCase.php +++ b/src/applications/policy/__tests__/PhabricatorPolicyDataTestCase.php @@ -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)); + } + + } diff --git a/src/applications/policy/query/PhabricatorPolicyQuery.php b/src/applications/policy/query/PhabricatorPolicyQuery.php index d0cf78b917..496c704b71 100644 --- a/src/applications/policy/query/PhabricatorPolicyQuery.php +++ b/src/applications/policy/query/PhabricatorPolicyQuery.php @@ -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( diff --git a/src/applications/policy/rule/PhabricatorPolicyRule.php b/src/applications/policy/rule/PhabricatorPolicyRule.php index 4499ea459e..919c511bb7 100644 --- a/src/applications/policy/rule/PhabricatorPolicyRule.php +++ b/src/applications/policy/rule/PhabricatorPolicyRule.php @@ -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(); }