Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Reply

8 Posts   1569 Views

Avatar
Rajaneesh

6 August 2009 at 10:09pm Community Member, 3 Posts

Please help me .. i am new user for silverstripe .

i want limit a page upto 10 pages .. after that when user try to create new page it should trough a error message that "Access Denied"

Avatar
Willr

9 August 2009 at 11:50am Forum Moderator, 5511 Posts

You can create a function called canCreate() which has the permission if a user can create a given page. If you want this check to be over the whole site you would do something like this on your Page.php file in the Page class

function canCreate() {
$pages = DataObject::get('SiteTree');
return ($pages && $pages->Count() > 10) ? false : true;
}

Avatar
Juanitou

10 August 2009 at 7:26am Community Member, 323 Posts

Hi Will!

Maybe I'm wrong, but I think that it's worth mentioning that by creating this function we will overload (right term?) the canCreate() function present in DataObject. Isn't it?

Best regards,
Juan

Avatar
banal

10 August 2009 at 8:21am (Last edited: 10 August 2009 8:23am), Community Member, 901 Posts

Yes, doing so, you will indeed overload the canCreate method (not of DataObject, but of SiteTree). But that isn't really a problem.
If you do user-permission checks in the canCreate method of a base-class, you can catch this first:

function canCreate($member = null) {
   // check permission of base-class
   $permission = parent::canCreate($member);
   // if there is no permission, we don't grant it here either.
   if($permission === false)
      return false;

   $pages = DataObject::get('SiteTree');
   return ($pages && $pages->Count() > 10) ? false : true;
}

That way, you can create up to 10 SiteTree instances, but only if you have the permissions to do so

Avatar
Juanitou

10 August 2009 at 9:39am Community Member, 323 Posts

Thank you!

Avatar
Rajaneesh

10 August 2009 at 4:40pm Community Member, 3 Posts

thank for reply ..
but show me how to do in before tiger i want call that function in addpage() function ? CMSMain.php

it should send error message and i want display that message in CMSLeft.ss

please help me ....

Avatar
Four

27 August 2009 at 9:37pm Community Member, 6 Posts

following on from this, how would I only limit the number of Level1 (top-level) pages that can be created without placing a limit on Level2 pages?

Avatar
banal

27 August 2009 at 11:31pm (Last edited: 27 August 2009 11:35pm), Community Member, 901 Posts

Hi. That should be simple to implement. Use the canCreate method, listed above, but change the following:

$pages = DataObject::get('SiteTree', 'ParentID = 0');

The WHERE statement checks if ParentID is zero, which is only the case for top-level pages.

Update: Sorry, my solution is slightly flawed. The limit check will only occur on Level 1 Pages, but you won't be able to create sub-pages once that limit is reached. Sadly, I don't know an easy work-around for your problem, you would have to find out what level the currently selected item of the SiteTree Navigation belongs to. Maybe somebody knows a easy way to check this?