October CMS 静态页面插件 - 根据用户角色在后端隐藏/显示页面?

October CMS Static Pages plugin - hide / show pages in the backend based on user roles?

如何根据用户角色隐藏一些静态页面?

我定义了名为 "blabla" 的用户的角色。 现在我想对这些用户隐藏所有页面,"Static Pages" 后端中的页面 "blabla" 除外。 我该怎么做?

对不起我的英语))

当然可以,但我们需要在这里编写一些代码。

我们可以利用cms.object.listInTheme事件

在您的插件中的引导方法中,您可以添加此事件侦听器并过滤静态页面。

\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);
                }
             }
         }
    }
});

目前此代码将检查 page-url / title / file-name 并静态限制用户在列表中显示页面,但您可以在此处放置您自己的逻辑并使事情充满活力。

如果你不明白或者想要动态解决方案那么请评论,我会更详细地解释。