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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Limiting the number of pages a user can create?


Go to End


4 Posts   2550 Views

Avatar
CEpeep

Community Member, 3 Posts

1 February 2009 at 8:13am

As the title says, is there any way to keep users of the system from either making too many pages (say, limit them to 5 pages) or to limit the total number of pages allowed in a single install of Silverstripe?

Avatar
Sean

Forum Moderator, 922 Posts

1 February 2009 at 10:46am

Edited: 01/02/2009 11:09am

What you'll want to do is create a function called canCreate on your Page class (found in Page.php).

Here's an example of what it may look like:

function canCreate($member = null) {
	parent::canCreate($member);

	$limit = 5;
	$pages = DataObject::get('Page');

	if($pages && $pages->Count() >= $limit) {
		return false;
	}
		
	return true;
}

This is a very simple example, and doesn't take into account the current logged in CMS admin.

You could get fancy and see how many pages the current Member Member::currentUser() has created, and check against that instead.

Cheers,
Sean

Avatar
Sean

Forum Moderator, 922 Posts

1 February 2009 at 10:58am

Edited: 01/02/2009 11:08am

Here is a more complex example of limit checking.

static $has_one = array(
	'Creator' => 'Member'
);

function getCMSFields() {
	$fields = parent::getCMSFields();
	$memberMap = DataObject::get('Member')->map('ID', 'FirstName');
	$fields->addFieldToTab('Root.Content.Main', new LookupField('CreatorID', 'Creator', $memberMap), 'Title');
	
	return $fields;
}
	
function canCreate($member = null) {
	parent::canCreate($member);

	$limit = 5;
	$currentMemberID = Member::currentUserID();
	$pages = DataObject::get('Page', "CreatorID = $currentMemberID");
	
	if($pages && $pages->Count() >= $limit) {
		return false;
	}
		
	return true;
}
	
function onBeforeWrite() {
	parent::onBeforeWrite();
		
	if(!$this->CreatorID) {
		$this->CreatorID = Member::currentUserID();
	}
}

All the above code should live in the Page class (Page.php).

What this does is add a new column called "CreatorID" on the Page table. This logs the ID of the member logged into the CMS whenever they create a page. (make sure to run db/build after copying in this code example, or dev/build if using SS 2.3.0).

Now that we have the CreatorID we can check how many pages the member has created and make a determination in canCreate() again.

A look up of the member's name is done in getCMSFields() as useful information to see who created the given page.

Hope this is of some value!

Cheers,
Sean

Avatar
CEpeep

Community Member, 3 Posts

1 February 2009 at 11:12am

Thanks for your help, Sean! I'll try these out later and let you know how they worked for me. I haven't been working with SilverStripe very long, but I continue to be impressed with its quality and flexibility.