BUGFIX check if parent context is SiteTree instance

fixes #1913
This commit is contained in:
Nic Horstmeier 2017-07-28 14:02:29 -05:00
parent a92261cc9b
commit 53a0206b1d
4 changed files with 40 additions and 2 deletions

View File

@ -1155,7 +1155,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
// Check parent (custom canCreate option for SiteTree) // Check parent (custom canCreate option for SiteTree)
// Block children not allowed for this parent type // Block children not allowed for this parent type
$parent = isset($context['Parent']) ? $context['Parent'] : null; $parent = isset($context['Parent']) ? $context['Parent'] : null;
if ($parent && !in_array(static::class, $parent->allowedChildren())) { $strictParentInstance = ($parent && $parent instanceof SiteTree);
if ($strictParentInstance && !in_array(static::class, $parent->allowedChildren())) {
return false; return false;
} }
@ -1171,7 +1172,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
} }
// Fall over to inherited permissions // Fall over to inherited permissions
if ($parent && $parent->exists()) { if ($strictParentInstance && $parent->exists()) {
return $parent->canAddChildren($member); return $parent->canAddChildren($member);
} else { } else {
// This doesn't necessarily mean we are creating a root page, but that // This doesn't necessarily mean we are creating a root page, but that

View File

@ -50,6 +50,7 @@ class SiteTreeTest extends SapphireTest
SiteTreeTest_ClassCext::class, SiteTreeTest_ClassCext::class,
SiteTreeTest_NotRoot::class, SiteTreeTest_NotRoot::class,
SiteTreeTest_StageStatusInherit::class, SiteTreeTest_StageStatusInherit::class,
SiteTreeTest_DataObject::class,
); );
public function testCreateDefaultpages() public function testCreateDefaultpages()
@ -632,6 +633,9 @@ class SiteTreeTest extends SapphireTest
// Test creation underneath a parent which doesn't exist in the database. This should // Test creation underneath a parent which doesn't exist in the database. This should
// fall back to checking whether the user can create pages at the root of the site // fall back to checking whether the user can create pages at the root of the site
$this->assertTrue(singleton(SiteTree::class)->canCreate(null, array('Parent' => singleton(SiteTree::class)))); $this->assertTrue(singleton(SiteTree::class)->canCreate(null, array('Parent' => singleton(SiteTree::class))));
//Test we don't check for allowedChildren on parent context if it's not SiteTree instance
$this->assertTrue(singleton(SiteTree::class)->canCreate(null, ['Parent' => $this->objFromFixture('SiteTreeTest_DataObject', 'relations')]));
} }
public function testEditPermissionsOnDraftVsLive() public function testEditPermissionsOnDraftVsLive()

View File

@ -123,3 +123,8 @@ SilverStripe\CMS\Model\RedirectorPage:
URLSegment: external URLSegment: external
RedirectionType: External RedirectionType: External
ExternalURL: "http://www.google.com?a&b" ExternalURL: "http://www.google.com?a&b"
SiteTreeTest_DataObject:
relations:
Title: 'Linked DataObject'
Pages: =>Page.home,=>Page.about,=>Page.staff

View File

@ -0,0 +1,28 @@
<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
/**
* Class SiteTreeTest_DataObject
*
* @property string $Title
* @method Pages[]|ManyManyList $Pages
*/
class SiteTreeTest_DataObject extends DataObject implements TestOnly
{
/**
* @var array
*/
private static $db = [
'Title' => 'Varchar',
];
/**
* @var array
*/
private static $many_many = [
'Pages' => SiteTree::class,
];
}