protected function getOptions()
 {
     $capability = $this->capability;
     $options = array();
     foreach ($this->policies as $policy) {
         if ($policy->getPHID() == PhabricatorPolicies::POLICY_PUBLIC) {
             // Never expose "Public" for capabilities which don't support it.
             $capobj = PhabricatorPolicyCapability::getCapabilityByKey($capability);
             if (!$capobj || !$capobj->shouldAllowPublicPolicySetting()) {
                 continue;
             }
         }
         $policy_short_name = id(new PhutilUTF8StringTruncator())->setMaximumGlyphs(28)->truncateString($policy->getName());
         $options[$policy->getType()][$policy->getPHID()] = array('name' => $policy_short_name, 'full' => $policy->getName(), 'icon' => $policy->getIcon());
     }
     // If we were passed several custom policy options, throw away the ones
     // which aren't the value for this capability. For example, an object might
     // have a custom view pollicy and a custom edit policy. When we render
     // the selector for "Can View", we don't want to show the "Can Edit"
     // custom policy -- if we did, the menu would look like this:
     //
     //   Custom
     //     Custom Policy
     //     Custom Policy
     //
     // ...where one is the "view" custom policy, and one is the "edit" custom
     // policy.
     $type_custom = PhabricatorPolicyType::TYPE_CUSTOM;
     if (!empty($options[$type_custom])) {
         $options[$type_custom] = array_select_keys($options[$type_custom], array($this->getValue()));
     }
     // If there aren't any custom policies, add a placeholder policy so we
     // render a menu item. This allows the user to switch to a custom policy.
     if (empty($options[$type_custom])) {
         $placeholder = new PhabricatorPolicy();
         $placeholder->setName(pht('Custom Policy...'));
         $options[$type_custom][$this->getCustomPolicyPlaceholder()] = array('name' => $placeholder->getName(), 'full' => $placeholder->getName(), 'icon' => $placeholder->getIcon());
     }
     $options = array_select_keys($options, array(PhabricatorPolicyType::TYPE_GLOBAL, PhabricatorPolicyType::TYPE_USER, PhabricatorPolicyType::TYPE_CUSTOM, PhabricatorPolicyType::TYPE_PROJECT));
     return $options;
 }
 protected function getOptions()
 {
     $capability = $this->capability;
     $policies = $this->policies;
     // Exclude object policies which don't make sense here. This primarily
     // filters object policies associated from template capabilities (like
     // "Default Task View Policy" being set to "Task Author") so they aren't
     // made available on non-template capabilities (like "Can Bulk Edit").
     foreach ($policies as $key => $policy) {
         if ($policy->getType() != PhabricatorPolicyType::TYPE_OBJECT) {
             continue;
         }
         $rule = PhabricatorPolicyQuery::getObjectPolicyRule($policy->getPHID());
         if (!$rule) {
             continue;
         }
         $target = nonempty($this->templateObject, $this->object);
         if (!$rule->canApplyToObject($target)) {
             unset($policies[$key]);
             continue;
         }
     }
     $options = array();
     foreach ($policies as $policy) {
         if ($policy->getPHID() == PhabricatorPolicies::POLICY_PUBLIC) {
             // Never expose "Public" for capabilities which don't support it.
             $capobj = PhabricatorPolicyCapability::getCapabilityByKey($capability);
             if (!$capobj || !$capobj->shouldAllowPublicPolicySetting()) {
                 continue;
             }
         }
         $policy_short_name = id(new PhutilUTF8StringTruncator())->setMaximumGlyphs(28)->truncateString($policy->getName());
         $options[$policy->getType()][$policy->getPHID()] = array('name' => $policy_short_name, 'full' => $policy->getName(), 'icon' => $policy->getIcon());
     }
     // If we were passed several custom policy options, throw away the ones
     // which aren't the value for this capability. For example, an object might
     // have a custom view pollicy and a custom edit policy. When we render
     // the selector for "Can View", we don't want to show the "Can Edit"
     // custom policy -- if we did, the menu would look like this:
     //
     //   Custom
     //     Custom Policy
     //     Custom Policy
     //
     // ...where one is the "view" custom policy, and one is the "edit" custom
     // policy.
     $type_custom = PhabricatorPolicyType::TYPE_CUSTOM;
     if (!empty($options[$type_custom])) {
         $options[$type_custom] = array_select_keys($options[$type_custom], array($this->getValue()));
     }
     // If there aren't any custom policies, add a placeholder policy so we
     // render a menu item. This allows the user to switch to a custom policy.
     if (empty($options[$type_custom])) {
         $placeholder = new PhabricatorPolicy();
         $placeholder->setName(pht('Custom Policy...'));
         $options[$type_custom][$this->getCustomPolicyPlaceholder()] = array('name' => $placeholder->getName(), 'full' => $placeholder->getName(), 'icon' => $placeholder->getIcon());
     }
     $options = array_select_keys($options, array(PhabricatorPolicyType::TYPE_GLOBAL, PhabricatorPolicyType::TYPE_OBJECT, PhabricatorPolicyType::TYPE_USER, PhabricatorPolicyType::TYPE_CUSTOM, PhabricatorPolicyType::TYPE_PROJECT));
     return $options;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     $action_options = array(PhabricatorPolicy::ACTION_ALLOW => pht('Allow'), PhabricatorPolicy::ACTION_DENY => pht('Deny'));
     $rules = id(new PhutilSymbolLoader())->setAncestorClass('PhabricatorPolicyRule')->loadObjects();
     $rules = msort($rules, 'getRuleOrder');
     $default_rule = array('action' => head_key($action_options), 'rule' => head_key($rules), 'value' => null);
     if ($this->phid) {
         $policies = id(new PhabricatorPolicyQuery())->setViewer($viewer)->withPHIDs(array($this->phid))->execute();
         if (!$policies) {
             return new Aphront404Response();
         }
         $policy = head($policies);
     } else {
         $policy = id(new PhabricatorPolicy())->setRules(array($default_rule))->setDefaultAction(PhabricatorPolicy::ACTION_DENY);
     }
     $root_id = celerity_generate_unique_node_id();
     $default_action = $policy->getDefaultAction();
     $rule_data = $policy->getRules();
     $errors = array();
     if ($request->isFormPost()) {
         $data = $request->getStr('rules');
         $data = @json_decode($data, true);
         if (!is_array($data)) {
             throw new Exception('Failed to JSON decode rule data!');
         }
         $rule_data = array();
         foreach ($data as $rule) {
             $action = idx($rule, 'action');
             switch ($action) {
                 case 'allow':
                 case 'deny':
                     break;
                 default:
                     throw new Exception("Invalid action '{$action}'!");
             }
             $rule_class = idx($rule, 'rule');
             if (empty($rules[$rule_class])) {
                 throw new Exception("Invalid rule class '{$rule_class}'!");
             }
             $rule_obj = $rules[$rule_class];
             $value = $rule_obj->getValueForStorage(idx($rule, 'value'));
             $rule_data[] = array('action' => $action, 'rule' => $rule_class, 'value' => $value);
         }
         // Filter out nonsense rules, like a "users" rule without any users
         // actually specified.
         $valid_rules = array();
         foreach ($rule_data as $rule) {
             $rule_class = $rule['rule'];
             if ($rules[$rule_class]->ruleHasEffect($rule['value'])) {
                 $valid_rules[] = $rule;
             }
         }
         if (!$valid_rules) {
             $errors[] = pht('None of these policy rules have any effect.');
         }
         // NOTE: Policies are immutable once created, and we always create a new
         // policy here. If we didn't, we would need to lock this endpoint down,
         // as users could otherwise just go edit the policies of objects with
         // custom policies.
         if (!$errors) {
             $new_policy = new PhabricatorPolicy();
             $new_policy->setRules($valid_rules);
             $new_policy->setDefaultAction($request->getStr('default'));
             $new_policy->save();
             $data = array('phid' => $new_policy->getPHID(), 'info' => array('name' => $new_policy->getName(), 'full' => $new_policy->getName(), 'icon' => $new_policy->getIcon()));
             return id(new AphrontAjaxResponse())->setContent($data);
         }
     }
     // Convert rule values to display format (for example, expanding PHIDs
     // into tokens).
     foreach ($rule_data as $key => $rule) {
         $rule_data[$key]['value'] = $rules[$rule['rule']]->getValueForDisplay($viewer, $rule['value']);
     }
     $default_select = AphrontFormSelectControl::renderSelectTag($default_action, $action_options, array('name' => 'default'));
     if ($errors) {
         $errors = id(new AphrontErrorView())->setErrors($errors);
     }
     $form = id(new PHUIFormLayoutView())->appendChild($errors)->appendChild(javelin_tag('input', array('type' => 'hidden', 'name' => 'rules', 'sigil' => 'rules')))->appendChild(id(new AphrontFormInsetView())->setTitle(pht('Rules'))->setRightButton(javelin_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'create-rule', 'mustcapture' => true), pht('New Rule')))->setDescription(pht('These rules are processed in order.'))->setContent(javelin_tag('table', array('sigil' => 'rules', 'class' => 'policy-rules-table'), '')))->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('If No Rules Match'))->setValue(pht('%s all other users.', $default_select)));
     $form = phutil_tag('div', array('id' => $root_id), $form);
     $rule_options = mpull($rules, 'getRuleDescription');
     $type_map = mpull($rules, 'getValueControlType');
     $templates = mpull($rules, 'getValueControlTemplate');
     require_celerity_resource('policy-edit-css');
     Javelin::initBehavior('policy-rule-editor', array('rootID' => $root_id, 'actions' => $action_options, 'rules' => $rule_options, 'types' => $type_map, 'templates' => $templates, 'data' => $rule_data, 'defaultRule' => $default_rule));
     $dialog = id(new AphrontDialogView())->setWidth(AphrontDialogView::WIDTH_FULL)->setUser($viewer)->setTitle(pht('Edit Policy'))->appendChild($form)->addSubmitButton(pht('Save Policy'))->addCancelButton('#');
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 protected function getOptions()
 {
     $capability = $this->capability;
     $policies = $this->policies;
     $viewer = $this->getUser();
     // Check if we're missing the policy for the current control value. This
     // is unusual, but can occur if the user is submitting a form and selected
     // an unusual project as a policy but the change has not been saved yet.
     $policy_map = mpull($policies, null, 'getPHID');
     $value = $this->getValue();
     if ($value && empty($policy_map[$value])) {
         $handle = id(new PhabricatorHandleQuery())->setViewer($viewer)->withPHIDs(array($value))->executeOne();
         if ($handle->isComplete()) {
             $policies[] = PhabricatorPolicy::newFromPolicyAndHandle($value, $handle);
         }
     }
     // Exclude object policies which don't make sense here. This primarily
     // filters object policies associated from template capabilities (like
     // "Default Task View Policy" being set to "Task Author") so they aren't
     // made available on non-template capabilities (like "Can Bulk Edit").
     foreach ($policies as $key => $policy) {
         if ($policy->getType() != PhabricatorPolicyType::TYPE_OBJECT) {
             continue;
         }
         $rule = PhabricatorPolicyQuery::getObjectPolicyRule($policy->getPHID());
         if (!$rule) {
             continue;
         }
         $target = nonempty($this->templateObject, $this->object);
         if (!$rule->canApplyToObject($target)) {
             unset($policies[$key]);
             continue;
         }
     }
     $options = array();
     foreach ($policies as $policy) {
         if ($policy->getPHID() == PhabricatorPolicies::POLICY_PUBLIC) {
             // Never expose "Public" for capabilities which don't support it.
             $capobj = PhabricatorPolicyCapability::getCapabilityByKey($capability);
             if (!$capobj || !$capobj->shouldAllowPublicPolicySetting()) {
                 continue;
             }
         }
         $policy_short_name = id(new PhutilUTF8StringTruncator())->setMaximumGlyphs(28)->truncateString($policy->getName());
         $options[$policy->getType()][$policy->getPHID()] = array('name' => $policy_short_name, 'full' => $policy->getName(), 'icon' => $policy->getIcon(), 'sort' => phutil_utf8_strtolower($policy->getName()));
     }
     $type_project = PhabricatorPolicyType::TYPE_PROJECT;
     // Make sure we have a "Projects" group before we adjust it.
     if (empty($options[$type_project])) {
         $options[$type_project] = array();
     }
     $options[$type_project] = isort($options[$type_project], 'sort');
     $placeholder = id(new PhabricatorPolicy())->setName(pht('Other Project...'))->setIcon('fa-search');
     $options[$type_project][$this->getSelectProjectKey()] = array('name' => $placeholder->getName(), 'full' => $placeholder->getName(), 'icon' => $placeholder->getIcon());
     // If we were passed several custom policy options, throw away the ones
     // which aren't the value for this capability. For example, an object might
     // have a custom view policy and a custom edit policy. When we render
     // the selector for "Can View", we don't want to show the "Can Edit"
     // custom policy -- if we did, the menu would look like this:
     //
     //   Custom
     //     Custom Policy
     //     Custom Policy
     //
     // ...where one is the "view" custom policy, and one is the "edit" custom
     // policy.
     $type_custom = PhabricatorPolicyType::TYPE_CUSTOM;
     if (!empty($options[$type_custom])) {
         $options[$type_custom] = array_select_keys($options[$type_custom], array($this->getValue()));
     }
     // If there aren't any custom policies, add a placeholder policy so we
     // render a menu item. This allows the user to switch to a custom policy.
     if (empty($options[$type_custom])) {
         $placeholder = new PhabricatorPolicy();
         $placeholder->setName(pht('Custom Policy...'));
         $options[$type_custom][$this->getSelectCustomKey()] = array('name' => $placeholder->getName(), 'full' => $placeholder->getName(), 'icon' => $placeholder->getIcon());
     }
     $options = array_select_keys($options, array(PhabricatorPolicyType::TYPE_GLOBAL, PhabricatorPolicyType::TYPE_OBJECT, PhabricatorPolicyType::TYPE_USER, PhabricatorPolicyType::TYPE_CUSTOM, PhabricatorPolicyType::TYPE_PROJECT));
     return $options;
 }