From 69fcc2f1bf1ab398c3b4f25c6c33b860200ba8a5 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Mon, 29 Aug 2022 16:53:53 +1200 Subject: [PATCH] ENH Add permissions for Content Editor --- code/Company.php | 41 ++++++++++++++++++++- code/elemental/ElementalBehatTestAdmin.php | 3 ++ code/elemental/ElementalBehatTestObject.php | 22 +++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/code/Company.php b/code/Company.php index 6b93f03..8e6ee41 100644 --- a/code/Company.php +++ b/code/Company.php @@ -13,6 +13,8 @@ use SilverStripe\Versioned\Versioned; use RelationFieldsTestPage; use GridFieldTestPage; +use SilverStripe\Security\Permission; +use SilverStripe\Security\PermissionProvider; /** * @@ -31,7 +33,7 @@ * @mixin Versioned * @mixin RecursivePublishable */ -class Company extends DataObject +class Company extends DataObject implements PermissionProvider { private static $table_name = 'Company'; @@ -370,4 +372,41 @@ public function scaffoldSearchField() { return DropdownField::create('CompanyID', 'Company', self::get()->map())->setEmptyString(''); } + + public function providePermissions() + { + return [ + 'COMPANY_EDIT' => [ + 'name' => _t( + __CLASS__ . '.EditPermissionLabel', + 'Edit a company' + ), + 'category' => _t( + __CLASS__ . '.Category', + 'Company' + ), + ], + ]; + } + + public function canView($member = null) + { + return Permission::check('COMPANY_EDIT', 'any', $member); + } + + public function canEdit($member = null) + { + return Permission::check('COMPANY_EDIT', 'any', $member); + } + + public function canDelete($member = null) + { + return Permission::check('COMPANY_EDIT', 'any', $member); + } + + public function canCreate($member = null, $context = []) + { + return Permission::check('COMPANY_EDIT', 'any', $member); + } + } diff --git a/code/elemental/ElementalBehatTestAdmin.php b/code/elemental/ElementalBehatTestAdmin.php index 8091419..da5eb8f 100644 --- a/code/elemental/ElementalBehatTestAdmin.php +++ b/code/elemental/ElementalBehatTestAdmin.php @@ -14,4 +14,7 @@ class ElementalBehatTestAdmin extends ModelAdmin private static $managed_models = [ ElementalBehatTestObject::class, ]; + + private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; + } diff --git a/code/elemental/ElementalBehatTestObject.php b/code/elemental/ElementalBehatTestObject.php index 864084e..007d0db 100644 --- a/code/elemental/ElementalBehatTestObject.php +++ b/code/elemental/ElementalBehatTestObject.php @@ -5,6 +5,7 @@ use SilverStripe\FrameworkTest\Elemental\Admin\ElementalBehatTestAdmin; use SilverStripe\Control\Controller; use SilverStripe\ORM\DataObject; +use SilverStripe\Security\Permission; class ElementalBehatTestObject extends DataObject { @@ -23,4 +24,25 @@ public function CMSEditLink() $this->ID, ); } + + public function canView($member = null) + { + return Permission::check(ElementalBehatTestAdmin::getRequiredPermissions() , 'any', $member); + } + + public function canEdit($member = null) + { + return Permission::check(ElementalBehatTestAdmin::getRequiredPermissions(), 'any', $member); + } + + public function canDelete($member = null) + { + return Permission::check(ElementalBehatTestAdmin::getRequiredPermissions(), 'any', $member); + } + + public function canCreate($member = null, $context = []) + { + return Permission::check(ElementalBehatTestAdmin::getRequiredPermissions(), 'any', $member); + } + }