mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: Allow altering of DataObject:$api_access by decorators.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77787 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
b478c8be34
commit
d8b46e5501
@ -13,18 +13,22 @@ abstract class DataObjectDecorator extends Extension {
|
|||||||
* which can be decorated onto. This list is
|
* which can be decorated onto. This list is
|
||||||
* limited for security and performance reasons.
|
* limited for security and performance reasons.
|
||||||
*
|
*
|
||||||
|
* Keys are the static names, and the values are whether or not the value is an array that should
|
||||||
|
* be merged.
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $decoratable_statics = array(
|
protected static $decoratable_statics = array(
|
||||||
'db',
|
'db' => true,
|
||||||
'has_one',
|
'has_one' => true,
|
||||||
'indexes',
|
'indexes' => true,
|
||||||
'defaults',
|
'defaults' => true,
|
||||||
'has_many',
|
'has_many' => true,
|
||||||
'many_many',
|
'many_many' => true,
|
||||||
'belongs_many_many',
|
'belongs_many_many' => true,
|
||||||
'many_many_extraFields',
|
'many_many_extraFields' => true,
|
||||||
'searchable_fields',
|
'searchable_fields' => true,
|
||||||
|
'api_access' => false,
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,14 +57,19 @@ abstract class DataObjectDecorator extends Extension {
|
|||||||
if(Object::has_extension($this->owner->parentClass(), $this->class)) return;
|
if(Object::has_extension($this->owner->parentClass(), $this->class)) return;
|
||||||
|
|
||||||
if($fields = $this->extraStatics()) {
|
if($fields = $this->extraStatics()) {
|
||||||
foreach($fields as $relation => $fields) {
|
foreach($fields as $relation => $newVal) {
|
||||||
if(in_array($relation, self::$decoratable_statics)) {
|
if(isset(self::$decoratable_statics[$relation])) {
|
||||||
// Can't use add_static_var() here as it would merge the array rather than replacing
|
$origVal = Object::get_static($this->owner->class, $relation);
|
||||||
Object::set_static (
|
|
||||||
$this->owner->class,
|
// Array to be merged
|
||||||
$relation,
|
if(self::$decoratable_statics[$relation]) {
|
||||||
array_merge((array) Object::get_static($this->owner->class, $relation), $fields)
|
// Can't use add_static_var() here as it would merge the array rather than replacing
|
||||||
);
|
Object::set_static($this->owner->class, $relation, array_merge((array)$origVal, $newVal));
|
||||||
|
|
||||||
|
// Value to be overwritten
|
||||||
|
} else {
|
||||||
|
Object::set_static ($this->owner->class, $relation, $newVal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,13 @@ class DataObjectDecoratorTest extends SapphireTest {
|
|||||||
$contact->delete();
|
$contact->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that DataObject::$api_access can be set to true via a decorator
|
||||||
|
*/
|
||||||
|
function testApiAccessCanBeDecorated() {
|
||||||
|
$this->assertTrue(Object::get_static('DataObjectDecoratorTest_Member', 'api_access'));
|
||||||
|
}
|
||||||
|
|
||||||
function testPermissionDecoration() {
|
function testPermissionDecoration() {
|
||||||
// testing behaviour in isolation, too many sideeffects and other checks
|
// testing behaviour in isolation, too many sideeffects and other checks
|
||||||
// in SiteTree->can*() methods to test one single feature reliably with them
|
// in SiteTree->can*() methods to test one single feature reliably with them
|
||||||
@ -97,7 +104,8 @@ class DataObjectDecoratorTest_ContactRole extends DataObjectDecorator implements
|
|||||||
),
|
),
|
||||||
'defaults' => array(
|
'defaults' => array(
|
||||||
'Phone' => '123'
|
'Phone' => '123'
|
||||||
)
|
),
|
||||||
|
'api_access' => true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user