如何根据用户的角色隐藏一些静态页面?
我以" Blabla"的名称定义了用户的角色。现在,我想对这些用户隐藏所有页面,除了"静态页面"后端的" blabla"之外。我该怎么做?
对不起我的英语))
是的,当然您可以做到这一点,但是我们需要在这里编写一些代码。
我们可以利用 cms.object.listintheme event
在启动方法中的插件中,您可以添加此事件侦听器和过滤器静态页面。
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 RainLabPagesClassesPage) {
$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 ,并从列表中显示page的用户静态限制,但是您可以将自己的逻辑放在此处并使事情变得动态。
如果您没有得到它或需要动态解决方案,请评论,我将更详细地解释。