Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Child inherit Parent edit permissions

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: 1267 Views
  • Twenty3design
    Avatar
    Community Member
    7 Posts

    Child inherit Parent edit permissions Link to this post

    Hello,

    Is it possible to set all children to automatically use their parents user permissions. I don't want the users to have to set the permissions every time they create a page or news item.

    Thanks in advance.
    Liam

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Child inherit Parent edit permissions Link to this post

    Hi Liam

    In 2.3 there is an option on each page to inherit it's parents settings, which you can set to a default using the Defaults array.

    In 2.2x you can do this using the OnBeforeWrite function in the Pages model class like so:

    function onBeforeWrite() {
    //If the page already exists in the database, don't take parent permissions
    if (!$this->exists()) {
    if($parent = $this->Parent()) {
    if($parent->Viewers) $this->Viewers = $parent->Viewers;
    if($parent->Editors) $this->Editors = $parent->Editors;
    if($parent->ViewersGroup) $this->ViewersGroup = $parent->ViewersGroup;
    if($parent->EditorsGroup) $this->EditorsGroup = $parent->EditorsGroup;
    }
    }

    parent::onBeforeWrite();
    }

  • Twenty3design
    Avatar
    Community Member
    7 Posts

    Re: Child inherit Parent edit permissions Link to this post

    Ahh, I'm using 2.2.3.
    That's why I couldn't find the option!

    Thanks for the code for 2.2x.
    As always a great reply, thanks Aram.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Child inherit Parent edit permissions Link to this post

    Your welcome

  • headless_pnub
    Avatar
    Community Member
    17 Posts

    Re: Child inherit Parent edit permissions Link to this post

    Do you need to call this function somehow or should it just run? I can't seem to get it to work.
    Sorry for my noobness.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Child inherit Parent edit permissions Link to this post

    The function gets run automatically before the page is saved/written.

    if you want it to retake the permisions each time you save it then remove the first if() statement so that it will do it for existing pages, otherwise this will only work when creating a new page.

  • headless_pnub
    Avatar
    Community Member
    17 Posts

    Re: Child inherit Parent edit permissions Link to this post

    Of course!

    Brilliant, thanks.

    1267 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.