我正在使用 Wagtail 开发一个小型网站。该网站由一个“主页”和几个“子页面”组成。到目前为止,一切都非常简单!但是,根据用户(不是管理员)所在的组,应该显示正确的子页面!
请参阅以下设置(最小化),以了解我在说什么。
如果我在 ToolKitPart 上设置权限(例如要求显式用户登录和组成员身份),则会发生以下情况:
当使用完全限定路径访问页面时,会要求用户登录,在权限不足的情况下,用户将看不到内容!
-
当转到 ToolkitIndex-Page 时,会显示所有子项,包括用户永远不应该看到的子项,而无需登录或成为某个组的成员。
class ToolkitIndex(Page):
def get_context(self, request):
# Update context to include only published posts, ordered by reverse-chron
context = super().get_context(request)
blogpages = self.get_children().live().order_by('-first_published_at')
context['pages'] = blogpages
return context
class ToolkitPart(Page):
body = StreamField([
('staff', MpStaff()),
('news', MpNews()),
('stuff', MpStuff()),
('teditor', blocks.RichTextBlock()),
('reditor', blocks.RawHTMLBlock()),
], blank=True)
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
class MpNews(blocks.StructBlock):
head = blocks.TextBlock(required=True, help_text='Schlagzeile')
lead = blocks.TextBlock(required=False, help_text='Einleitung')
body = blocks.RichTextBlock(required=True, help_text='Inhalt')
image = ImageChooserBlock(required=False)
type = blocks.ChoiceBlock(
choices=[('default', 'Standard'),
('highlight', 'Hervorgehoben'),
], required=True)
class Meta:
template = 'home/mparts/toolkit_news.html'
icon = 'code'
知道如何解决这个问题吗?
假设您已使用 Wagtail 设置了这些权限私人页面 https://docs.wagtail.io/en/stable/advanced_topics/privacy.html功能,这些存储在PageViewRestriction https://github.com/wagtail/wagtail/blob/dbb7ec77b30a7bd55201e069603b1ae3e6aa3b90/wagtail/core/models.py#L1915模型。不幸的是,Wagtail 目前不提供对当前页面请求以外的任何内容应用这些权限检查的方法,因此您必须自己重新创建此逻辑,以将查询集过滤到用户的查看权限。这将类似于(未经测试):
from django.db.models import Q
class ToolkitIndex(Page):
def get_context(self, request):
context = super().get_context(request)
blogpages = self.get_children().live().order_by('-first_published_at')
if not request.user.is_authenticated:
blogpages = blogpages.public() # only pages with no view restrictions at all
else:
blogpages = blogpages.filter(
# pages with no view restrictions
Q(view_restrictions__isnull=True)
# pages restricted to any logged-in user
| Q(view_restrictions__restriction_type='login')
# pages restricted by group
| Q(view_restrictions__restriction_type='groups', view_restrictions__groups__in=request.user.groups.all())
)
免责声明:
- 这不考虑受共享密码保护的页面
- 为了完全正确,我们需要考虑视图限制沿着树传播的事实(因此即使子页面没有直接附加视图限制记录,它仍然可能受到限制);然而,我们只查看当前页面的直接子页面(它们显然是do可以访问...),这样这个问题就不会出现在这里。
-
PageViewRestriction
不是公共 Wagtail API,可能会在未来版本中发生变化 - 特别是,请参阅RFC 32 https://github.com/wagtail/rfcs/pull/32对于在不久的将来可能发生的拟议变更。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)