ENH Extracting logic for canX from Company and Employee into DataObject Extension

This commit is contained in:
Sabina Talipova 2022-09-07 14:05:40 +12:00
parent 69fcc2f1bf
commit 8b9a80ecbb
3 changed files with 54 additions and 39 deletions

View File

@ -8,6 +8,13 @@ SilverStripe\Assets\File:
extensions: extensions:
- BasicFieldsTestFileExtension - BasicFieldsTestFileExtension
SilverStripe\FrameworkTest\Model\Company:
extensions:
- SilverStripe\FrameworkTest\Extension\TestDataObjectExtension
SilverStripe\FrameworkTest\Model\Employee:
extensions:
- SilverStripe\FrameworkTest\Extension\TestDataObjectExtension
--- ---
Only: Only:
moduleexists: 'dnadesign/silverstripe-elemental' moduleexists: 'dnadesign/silverstripe-elemental'

View File

@ -13,8 +13,6 @@ use SilverStripe\Versioned\RecursivePublishable;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use RelationFieldsTestPage; use RelationFieldsTestPage;
use GridFieldTestPage; use GridFieldTestPage;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;
/** /**
* *
@ -33,7 +31,7 @@ use SilverStripe\Security\PermissionProvider;
* @mixin Versioned * @mixin Versioned
* @mixin RecursivePublishable * @mixin RecursivePublishable
*/ */
class Company extends DataObject implements PermissionProvider class Company extends DataObject
{ {
private static $table_name = 'Company'; private static $table_name = 'Company';
@ -373,40 +371,4 @@ class Company extends DataObject implements PermissionProvider
return DropdownField::create('CompanyID', 'Company', self::get()->map())->setEmptyString(''); 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);
}
} }

View File

@ -0,0 +1,46 @@
<?php
namespace SilverStripe\FrameworkTest\Extension;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;
class TestDataObjectExtension extends DataExtension implements PermissionProvider
{
public function providePermissions()
{
return [
'TEST_DATAOBJECT_EDIT' => [
'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);
}
}