Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Limiting the number of pages a user can create?

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

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

Page: 1
Go to End
Author Topic: 1263 Views
  • CEpeep
    Avatar
    Community Member
    3 Posts

    Limiting the number of pages a user can create? Link to this post

    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?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Limiting the number of pages a user can create? Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Limiting the number of pages a user can create? Link to this post

    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

  • CEpeep
    Avatar
    Community Member
    3 Posts

    Re: Limiting the number of pages a user can create? Link to this post

    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.

    1263 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.