From 8b9a80ecbb30127ae136ccba8938ac9883b54807 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Wed, 7 Sep 2022 14:05:40 +1200 Subject: [PATCH] ENH Extracting logic for canX from Company and Employee into DataObject Extension --- _config/extensions.yml | 7 +++++ code/Company.php | 40 +-------------------------- code/TestDataObjectExtension.php | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 code/TestDataObjectExtension.php diff --git a/_config/extensions.yml b/_config/extensions.yml index e22d64b..29a123c 100644 --- a/_config/extensions.yml +++ b/_config/extensions.yml @@ -7,6 +7,13 @@ SilverStripe\ORM\DataObject: SilverStripe\Assets\File: extensions: - BasicFieldsTestFileExtension + +SilverStripe\FrameworkTest\Model\Company: + extensions: + - SilverStripe\FrameworkTest\Extension\TestDataObjectExtension +SilverStripe\FrameworkTest\Model\Employee: + extensions: + - SilverStripe\FrameworkTest\Extension\TestDataObjectExtension --- Only: diff --git a/code/Company.php b/code/Company.php index 8e6ee41..c1cd974 100644 --- a/code/Company.php +++ b/code/Company.php @@ -13,8 +13,6 @@ use SilverStripe\Versioned\Versioned; use RelationFieldsTestPage; use GridFieldTestPage; -use SilverStripe\Security\Permission; -use SilverStripe\Security\PermissionProvider; /** * @@ -33,7 +31,7 @@ * @mixin Versioned * @mixin RecursivePublishable */ -class Company extends DataObject implements PermissionProvider +class Company extends DataObject { private static $table_name = 'Company'; @@ -373,40 +371,4 @@ 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/TestDataObjectExtension.php b/code/TestDataObjectExtension.php new file mode 100644 index 0000000..a59aeb4 --- /dev/null +++ b/code/TestDataObjectExtension.php @@ -0,0 +1,46 @@ + [ + 'name' => _t( + __CLASS__ . '.EditPermissionLabel', + 'Manage a test object' + ), + 'category' => _t( + __CLASS__ . '.Category', + 'Test Data Object' + ), + ], + ]; + } + + public function canView($member = null) + { + return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member); + } + + public function canEdit($member = null) + { + return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member); + } + + public function canDelete($member = null) + { + return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member); + } + + public function canCreate($member = null, $context = []) + { + return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member); + } +}