Ad

October CMS Static Pages Plugin - Hide / Show Pages In The Backend Based On User Roles?

How can I hide some static pages based on the user's role?

I defined the role of users with the name "blabla". Now I want to hide all the pages from these users, except for the page "blabla" in the "Static Pages" backend. How can i do this?

sorry for my English))

Ad

Answer

yes of course you can do it but we need to write some code here.

we can utilize cms.object.listInTheme event

In your plugin within boot method you can add this event listener and filter static pages.

\Event::listen('cms.object.listInTheme', function ($cmsObject, $objectList) {

    // lets check if we are really running in static pages
    // you can also add more checks here based on controllers etc ..
    if ($cmsObject instanceof \RainLab\Pages\Classes\Page) {

        $user = \BackendAuth::getUser();
        // role code and role name are different things
        // we should use role code as it act as constant
        $hasRoleFromWhichIneedTohidePages = $user->role->code === 'blabla' ? true : false;

        // if user has that role then we start filtering
        if($hasRoleFromWhichIneedTohidePages) {
            foreach ($objectList as $index => $page) {

                // we can use different matching you can use one of them
                // to identify your page which you want to hide. 
                // forgot method will hide that page

                // match against filename 
                if ($page->fileName == 'hidethispage.htm') {
                    $objectList->forget($index);
                }

                // OR match against title
                if ($page->title == 'hidethispage') {
                    $objectList->forget($index);
                }

                // OR match against url
                if ($page->url == '/hidethispage') {
                    $objectList->forget($index);
                }
             }
         }
    }
});

currently this code will check page-url / title / file-name and restrict user statically from showing page in list but you can put your own logic here and make things dynamic.

if you didn't get it or want dynamic solution then please comment , I will explain in more detail.

Ad
source: stackoverflow.com
Ad