From f5d2f8e07a045bcb13dbf3a690c571a46748a39a Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 22 Jul 2016 06:03:31 -0700 Subject: [PATCH] Add default create, view, edit capabilities to Packages Summary: Ref T8116. This adds a control for creating publishers (default: administrators) and default publisher/package edit controls. I've left the edit defaults at "no one" for now to force you to select a policy. This might be something to look at later. Test Plan: Created publishers, packages. Tried to create publishers with "can create" policy set restrictively. Reviewers: chad Reviewed By: chad Maniphest Tasks: T8116 Differential Revision: https://secure.phabricator.com/D16319 --- src/__phutil_library_map__.php | 8 +++++++ .../PhabricatorPackagesApplication.php | 21 +++++++++++++++++++ ...catorPackagesCreatePublisherCapability.php | 16 ++++++++++++++ ...orPackagesPackageDefaultEditCapability.php | 12 +++++++++++ ...orPackagesPackageDefaultViewCapability.php | 16 ++++++++++++++ ...PackagesPublisherDefaultEditCapability.php | 12 +++++++++++ ...PhabricatorPackagesPublisherEditEngine.php | 5 +++++ .../storage/PhabricatorPackagesPackage.php | 15 ++++++++++++- .../storage/PhabricatorPackagesPublisher.php | 11 +++++++++- 9 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/applications/packages/capability/PhabricatorPackagesCreatePublisherCapability.php create mode 100644 src/applications/packages/capability/PhabricatorPackagesPackageDefaultEditCapability.php create mode 100644 src/applications/packages/capability/PhabricatorPackagesPackageDefaultViewCapability.php create mode 100644 src/applications/packages/capability/PhabricatorPackagesPublisherDefaultEditCapability.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index b85da8bfac..6190d055c9 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2974,12 +2974,15 @@ phutil_register_library_map(array( 'PhabricatorPHPMailerConfigOptions' => 'applications/config/option/PhabricatorPHPMailerConfigOptions.php', 'PhabricatorPackagesApplication' => 'applications/packages/application/PhabricatorPackagesApplication.php', 'PhabricatorPackagesController' => 'applications/packages/controller/PhabricatorPackagesController.php', + 'PhabricatorPackagesCreatePublisherCapability' => 'applications/packages/capability/PhabricatorPackagesCreatePublisherCapability.php', 'PhabricatorPackagesDAO' => 'applications/packages/storage/PhabricatorPackagesDAO.php', 'PhabricatorPackagesEditEngine' => 'applications/packages/editor/PhabricatorPackagesEditEngine.php', 'PhabricatorPackagesEditor' => 'applications/packages/editor/PhabricatorPackagesEditor.php', 'PhabricatorPackagesPackage' => 'applications/packages/storage/PhabricatorPackagesPackage.php', 'PhabricatorPackagesPackageController' => 'applications/packages/controller/PhabricatorPackagesPackageController.php', 'PhabricatorPackagesPackageDatasource' => 'applications/packages/typeahead/PhabricatorPackagesPackageDatasource.php', + 'PhabricatorPackagesPackageDefaultEditCapability' => 'applications/packages/capability/PhabricatorPackagesPackageDefaultEditCapability.php', + 'PhabricatorPackagesPackageDefaultViewCapability' => 'applications/packages/capability/PhabricatorPackagesPackageDefaultViewCapability.php', 'PhabricatorPackagesPackageEditConduitAPIMethod' => 'applications/packages/conduit/PhabricatorPackagesPackageEditConduitAPIMethod.php', 'PhabricatorPackagesPackageEditController' => 'applications/packages/controller/PhabricatorPackagesPackageEditController.php', 'PhabricatorPackagesPackageEditEngine' => 'applications/packages/editor/PhabricatorPackagesPackageEditEngine.php', @@ -2999,6 +3002,7 @@ phutil_register_library_map(array( 'PhabricatorPackagesPublisher' => 'applications/packages/storage/PhabricatorPackagesPublisher.php', 'PhabricatorPackagesPublisherController' => 'applications/packages/controller/PhabricatorPackagesPublisherController.php', 'PhabricatorPackagesPublisherDatasource' => 'applications/packages/typeahead/PhabricatorPackagesPublisherDatasource.php', + 'PhabricatorPackagesPublisherDefaultEditCapability' => 'applications/packages/capability/PhabricatorPackagesPublisherDefaultEditCapability.php', 'PhabricatorPackagesPublisherEditConduitAPIMethod' => 'applications/packages/conduit/PhabricatorPackagesPublisherEditConduitAPIMethod.php', 'PhabricatorPackagesPublisherEditController' => 'applications/packages/controller/PhabricatorPackagesPublisherEditController.php', 'PhabricatorPackagesPublisherEditEngine' => 'applications/packages/editor/PhabricatorPackagesPublisherEditEngine.php', @@ -7789,6 +7793,7 @@ phutil_register_library_map(array( 'PhabricatorPHPMailerConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorPackagesApplication' => 'PhabricatorApplication', 'PhabricatorPackagesController' => 'PhabricatorController', + 'PhabricatorPackagesCreatePublisherCapability' => 'PhabricatorPolicyCapability', 'PhabricatorPackagesDAO' => 'PhabricatorLiskDAO', 'PhabricatorPackagesEditEngine' => 'PhabricatorEditEngine', 'PhabricatorPackagesEditor' => 'PhabricatorApplicationTransactionEditor', @@ -7803,6 +7808,8 @@ phutil_register_library_map(array( ), 'PhabricatorPackagesPackageController' => 'PhabricatorPackagesController', 'PhabricatorPackagesPackageDatasource' => 'PhabricatorTypeaheadDatasource', + 'PhabricatorPackagesPackageDefaultEditCapability' => 'PhabricatorPolicyCapability', + 'PhabricatorPackagesPackageDefaultViewCapability' => 'PhabricatorPolicyCapability', 'PhabricatorPackagesPackageEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod', 'PhabricatorPackagesPackageEditController' => 'PhabricatorPackagesPackageController', 'PhabricatorPackagesPackageEditEngine' => 'PhabricatorPackagesEditEngine', @@ -7830,6 +7837,7 @@ phutil_register_library_map(array( ), 'PhabricatorPackagesPublisherController' => 'PhabricatorPackagesController', 'PhabricatorPackagesPublisherDatasource' => 'PhabricatorTypeaheadDatasource', + 'PhabricatorPackagesPublisherDefaultEditCapability' => 'PhabricatorPolicyCapability', 'PhabricatorPackagesPublisherEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod', 'PhabricatorPackagesPublisherEditController' => 'PhabricatorPackagesPublisherController', 'PhabricatorPackagesPublisherEditEngine' => 'PhabricatorPackagesEditEngine', diff --git a/src/applications/packages/application/PhabricatorPackagesApplication.php b/src/applications/packages/application/PhabricatorPackagesApplication.php index e52a26310d..7a100e82b8 100644 --- a/src/applications/packages/application/PhabricatorPackagesApplication.php +++ b/src/applications/packages/application/PhabricatorPackagesApplication.php @@ -26,6 +26,27 @@ final class PhabricatorPackagesApplication extends PhabricatorApplication { return true; } + protected function getCustomCapabilities() { + return array( + PhabricatorPackagesCreatePublisherCapability::CAPABILITY => array( + 'default' => PhabricatorPolicies::POLICY_ADMIN, + ), + PhabricatorPackagesPublisherDefaultEditCapability::CAPABILITY => array( + 'caption' => pht('Default edit policy for newly created publishers.'), + 'template' => PhabricatorPackagesPublisherPHIDType::TYPECONST, + 'default' => PhabricatorPolicies::POLICY_NOONE, + ), + PhabricatorPackagesPackageDefaultViewCapability::CAPABILITY => array( + 'caption' => pht('Default edit policy for newly created packages.'), + 'template' => PhabricatorPackagesPackagePHIDType::TYPECONST, + ), + PhabricatorPackagesPackageDefaultEditCapability::CAPABILITY => array( + 'caption' => pht('Default view policy for newly created packages.'), + 'template' => PhabricatorPackagesPackagePHIDType::TYPECONST, + 'default' => PhabricatorPolicies::POLICY_NOONE, + ), + ); + } public function getRoutes() { return array( '/package/' => array( diff --git a/src/applications/packages/capability/PhabricatorPackagesCreatePublisherCapability.php b/src/applications/packages/capability/PhabricatorPackagesCreatePublisherCapability.php new file mode 100644 index 0000000000..d744c35438 --- /dev/null +++ b/src/applications/packages/capability/PhabricatorPackagesCreatePublisherCapability.php @@ -0,0 +1,16 @@ +getURI(); } + protected function getCreateNewObjectPolicy() { + return $this->getApplication()->getPolicy( + PhabricatorPackagesCreatePublisherCapability::CAPABILITY); + } + protected function buildCustomEditFields($object) { $fields = array(); diff --git a/src/applications/packages/storage/PhabricatorPackagesPackage.php b/src/applications/packages/storage/PhabricatorPackagesPackage.php index 472e145900..57fe32e1af 100644 --- a/src/applications/packages/storage/PhabricatorPackagesPackage.php +++ b/src/applications/packages/storage/PhabricatorPackagesPackage.php @@ -19,7 +19,20 @@ final class PhabricatorPackagesPackage private $publisher = self::ATTACHABLE; public static function initializeNewPackage(PhabricatorUser $actor) { - return id(new self()); + $packages_application = id(new PhabricatorApplicationQuery()) + ->setViewer($actor) + ->withClasses(array('PhabricatorPackagesApplication')) + ->executeOne(); + + $view_policy = $packages_application->getPolicy( + PhabricatorPackagesPackageDefaultViewCapability::CAPABILITY); + + $edit_policy = $packages_application->getPolicy( + PhabricatorPackagesPackageDefaultEditCapability::CAPABILITY); + + return id(new self()) + ->setViewPolicy($view_policy) + ->setEditPolicy($edit_policy); } protected function getConfiguration() { diff --git a/src/applications/packages/storage/PhabricatorPackagesPublisher.php b/src/applications/packages/storage/PhabricatorPackagesPublisher.php index 80c797fdb6..09e417f763 100644 --- a/src/applications/packages/storage/PhabricatorPackagesPublisher.php +++ b/src/applications/packages/storage/PhabricatorPackagesPublisher.php @@ -15,7 +15,16 @@ final class PhabricatorPackagesPublisher protected $editPolicy; public static function initializeNewPublisher(PhabricatorUser $actor) { - return id(new self()); + $packages_application = id(new PhabricatorApplicationQuery()) + ->setViewer($actor) + ->withClasses(array('PhabricatorPackagesApplication')) + ->executeOne(); + + $edit_policy = $packages_application->getPolicy( + PhabricatorPackagesPublisherDefaultEditCapability::CAPABILITY); + + return id(new self()) + ->setEditPolicy($edit_policy); } protected function getConfiguration() {