@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add storage for custom policies

Summary: Ref T603. Allows custom policies to be saved. No integration with policy controls yet.

Test Plan:
mysql> select * from policy where id = 3\G
*************************** 1. row ***************************
id: 3
phid: PHID-PLCY-e4v2fnbyuibi4supl5tn
rules: [{"action":"allow","rule":"PhabricatorPolicyRuleAdministrators","value":null},{"action":"allow","rule":"PhabricatorPolicyRuleProjects","value":["PHID-PROJ-cwovm5gn2ilubjehcdgd"]},{"action":"allow","rule":"PhabricatorPolicyRuleLunarPhase","value":"new"}]
defaultAction: deny
dateCreated: 1381437466
dateModified: 1381437466
1 row in set (0.00 sec)

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603

Differential Revision: https://secure.phabricator.com/D7282

+138 -25
+9
resources/sql/patches/20131010.pstorage.sql
··· 1 + CREATE TABLE {$NAMESPACE}_policy.policy ( 2 + id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 + phid VARCHAR(64) NOT NULL COLLATE utf8_bin, 4 + rules LONGTEXT NOT NULL COLLATE utf8_bin, 5 + defaultAction VARCHAR(32) NOT NULL COLLATE utf8_bin, 6 + dateCreated INT UNSIGNED NOT NULL, 7 + dateModified INT UNSIGNED NOT NULL, 8 + UNIQUE KEY (phid) 9 + ) ENGINE=InnoDB, COLLATE utf8_general_ci;
+6 -1
src/__phutil_library_map__.php
··· 1472 1472 'PhabricatorPhrequentConfigOptions' => 'applications/phrequent/config/PhabricatorPhrequentConfigOptions.php', 1473 1473 'PhabricatorPhrictionConfigOptions' => 'applications/phriction/config/PhabricatorPhrictionConfigOptions.php', 1474 1474 'PhabricatorPolicies' => 'applications/policy/constants/PhabricatorPolicies.php', 1475 - 'PhabricatorPolicy' => 'applications/policy/filter/PhabricatorPolicy.php', 1475 + 'PhabricatorPolicy' => 'applications/policy/storage/PhabricatorPolicy.php', 1476 1476 'PhabricatorPolicyAwareQuery' => 'infrastructure/query/policy/PhabricatorPolicyAwareQuery.php', 1477 1477 'PhabricatorPolicyAwareTestQuery' => 'applications/policy/__tests__/PhabricatorPolicyAwareTestQuery.php', 1478 1478 'PhabricatorPolicyCapability' => 'applications/policy/capability/PhabricatorPolicyCapability.php', ··· 1482 1482 'PhabricatorPolicyConfigOptions' => 'applications/policy/config/PhabricatorPolicyConfigOptions.php', 1483 1483 'PhabricatorPolicyConstants' => 'applications/policy/constants/PhabricatorPolicyConstants.php', 1484 1484 'PhabricatorPolicyController' => 'applications/policy/controller/PhabricatorPolicyController.php', 1485 + 'PhabricatorPolicyDAO' => 'applications/policy/storage/PhabricatorPolicyDAO.php', 1485 1486 'PhabricatorPolicyDataTestCase' => 'applications/policy/__tests__/PhabricatorPolicyDataTestCase.php', 1486 1487 'PhabricatorPolicyEditController' => 'applications/policy/controller/PhabricatorPolicyEditController.php', 1487 1488 'PhabricatorPolicyException' => 'applications/policy/exception/PhabricatorPolicyException.php', ··· 1491 1492 'PhabricatorPolicyManagementShowWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php', 1492 1493 'PhabricatorPolicyManagementUnlockWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementUnlockWorkflow.php', 1493 1494 'PhabricatorPolicyManagementWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementWorkflow.php', 1495 + 'PhabricatorPolicyPHIDTypePolicy' => 'applications/policy/phid/PhabricatorPolicyPHIDTypePolicy.php', 1494 1496 'PhabricatorPolicyQuery' => 'applications/policy/query/PhabricatorPolicyQuery.php', 1495 1497 'PhabricatorPolicyRule' => 'applications/policy/rule/PhabricatorPolicyRule.php', 1496 1498 'PhabricatorPolicyRuleAdministrators' => 'applications/policy/rule/PhabricatorPolicyRuleAdministrators.php', ··· 3670 3672 'PhabricatorPhrequentConfigOptions' => 'PhabricatorApplicationConfigOptions', 3671 3673 'PhabricatorPhrictionConfigOptions' => 'PhabricatorApplicationConfigOptions', 3672 3674 'PhabricatorPolicies' => 'PhabricatorPolicyConstants', 3675 + 'PhabricatorPolicy' => 'PhabricatorPolicyDAO', 3673 3676 'PhabricatorPolicyAwareQuery' => 'PhabricatorOffsetPagedQuery', 3674 3677 'PhabricatorPolicyAwareTestQuery' => 'PhabricatorPolicyAwareQuery', 3675 3678 'PhabricatorPolicyCapability' => 'Phobject', ··· 3678 3681 'PhabricatorPolicyCapabilityCanView' => 'PhabricatorPolicyCapability', 3679 3682 'PhabricatorPolicyConfigOptions' => 'PhabricatorApplicationConfigOptions', 3680 3683 'PhabricatorPolicyController' => 'PhabricatorController', 3684 + 'PhabricatorPolicyDAO' => 'PhabricatorLiskDAO', 3681 3685 'PhabricatorPolicyDataTestCase' => 'PhabricatorTestCase', 3682 3686 'PhabricatorPolicyEditController' => 'PhabricatorPolicyController', 3683 3687 'PhabricatorPolicyException' => 'Exception', ··· 3685 3689 'PhabricatorPolicyManagementShowWorkflow' => 'PhabricatorPolicyManagementWorkflow', 3686 3690 'PhabricatorPolicyManagementUnlockWorkflow' => 'PhabricatorPolicyManagementWorkflow', 3687 3691 'PhabricatorPolicyManagementWorkflow' => 'PhutilArgumentWorkflow', 3692 + 'PhabricatorPolicyPHIDTypePolicy' => 'PhabricatorPHIDType', 3688 3693 'PhabricatorPolicyQuery' => 'PhabricatorQuery', 3689 3694 'PhabricatorPolicyRuleAdministrators' => 'PhabricatorPolicyRule', 3690 3695 'PhabricatorPolicyRuleLunarPhase' => 'PhabricatorPolicyRule',
+9 -3
src/applications/policy/controller/PhabricatorPolicyEditController.php
··· 7 7 $request = $this->getRequest(); 8 8 $viewer = $request->getUser(); 9 9 10 + $policy = new PhabricatorPolicy(); 11 + 10 12 $root_id = celerity_generate_unique_node_id(); 11 13 12 14 $action_options = array( ··· 53 55 $rule_obj = $rules[$rule_class]; 54 56 55 57 $value = $rule_obj->getValueForStorage(idx($rule, 'value')); 56 - $value = $rule_obj->getValueForDisplay($viewer, $value); 57 58 58 59 $rule_data[] = array( 59 60 'action' => $action, ··· 62 63 ); 63 64 } 64 65 65 - $default_value = $request->getStr('default'); 66 + $policy->setRules($rule_data); 67 + $policy->setDefaultAction($request->getStr('default')); 68 + $policy->save(); 69 + 70 + // TODO: Integrate with policy editors. 71 + $id = $policy->getID(); 72 + throw new Exception("OK, saved policy {$id}!"); 66 73 } else { 67 74 $rule_data = array( 68 75 $default_rule, ··· 75 82 array( 76 83 'name' => 'default', 77 84 )); 78 - 79 85 80 86 $form = id(new PHUIFormLayoutView()) 81 87 ->appendChild(
+24 -11
src/applications/policy/filter/PhabricatorPolicy.php src/applications/policy/storage/PhabricatorPolicy.php
··· 1 1 <?php 2 2 3 - final class PhabricatorPolicy { 3 + final class PhabricatorPolicy 4 + extends PhabricatorPolicyDAO { 5 + 6 + const ACTION_ACCEPT = 'accept'; 7 + const ACTION_DENY = 'deny'; 4 8 5 - private $phid; 6 9 private $name; 7 10 private $type; 8 11 private $href; 9 12 private $icon; 10 13 14 + protected $rules = array(); 15 + protected $defaultAction = self::ACTION_DENY; 16 + 17 + public function getConfiguration() { 18 + return array( 19 + self::CONFIG_AUX_PHID => true, 20 + self::CONFIG_SERIALIZATION => array( 21 + 'rules' => self::SERIALIZATION_JSON, 22 + ), 23 + ) + parent::getConfiguration(); 24 + } 25 + 26 + public function generatePHID() { 27 + return PhabricatorPHID::generateNewPHID( 28 + PhabricatorPolicyPHIDTypePolicy::TYPECONST); 29 + } 30 + 11 31 public static function newFromPolicyAndHandle( 12 32 $policy_identifier, 13 33 PhabricatorObjectHandle $handle = null) { ··· 48 68 break; 49 69 } 50 70 71 + $policy->makeEphemeral(); 72 + 51 73 return $policy; 52 74 } 53 75 ··· 67 89 68 90 public function getName() { 69 91 return $this->name; 70 - } 71 - 72 - public function setPHID($phid) { 73 - $this->phid = $phid; 74 - return $this; 75 - } 76 - 77 - public function getPHID() { 78 - return $this->phid; 79 92 } 80 93 81 94 public function setHref($href) {
+48
src/applications/policy/phid/PhabricatorPolicyPHIDTypePolicy.php
··· 1 + <?php 2 + 3 + final class PhabricatorPolicyPHIDTypePolicy 4 + extends PhabricatorPHIDType { 5 + 6 + const TYPECONST = 'PLCY'; 7 + 8 + public function getTypeConstant() { 9 + return self::TYPECONST; 10 + } 11 + 12 + public function getTypeName() { 13 + return pht('Policy'); 14 + } 15 + 16 + public function newObject() { 17 + return new PhabricatorPolicy(); 18 + } 19 + 20 + public function loadObjects( 21 + PhabricatorObjectQuery $query, 22 + array $phids) { 23 + 24 + return id(new PhabricatorPolicyQuery()) 25 + ->setViewer($query->getViewer()) 26 + ->setParentQuery($query) 27 + ->withPHIDs($phids) 28 + ->execute(); 29 + } 30 + 31 + public function loadHandles( 32 + PhabricatorHandleQuery $query, 33 + array $handles, 34 + array $objects) { 35 + 36 + foreach ($handles as $phid => $handle) { 37 + $policy = $objects[$phid]; 38 + 39 + $handle->setName($policy->getName()); 40 + $handle->setURI($policy->getHref()); 41 + } 42 + } 43 + 44 + public function canLoadNamedObject($name) { 45 + return false; 46 + } 47 + 48 + }
+25 -10
src/applications/policy/query/PhabricatorPolicyQuery.php
··· 4 4 5 5 private $viewer; 6 6 private $object; 7 + private $phids; 7 8 8 9 public function setViewer(PhabricatorUser $viewer) { 9 10 $this->viewer = $viewer; ··· 12 13 13 14 public function setObject(PhabricatorPolicyInterface $object) { 14 15 $this->object = $object; 16 + return $this; 17 + } 18 + 19 + public function withPHIDs(array $phids) { 20 + $this->phids = $phids; 15 21 return $this; 16 22 } 17 23 ··· 68 74 if (!$this->viewer) { 69 75 throw new Exception('Call setViewer() before execute()!'); 70 76 } 71 - if (!$this->object) { 72 - throw new Exception('Call setObject() before execute()!'); 73 - } 74 77 75 78 $results = $this->getGlobalPolicies(); 76 79 ··· 93 96 $results = mpull($results, null, 'getPHID'); 94 97 95 98 $other_policies = array(); 96 - $capabilities = $this->object->getCapabilities(); 97 - foreach ($capabilities as $capability) { 98 - $policy = $this->object->getPolicy($capability); 99 - if (!$policy) { 100 - continue; 99 + if ($this->object) { 100 + $capabilities = $this->object->getCapabilities(); 101 + foreach ($capabilities as $capability) { 102 + $policy = $this->object->getPolicy($capability); 103 + if (!$policy) { 104 + continue; 105 + } 106 + $other_policies[$policy] = $policy; 101 107 } 102 - $other_policies[$policy] = $policy; 103 108 } 104 109 105 110 // If this install doesn't have "Public" enabled, remove it as an option ··· 127 132 128 133 $results = msort($results, 'getSortKey'); 129 134 135 + if ($this->phids) { 136 + $phids = array_fuse($this->phids); 137 + foreach ($results as $key => $result) { 138 + if (empty($phids[$result->getPHID()])) { 139 + unset($results[$key]); 140 + } 141 + } 142 + } 143 + 130 144 return $results; 131 145 } 132 146 ··· 160 174 $results[$constant] = id(new PhabricatorPolicy()) 161 175 ->setType(PhabricatorPolicyType::TYPE_GLOBAL) 162 176 ->setPHID($constant) 163 - ->setName(self::getGlobalPolicyName($constant)); 177 + ->setName(self::getGlobalPolicyName($constant)) 178 + ->makeEphemeral(); 164 179 } 165 180 166 181 return $results;
+9
src/applications/policy/storage/PhabricatorPolicyDAO.php
··· 1 + <?php 2 + 3 + abstract class PhabricatorPolicyDAO extends PhabricatorLiskDAO { 4 + 5 + public function getApplicationName() { 6 + return 'policy'; 7 + } 8 + 9 + }
+8
src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php
··· 204 204 'type' => 'db', 205 205 'name' => 'legalpad', 206 206 ), 207 + 'db.policy' => array( 208 + 'type' => 'db', 209 + 'name' => 'policy', 210 + ), 207 211 '0000.legacy.sql' => array( 208 212 'type' => 'sql', 209 213 'name' => $this->getPatchPath('0000.legacy.sql'), ··· 1663 1667 '20131006.hdisable.sql' => array( 1664 1668 'type' => 'sql', 1665 1669 'name' => $this->getPatchPath('20131006.hdisable.sql'), 1670 + ), 1671 + '20131010.pstorage.sql' => array( 1672 + 'type' => 'sql', 1673 + 'name' => $this->getPatchPath('20131010.pstorage.sql'), 1666 1674 ), 1667 1675 ); 1668 1676 }