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:
Sam Minnee 2009-05-26 00:52:54 +00:00
parent b478c8be34
commit d8b46e5501
2 changed files with 35 additions and 18 deletions

View File

@ -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);
}
} }
} }

View File

@ -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,
); );
} }