From c34dd2158b791328e90ebdc525f292b2a96ef72e Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 9 Aug 2012 09:45:07 -0700 Subject: [PATCH] Enhance policy control Summary: Make the policy control accept a more sensible set of inputs. (This currently has no callsites.) Test Plan: Used in future diff. Reviewers: vrana, btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D3211 --- .../form/control/AphrontFormPolicyControl.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/view/form/control/AphrontFormPolicyControl.php b/src/view/form/control/AphrontFormPolicyControl.php index 472bf29ff7..40dac998ad 100644 --- a/src/view/form/control/AphrontFormPolicyControl.php +++ b/src/view/form/control/AphrontFormPolicyControl.php @@ -18,6 +18,10 @@ final class AphrontFormPolicyControl extends AphrontFormControl { + private $user; + private $object; + private $capability; + public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; @@ -27,6 +31,25 @@ final class AphrontFormPolicyControl extends AphrontFormControl { return $this->user; } + public function setPolicyObject(PhabricatorPolicyInterface $object) { + $this->object = $object; + return $this; + } + + public function setCapability($capability) { + $this->capability = $capability; + + $labels = array( + PhabricatorPolicyCapability::CAN_VIEW => 'Visible To', + PhabricatorPolicyCapability::CAN_EDIT => 'Editable By', + PhabricatorPolicyCapability::CAN_JOIN => 'Joinable By', + ); + + $this->setLabel(idx($labels, $this->capability, 'Unknown Policy')); + + return $this; + } + protected function getCustomControlClass() { return 'aphront-form-control-policy'; } @@ -34,6 +57,11 @@ final class AphrontFormPolicyControl extends AphrontFormControl { private function getOptions() { $show_public = PhabricatorEnv::getEnvConfig('policy.allow-public'); + if ($this->capability != PhabricatorPolicyCapability::CAN_VIEW) { + // We don't generally permit 'public' for anything except viewing. + $show_public = false; + } + if ($this->getValue() == PhabricatorPolicies::POLICY_PUBLIC) { // If the object already has a "public" policy, show the option in // the dropdown even if it will be enforced as "users", so we don't @@ -59,6 +87,20 @@ final class AphrontFormPolicyControl extends AphrontFormControl { } protected function renderInput() { + if (!$this->object) { + throw new Exception("Call setPolicyObject() before rendering!"); + } + if (!$this->capability) { + throw new Exception("Call setCapability() before rendering!"); + } + + $policy = $this->object->getPolicy($this->capability); + if (!$policy) { + // TODO: Make this configurable. + $policy = PhabricatorPolicies::POLICY_USER; + } + $this->setValue($policy); + return AphrontFormSelectControl::renderSelectTag( $this->getValue(), $this->getOptions(),