BUG Fix SiteTree / SiteConfig permissions

This commit is contained in:
Damian Mooyman 2015-03-16 16:27:31 +13:00
parent a495385ee5
commit 64955e57d1
12 changed files with 712 additions and 347 deletions

View File

@ -46,6 +46,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'treeview',
'listview',
'ListViewForm',
'childfilter',
);
public function init() {
@ -379,55 +380,49 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$json = $cache->load($cacheKey);
if(!$json) {
$def['Root'] = array();
$def['Root']['disallowedParents'] = array();
$def['Root']['disallowedChildren'] = array();
// Contains all possible classes to support UI controls listing them all,
// such as the "add page here" context menu.
$def['All'] = array();
// Identify disallows and set globals
foreach($classes as $class) {
$obj = singleton($class);
if($obj instanceof HiddenClass) continue;
$allowedChildren = $obj->allowedChildren();
// Name item
$def['All'][$class] = array(
'title' => $obj->i18n_singular_name()
);
// SiteTree::allowedChildren() returns null rather than an empty array if SiteTree::allowed_chldren == 'none'
if($allowedChildren == null) $allowedChildren = array();
// Exclude SiteTree from possible Children
$possibleChildren = array_diff($allowedChildren, array("SiteTree"));
// Find i18n - names and build allowed children array
foreach($possibleChildren as $child) {
$instance = singleton($child);
if($instance instanceof HiddenClass) continue;
if(!array_key_exists($child, $cacheCanCreate) || !$cacheCanCreate[$child]) continue;
// skip this type if it is restricted
if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;
$title = $instance->i18n_singular_name();
$def[$class]['allowedChildren'][] = array("ssclass" => $child, "ssname" => $title);
// Check if can be created at the root
$needsPerm = $obj->stat('need_permission');
if(
!$obj->stat('can_be_root')
|| (!array_key_exists($class, $cacheCanCreate) || !$cacheCanCreate[$class])
|| ($needsPerm && !$this->can($needsPerm))
) {
$def['Root']['disallowedChildren'][] = $class;
}
$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
// Hint data specific to the class
$def[$class] = array();
$defaultChild = $obj->defaultChild();
if($defaultChild != 'Page' && $defaultChild != null) $def[$class]['defaultChild'] = $defaultChild;
$defaultParent = $obj->defaultParent();
$parent = SiteTree::get_by_link($defaultParent);
$id = $parent ? $parent->id : null;
if ($defaultParent != 1 && $defaultParent != null) $def[$class]['defaultParent'] = $defaultParent;
if(isset($def[$class]['disallowedChildren'])) {
foreach($def[$class]['disallowedChildren'] as $disallowedChild) {
$def[$disallowedChild]['disallowedParents'][] = $class;
}
if($defaultChild !== 'Page' && $defaultChild !== null) {
$def[$class]['defaultChild'] = $defaultChild;
}
// Are any classes allowed to be parents of root?
$def['Root']['disallowedParents'][] = $class;
$defaultParent = $obj->defaultParent();
if ($defaultParent !== 1 && $defaultParent !== null) {
$def[$class]['defaultParent'] = $defaultParent;
}
}
$json = Convert::raw2xml(Convert::raw2json($def));
$this->extend('updateSiteTreeHints', $def);
$json = Convert::raw2json($def);
$cache->save($json, $cacheKey);
}
return $json;
@ -490,8 +485,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
if($instance instanceof HiddenClass) continue;
if(!$instance->canCreate()) continue;
// skip this type if it is restricted
if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;
@ -675,6 +668,38 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
return $this->renderWith($this->getTemplatesWithSuffix('_ListView'));
}
/**
* Callback to request the list of page types allowed under a given page instance.
* Provides a slower but more precise response over SiteTreeHints
*
* @param SS_HTTPRequest $request
* @return SS_HTTPResponse
*/
public function childfilter($request) {
// Check valid parent specified
$parentID = $request->requestVar('ParentID');
$parent = SiteTree::get()->byID($parentID);
if(!$parent || !$parent->exists()) return $this->httpError(404);
// Build hints specific to this class
// Identify disallows and set globals
$classes = SiteTree::page_type_classes();
$disallowedChildren = array();
foreach($classes as $class) {
$obj = singleton($class);
if($obj instanceof HiddenClass) continue;
if(!$obj->canCreate(null, array('Parent' => $parent))) {
$disallowedChildren[] = $class;
}
}
$this->extend('updateChildFilter', $disallowedChildren, $parentID);
$this->response->addHeader('Content-Type', 'application/json; charset=utf-8');
$this->response->setBody(Convert::raw2json($disallowedChildren));
return $this->response;
}
/**
* Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page
* defaulting to no filter and show all pages in first level.

View File

@ -21,7 +21,7 @@ class CMSPageAddController extends CMSPageEditController {
$pageTypes = array();
foreach($this->PageTypes() as $type) {
$html = sprintf('<span class="page-icon class-%s"></span><strong class="title">%s</strong><span class="description">%s</span>',
$type->getField('Title'),
$type->getField('ClassName'),
$type->getField('AddAction'),
$type->getField('Description')
);
@ -39,9 +39,6 @@ class CMSPageAddController extends CMSPageEditController {
$childTitle = _t('CMSPageAddController.ParentMode_child', 'Under another page');
$fields = new FieldList(
// new HiddenField("ParentID", false, ($this->parentRecord) ? $this->parentRecord->ID : null),
// TODO Should be part of the form attribute, but not possible in current form API
$hintsField = new LiteralField('Hints', sprintf('<span class="hints" data-hints="%s"></span>', $this->SiteTreeHints())),
new LiteralField('PageModeHeader', sprintf($numericLabelTmpl, 1, _t('CMSMain.ChoosePageParentMode', 'Choose where to create this page'))),
$parentModeField = new SelectionGroup(
@ -87,6 +84,8 @@ class CMSPageAddController extends CMSPageEditController {
$this->extend('updatePageOptions', $fields);
$form = new Form($this, "AddForm", $fields, $actions);
$form->setAttribute('data-hints', $this->SiteTreeHints());
$form->setAttribute('data-childfilter', $this->Link('childfilter'));
$form->addExtraClass('cms-add-form stacked cms-content center cms-edit-form ' . $this->BaseCSSClasses());
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
@ -113,12 +112,8 @@ class CMSPageAddController extends CMSPageEditController {
if(!$parentObj || !$parentObj->ID) $parentID = 0;
if($parentObj) {
if(!$parentObj->canAddChildren()) return Security::permissionFailure($this);
if(!singleton($className)->canCreate()) return Security::permissionFailure($this);
} else {
if(!SiteConfig::current_site_config()->canCreateTopLevel())
return Security::permissionFailure($this);
if(!singleton($className)->canCreate(Member::currentUser(), array('Parent' => $parentObj))) {
return Security::permissionFailure($this);
}
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);

View File

@ -22,8 +22,21 @@ class SiteConfig extends DataObject implements PermissionProvider {
"CreateTopLevelGroups" => "Group"
);
static $defaults = array(
"CanViewType" => "Anyone",
"CanEditType" => "LoggedInUsers",
"CanCreateTopLevelType" => "LoggedInUsers",
);
protected static $disabled_themes = array();
/**
* Default permission to check for 'LoggedInUsers' to create or edit pages
*
* @var array
*/
static $required_permission = array('CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain');
static public function disable_theme($theme) {
self::$disabled_themes[$theme] = $theme;
}
@ -192,22 +205,25 @@ class SiteConfig extends DataObject implements PermissionProvider {
* called if a page is set to Inherit, but there is nothing
* to inherit from.
*
* @param mixed $member
* @param Member $member
* @return boolean
*/
public function canView($member = null) {
public function canViewPages($member = null) {
if(!$member) $member = Member::currentUserID();
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if ($member && Permission::checkMember($member, "ADMIN")) return true;
$extended = $this->extendedCan('canViewPages', $member);
if($extended !== null) return $extended;
if (!$this->CanViewType || $this->CanViewType == 'Anyone') return true;
// check for any logged-in users
if($this->CanViewType == 'LoggedInUsers' && $member) return true;
if($this->CanViewType === 'LoggedInUsers' && $member) return true;
// check for specific groups
if($this->CanViewType == 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) return true;
if($this->CanViewType === 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) return true;
return false;
}
@ -215,26 +231,45 @@ class SiteConfig extends DataObject implements PermissionProvider {
/**
* Can a user edit pages on this site? This method is only
* called if a page is set to Inherit, but there is nothing
* to inherit from.
* to inherit from, or on new records without a parent.
*
* @param mixed $member
* @param Member $member
* @return boolean
*/
public function canEdit($member = null) {
public function canEditPages($member = null) {
if(!$member) $member = Member::currentUserID();
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if ($member && Permission::checkMember($member, "ADMIN")) return true;
// check for any logged-in users
if(!$this->CanEditType || $this->CanEditType == 'LoggedInUsers' && $member) return true;
$extended = $this->extendedCan('canEditPages', $member);
if($extended !== null) return $extended;
// check for any logged-in users with CMS access
if( $this->CanEditType === 'LoggedInUsers'
&& Permission::checkMember($member, $this->config()->required_permission)
) {
return true;
}
// check for specific groups
if($this->CanEditType == 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) return true;
if($this->CanEditType === 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) {
return true;
}
return false;
}
public function canEdit($member = null) {
if(!$member) $member = Member::currentUserID();
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
$extended = $this->extendedCan('canEdit', $member);
if($extended !== null) return $extended;
return Permission::checkMember($member, "EDIT_SITECONFIG");
}
public function providePermissions() {
return array(
'EDIT_SITECONFIG' => array(
@ -249,25 +284,32 @@ class SiteConfig extends DataObject implements PermissionProvider {
/**
* Can a user create pages in the root of this site?
*
* @param mixed $member
* @param Member $member
* @return boolean
*/
public function canCreateTopLevel($member = null) {
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
$member = Member::currentUserID();
}
if (Permission::check('ADMIN')) return true;
if(!$member) $member = Member::currentUserID();
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if ($member && Permission::checkMember($member, "ADMIN")) return true;
// check for any logged-in users
if($this->CanCreateTopLevelType == 'LoggedInUsers' && $member) return true;
$extended = $this->extendedCan('canCreateTopLevel', $member);
if($extended !== null) return $extended;
// check for any logged-in users with CMS permission
if( $this->CanCreateTopLevelType === 'LoggedInUsers'
&& Permission::checkMember($member, $this->config()->required_permission)
) {
return true;
}
// check for specific groups
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if($this->CanCreateTopLevelType == 'OnlyTheseUsers' && $member && $member->inGroups($this->CreateTopLevelGroups())) return true;
if( $this->CanCreateTopLevelType === 'OnlyTheseUsers'
&& $member
&& $member->inGroups($this->CreateTopLevelGroups())
) {
return true;
}
return false;
}

View File

@ -815,7 +815,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
// check for inherit
if($this->CanViewType == 'Inherit') {
if($this->ParentID) return $this->Parent()->canView($member);
else return $this->getSiteConfig()->canView($member);
else return $this->getSiteConfig()->canViewPages($member);
}
// check for any logged-in users
@ -896,12 +896,12 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
/**
* This function should return true if the current user can create new
* pages of this class. It can be overloaded to customise the security model for an
* application.
* pages of this class, regardless of context. It can be overloaded
* to customise the security model for an application.
*
* Denies permission if any of the following conditions is TRUE:
* - canCreate() returns FALSE on any extension
* - $can_create is set to FALSE and the site is not in "dev mode"
* By default, permission to create at the root level is based on the SiteConfig
* configuration, and permission to create beneath a parent is based on the
* ability to edit that parent page.
*
* Use {@link canAddChildren()} to control behaviour of creating children under this page.
*
@ -909,6 +909,9 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @uses DataExtension->canCreate()
*
* @param Member $member
* @param array $context Optional array which may contain array('Parent' => $parentObj)
* If a parent page is known, it will be checked for validity.
* If omitted, it will be assumed this is to be created as a top level page.
* @return boolean True if the current user can create pages on this class.
*/
public function canCreate($member = null) {
@ -916,16 +919,31 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$member = Member::currentUserID();
}
// Check parent (custom canCreate option for SiteTree)
// Block children not allowed for this parent type
$context = func_num_args() > 1 ? func_get_arg(1) : array();
$parent = isset($context['Parent']) ? $context['Parent'] : null;
if($parent && !in_array(get_class($this), $parent->allowedChildren())) return false;
// Check permission
if($member && Permission::checkMember($member, "ADMIN")) return true;
// Standard mechanism for accepting permission changes from extensions
$extended = $this->extendedCan('canCreate', $member);
if($extended !== null) return $extended;
$results = $this->extend('canCreate', $member, $parent);
if(is_array($results) && ($results = array_filter($results, function($v) {return $v !== null;}))) {
return min($results);
}
return $this->stat('can_create') != false || Director::isDev();
// Fall over to inherited permissions
if($parent) {
return $parent->canAddChildren($member);
} else {
// This doesn't necessarily mean we are creating a root page, but that
// we don't know if there is a parent, so default to this permission
return SiteConfig::current_site_config()->canCreateTopLevel($member);
}
}
/**
* This function should return true if the current user can edit this
* page. It can be overloaded to customise the security model for an
@ -966,7 +984,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
// Default for unsaved pages
} else {
return $this->getSiteConfig()->canEdit($member);
return $this->getSiteConfig()->canEditPages($member);
}
}
@ -1207,7 +1225,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* page can be edited.
*/
static public function can_edit_multiple($ids, $memberID, $useCached = true) {
return self::batch_permission_check($ids, $memberID, 'CanEditType', 'SiteTree_EditorGroups', 'canEdit', 'CMS_ACCESS_CMSMain', $useCached);
return self::batch_permission_check($ids, $memberID, 'CanEditType', 'SiteTree_EditorGroups', 'canEditPages', 'CMS_ACCESS_CMSMain', $useCached);
}
/**
@ -2574,9 +2592,20 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @return string a html string ready to be directly used in a template
*/
public function getTreeTitle() {
// Build the list of candidate children
$children = array();
$candidates = static::page_type_classes();
foreach($this->allowedChildren() as $childClass) {
if(!in_array($childClass, $candidates)) continue;
$child = singleton($childClass);
if($child->canCreate(null, array('Parent' => $this))) {
$children[$childClass] = $child->i18n_singular_name();
}
}
$flags = $this->getStatusFlags();
$treeTitle = sprintf(
"<span class=\"jstree-pageicon\"></span><span class=\"item\">%s</span>",
"<span class=\"jstree-pageicon\"></span><span class=\"item\" data-allowedchildren=\"%s\">%s</span>",
Convert::raw2att(Convert::raw2json($children)),
Convert::raw2xml(str_replace(array("\n","\r"),"",$this->MenuTitle))
);
foreach($flags as $class => $data) {

View File

@ -15,37 +15,107 @@
});
$(".cms-add-form").entwine({
onmatch: function() {
ParentID: 0, // Last selected parentID
ParentCache: {}, // Cache allowed children for each selected page
onadd: function() {
var self = this;
this.find('#ParentID .TreeDropdownField').bind('change', function() {
self.updateTypeList();
});
this.find(".SelectionGroup.parent-mode").bind('change', function() {
self.updateTypeList();
});
this.updateTypeList();
this._super();
},
onunmatch: function() {
this._super();
loadCachedChildren: function(parentID) {
var cache = this.getParentCache();
if(typeof cache[parentID] !== 'undefined') return cache[parentID];
else return null;
},
saveCachedChildren: function(parentID, children) {
var cache = this.getParentCache();
cache[parentID] = children;
this.setParentCache(cache);
},
/**
* Limit page type selection based on parent class.
* Limit page type selection based on parent selection.
* Select of root classes is pre-computed, but selections with a given parent
* are updated on-demand.
* Similar implementation to LeftAndMain.Tree.js.
*/
updateTypeList: function() {
var hints = this.find('.hints').data('hints'),
metadata = this.find('#ParentID .TreeDropdownField').data('metadata'),
id = this.find('#ParentID .TreeDropdownField').getValue(),
var hints = this.data('hints'),
parentTree = this.find('#ParentID .TreeDropdownField'),
parentMode = this.find("input[name=ParentModeField]:checked").val(),
metadata = parentTree.data('metadata'),
id = (metadata && parentMode === 'child')
? (parentTree.getValue() || this.getParentID())
: null,
newClassName = metadata ? metadata.ClassName : null,
hintKey = newClassName ? newClassName : 'Root',
hint = (typeof hints[hintKey] != 'undefined') ? hints[hintKey] : null;
hintKey = (newClassName && parentMode === 'child')
? newClassName
: 'Root',
hint = (typeof hints[hintKey] !== 'undefined') ? hints[hintKey] : null,
self = this,
defaultChildClass = (hint && typeof hint.defaultChild !== 'undefined')
? hint.defaultChild
: null,
disallowedChildren = [];
var disallowedChildren = (hint && typeof hint.disallowedChildren != 'undefined') ? hint.disallowedChildren : [],
defaultChildClass = (hint && typeof hint.defaultChild != 'undefined') ? hint.defaultChild : null;
if(id) {
// Prevent interface operations
if(this.hasClass('loading')) return;
this.addClass('loading');
// Enable last parent ID to be re-selected from memory
this.setParentID(id);
if(!parentTree.getValue()) parentTree.setValue(id);
// Use cached data if available
disallowedChildren = this.loadCachedChildren(id);
if(disallowedChildren !== null) {
this.updateSelectionFilter(disallowedChildren, defaultChildClass);
this.removeClass('loading');
return;
}
$.ajax({
url: self.data('childfilter'),
data: {'ParentID': id},
success: function(data) {
// reload current form and tree
self.saveCachedChildren(id, data);
self.updateSelectionFilter(data, defaultChildClass);
},
complete: function() {
self.removeClass('loading');
}
});
return false;
} else {
disallowedChildren = (hint && typeof hint.disallowedChildren !== 'undefined')
? hint.disallowedChildren
: [],
this.updateSelectionFilter(disallowedChildren, defaultChildClass);
}
},
/**
* Update the selection filter with the given blacklist and default selection
*
* @param array disallowedChildren
* @param string defaultChildClass
*/
updateSelectionFilter: function(disallowedChildren, defaultChildClass) {
// Limit selection
var allAllowed = null; // troolian
this.find('#PageType li').each(function() {
var className = $(this).find('input').val(), isAllowed = ($.inArray(className, disallowedChildren) == -1);
var className = $(this).find('input').val(),
isAllowed = ($.inArray(className, disallowedChildren) === -1);
$(this).setEnabled(isAllowed);
if(!isAllowed) $(this).setSelected(false);
if(allAllowed === null) allAllowed = isAllowed;
else allAllowed = allAllowed && isAllowed;
});
// Set default child selection, or fall back to first available option
@ -69,10 +139,13 @@
},
setSelected: function(bool) {
var input = this.find('input');
this.toggleClass('selected', bool);
if(bool && !input.is(':disabled')) {
this.siblings().setSelected(false);
input.attr('checked', 'checked');
this.toggleClass('selected', true);
input.prop('checked', true);
} else {
this.toggleClass('selected', false);
input.prop('checked', false);
}
},
setEnabled: function(bool) {

View File

@ -9,23 +9,23 @@
'items': function(node) {
// Build a list for allowed children as submenu entries
var pagetype = node.data('pagetype');
var id = node.data('id');
var allowedChildrenClasses = node.find('>a .item').data('allowedchildren');
var allowedChildren = new Object;
$(hints[pagetype].allowedChildren).each(
function(key, val){
allowedChildren["allowedchildren-" + key ] = {
'label': '<span class="jstree-pageicon"></span>' + val.ssname,
'_class': 'class-' + val.ssclass,
'action': function(obj) {
$('.cms-container').entwine('.ss').loadPanel(ss.i18n.sprintf(
self.data('urlAddpage'), id, val.ssclass
));
}
};
}
);
var hasAllowedChildren = false;
$.each(allowedChildrenClasses, function(klass, title) {
hasAllowedChildren = true;
allowedChildren["allowedchildren-" + klass ] = {
'label': '<span class="jstree-pageicon"></span>' + title,
'_class': 'class-' + klass,
'action': function(obj) {
$('.cms-container').entwine('.ss').loadPanel(ss.i18n.sprintf(
self.data('urlAddpage'), id, klass
));
}
};
});
var menuitems =
{
'edit': {
@ -38,7 +38,7 @@
}
};
// Test if there are any allowed Children and thus the possibility of adding some
if(allowedChildren.hasOwnProperty('allowedchildren-0')) {
if(hasAllowedChildren) {
menuitems['addsubpage'] = {
'label': ss.i18n._t('Tree.AddSubPage', 'Add page under this page', 100, 'Used in the context menu when right-clicking on a page node in the CMS tree'),
'submenu': allowedChildren

View File

@ -19,7 +19,7 @@ $ExtraTreeTools
</div>
<% end_if %>
<div class="cms-tree" data-url-tree="$Link(getsubtree)" data-url-savetreenode="$Link(savetreenode)" data-url-updatetreenodes="$Link(updatetreenodes)" data-url-addpage="{$LinkPageAdd('AddForm/?action_doAdd=1')}&amp;ParentID=%s&amp;PageType=%s&amp;SecurityID=$SecurityID" data-url-editpage="$LinkPageEdit('%s')" data-hints="$SiteTreeHints">
<div class="cms-tree" data-url-tree="$Link(getsubtree)" data-url-savetreenode="$Link(savetreenode)" data-url-updatetreenodes="$Link(updatetreenodes)" data-url-addpage="{$LinkPageAdd('AddForm/?action_doAdd=1')}&amp;ParentID=%s&amp;PageType=%s&amp;SecurityID=$SecurityID" data-url-editpage="$LinkPageEdit('%s')" data-hints="$SiteTreeHints.ATT" data-childfilter="$Link('childfilter')">
$SiteTreeAsUL
</div>
</div>

View File

@ -26,6 +26,78 @@ class CMSMainTest extends FunctionalTest {
parent::tearDownOnce();
}
function testSiteTreeHints() {
$cache = SS_Cache::factory('CMSMain_SiteTreeHints');
// Login as user with root creation privileges
$user = $this->objFromFixture('Member', 'rootedituser');
$user->logIn();
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
$rawHints = singleton('CMSMain')->SiteTreeHints();
$this->assertNotNull($rawHints);
$rawHints = preg_replace('/^"(.*)"$/', '$1', Convert::xml2raw($rawHints));
$hints = Convert::json2array($rawHints);
$this->assertArrayHasKey('Root', $hints);
$this->assertArrayHasKey('Page', $hints);
$this->assertArrayHasKey('All', $hints);
$this->assertArrayHasKey(
'CMSMainTest_ClassA',
$hints['All'],
'Global list shows allowed classes'
);
$this->assertArrayNotHasKey(
'CMSMainTest_HiddenClass',
$hints['All'],
'Global list does not list hidden classes'
);
$this->assertNotContains(
'CMSMainTest_ClassA',
$hints['Root']['disallowedChildren'],
'Limits root classes'
);
$this->assertContains(
'CMSMainTest_NotRoot',
$hints['Root']['disallowedChildren'],
'Limits root classes'
);
}
public function testChildFilter() {
$this->logInWithPermission('ADMIN');
// Check page A
$pageA = new CMSMainTest_ClassA();
$pageA->write();
$pageB = new CMSMainTest_ClassB();
$pageB->write();
// Check query
$response = $this->get('CMSMain/childfilter?ParentID='.$pageA->ID);
$children = json_decode($response->getBody());
$this->assertFalse($response->isError());
// Page A can't have unrelated children
$this->assertContains(
'Page',
$children,
'Limited parent lists disallowed classes'
);
// But it can create a ClassB
$this->assertNotContains(
'CMSMainTest_ClassB',
$children,
'Limited parent omits explicitly allowed classes in disallowedChildren'
);
}
/**
* @todo Test the results of a publication better
*/
@ -256,11 +328,7 @@ class CMSMainTest extends FunctionalTest {
'admin/pages/add/AddForm',
array('ParentID' => $newPageId, 'PageType' => 'Page', 'Locale' => 'en_US', 'action_doAdd' => 1)
);
$this->assertFalse($response->isError());
$this->assertContains(
htmlentities(_t('SiteTree.PageTypeNotAllowed', array('type' => 'Page'))),
$response->getBody()
);
$this->assertEquals(403, $response->getStatusCode(), 'Add disallowed child should fail');
$this->session()->inst_set('loggedInAs', NULL);
@ -314,3 +382,11 @@ class CMSMainTest_ClassA extends Page implements TestOnly {
class CMSMainTest_ClassB extends Page implements TestOnly {
}
class CMSMainTest_NotRoot extends Page implements TestOnly {
static $can_be_root = false;
}
class CMSMainTest_HiddenClass extends Page implements TestOnly, HiddenClass {
}

View File

@ -8,12 +8,14 @@
*/
class SiteConfigTest extends SapphireTest {
static $fixture_file = 'SiteConfigTest.yml';
protected $illegalExtensions = array(
'SiteTree' => array('SiteTreeSubsites')
);
public function testAvailableThemes() {
$config = SiteConfig::current_site_config();
$config = $this->objFromFixture('SiteConfig', 'default');
$ds = DIRECTORY_SEPARATOR;
$testThemeBaseDir = TEMP_FOLDER . $ds . 'test-themes';
@ -35,4 +37,49 @@ class SiteConfigTest extends SapphireTest {
Filesystem::removeFolder($testThemeBaseDir);
}
public function testCanCreateRootPages() {
$config = $this->objFromFixture('SiteConfig', 'default');
// Log in without pages admin access
$this->logInWithPermission('CMS_ACCESS_AssetAdmin');
$this->assertFalse($config->canCreateTopLevel());
// Login with necessary edit permission
$perms = SiteConfig::config()->required_permission;
$this->logInWithPermission(reset($perms));
$this->assertTrue($config->canCreateTopLevel());
}
public function testCanViewPages() {
$config = $this->objFromFixture('SiteConfig', 'default');
$this->assertTrue($config->canViewPages());
}
public function testCanEdit() {
$config = $this->objFromFixture('SiteConfig', 'default');
// Unrelated permissions don't allow siteconfig
$this->logInWithPermission('CMS_ACCESS_AssetAdmin');
$this->assertFalse($config->canEdit());
// Only those with edit permission can do this
$this->logInWithPermission('EDIT_SITECONFIG');
$this->assertTrue($config->canEdit());
}
public function testCanEditPages() {
$config = $this->objFromFixture('SiteConfig', 'default');
// Log in without pages admin access
$this->logInWithPermission('CMS_ACCESS_AssetAdmin');
$this->assertFalse($config->canEditPages());
// Login with necessary edit permission
$perms = SiteConfig::config()->required_permission;
$this->logInWithPermission(reset($perms));
$this->assertTrue($config->canEditPages());
}
}

View File

@ -0,0 +1,7 @@
SiteConfig:
default:
Title: My test site
Tagline: Default site config
CanViewType: Anyone
CanEditType: LoggedInUsers
CanCreateTopLevelType: LoggedInUsers

View File

@ -20,6 +20,13 @@ class SiteTreeTest extends SapphireTest {
'SiteTreeTest_StageStatusInherit',
);
/**
* Ensure any current member is logged out
*/
public function logOut() {
if($member = Member::currentUser()) $member->logOut();
}
public function testCreateDefaultpages() {
$remove = DataObject::get('SiteTree');
if($remove) foreach($remove as $page) $page->delete();
@ -98,7 +105,6 @@ class SiteTreeTest extends SapphireTest {
$this->assertTrue($obj->doPublish());
$this->assertNull(DB::query("SELECT \"MetaTitle\" FROM \"SiteTree_Live\" WHERE \"ID\" = '$obj->ID'")->value());
}
public function testParentNodeCachedInMemory() {
@ -434,10 +440,15 @@ class SiteTreeTest extends SapphireTest {
$editor = $this->objFromFixture("Member", "editor");
$home = $this->objFromFixture("Page", "home");
$staff = $this->objFromFixture("Page", "staff");
$products = $this->objFromFixture("Page", "products");
$product1 = $this->objFromFixture("Page", "product1");
$product4 = $this->objFromFixture("Page", "product4");
// Test logged out users cannot edit
$this->logOut();
$this->assertFalse($staff->canEdit());
// Can't edit a page that is locked to admins
$this->assertFalse($home->canEdit($editor));
@ -451,6 +462,44 @@ class SiteTreeTest extends SapphireTest {
$this->assertFalse($product4->canEdit($editor));
}
public function testCanEditWithAccessToAllSections() {
$page = new Page();
$page->write();
$allSectionMember = $this->objFromFixture('Member', 'allsections');
$securityAdminMember = $this->objFromFixture('Member', 'securityadmin');
$this->assertTrue(SiteConfig::current_site_config()->canEditPages($allSectionMember));
$this->assertTrue($page->canEdit($allSectionMember));
$this->assertFalse($page->canEdit($securityAdminMember));
}
public function testCreatePermissions() {
// Test logged out users cannot create
$this->logOut();
$this->assertFalse(singleton('SiteTree')->canCreate());
// Login with another permission
$this->logInWithPermission('DUMMY');
$this->assertFalse(singleton('SiteTree')->canCreate());
// Login with basic CMS permission
$perms = SiteConfig::config()->required_permission;
$this->logInWithPermission(reset($perms));
$this->assertTrue(singleton('SiteTree')->canCreate());
// Test creation underneath a parent which this user doesn't have access to
$parent = $this->objFromFixture('Page', 'about');
$this->assertFalse(singleton('SiteTree')->canCreate(null, array('Parent' => $parent)));
// Test creation underneath a parent which doesn't allow a certain child
$parentB = new SiteTreeTest_ClassB();
$parentB->Title = 'Only Allows SiteTreeTest_ClassC';
$parentB->write();
$this->assertTrue(singleton('SiteTreeTest_ClassA')->canCreate(null));
$this->assertFalse(singleton('SiteTreeTest_ClassA')->canCreate(null, array('Parent' => $parentB)));
$this->assertTrue(singleton('SiteTreeTest_ClassC')->canCreate(null, array('Parent' => $parentB)));
}
public function testEditPermissionsOnDraftVsLive() {
// Create an inherit-permission page
$page = new Page();
@ -488,7 +537,7 @@ class SiteTreeTest extends SapphireTest {
// Confirm that Member.editor can still edit the page
$this->objFromFixture('Member','editor')->logIn();
$this->assertTrue($page->canEdit());
}
}
public function testCompareVersions() {
// Necessary to avoid
@ -879,7 +928,7 @@ class SiteTreeTest extends SapphireTest {
$this->assertEquals(null, $page->getField('MenuTitle'));
}
}
}
/**#@+
* @ignore

View File

@ -1,82 +1,104 @@
SiteConfig:
default:
Title: My test site
Tagline: Default site config
CanViewType: Anyone
CanEditType: LoggedInUsers
CanCreateTopLevelType: LoggedInUsers
Group:
editors:
Title: Editors
admins:
Title: Administrators
editors:
Title: Editors
admins:
Title: Administrators
allsections:
Title: All Section Editors
securityadmins:
Title: Security Admins
Permission:
admins:
Code: ADMIN
Group: =>Group.admins
editors:
Code: CMS_ACCESS_CMSMain
Group: =>Group.editors
admins:
Code: ADMIN
Group: =>Group.admins
editors:
Code: CMS_ACCESS_CMSMain
Group: =>Group.editors
allsections:
Code: CMS_ACCESS_CMSMain
Group: =>Group.allsections
securityadmins:
Code: CMS_ACCESS_SecurityAdmin
Group: =>Group.securityadmins
Member:
editor:
FirstName: Test
Surname: Editor
Groups: =>Group.editors
admin:
FirstName: Test
Surname: Administrator
Groups: =>Group.admins
editor:
FirstName: Test
Surname: Editor
Groups: =>Group.editors
admin:
FirstName: Test
Surname: Administrator
Groups: =>Group.admins
allsections:
Groups: =>Group.allsections
securityadmin:
Groups: =>Group.securityadmins
Page:
home:
Title: Home
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.admins
about:
Title: About Us
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.admins
staff:
Title: Staff
URLSegment: my-staff
Parent: =>Page.about
ceo:
Title: CEO
Parent: =>Page.staff
staffduplicate:
Title: Staff
URLSegment: my-staff
Parent: =>Page.about
products:
Title: Products
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.editors
product1:
Title: 1.1 Test Product
Parent: =>Page.products
CanEditType: Inherit
product2:
Title: Another Product
Parent: =>Page.products
CanEditType: Inherit
product3:
Title: Another Product
Parent: =>Page.products
CanEditType: Inherit
product4:
Title: Another Product
Parent: =>Page.products
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.admins
contact:
Title: Contact Us
object:
Title: Object
controller:
Title: Controller
numericonly:
Title: 1930
home:
Title: Home
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.admins
about:
Title: About Us
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.admins
staff:
Title: Staff
URLSegment: my-staff
Parent: =>Page.about
ceo:
Title: CEO
Parent: =>Page.staff
staffduplicate:
Title: Staff
URLSegment: my-staff
Parent: =>Page.about
products:
Title: Products
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.editors
product1:
Title: 1.1 Test Product
Parent: =>Page.products
CanEditType: Inherit
product2:
Title: Another Product
Parent: =>Page.products
CanEditType: Inherit
product3:
Title: Another Product
Parent: =>Page.products
CanEditType: Inherit
product4:
Title: Another Product
Parent: =>Page.products
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.admins
contact:
Title: Contact Us
object:
Title: Object
controller:
Title: Controller
numericonly:
Title: 1930
SiteTreeTest_Conflicted:
parent:
Title: Parent
parent:
Title: Parent
ErrorPage:
404:
Title: Page not Found
ErrorCode: 404
404:
Title: Page not Found
ErrorCode: 404