【问题标题】:Wagtail how to filter pages a user has view access to via their groupsWagtail 如何过滤用户可以通过其组查看访问权限的页面
【发布时间】:2019-10-01 18:52:52
【问题描述】:

我正在实现一个搜索功能,该功能返回用户可以通过他们的组访问的页面。页面在编辑时通过 Wagtail 管理页面隐私设置设置这些设置。

例如,页面只能对 Editors 组中的用户可见。因此,当不在 Editors 组中的用户搜索此页面时,应将其过滤掉。

如何有效过滤用户无法通过这种方式访问​​的页面?我找不到任何明确的方法来做到这一点。

【问题讨论】:

    标签: wagtail wagtail-search


    【解决方案1】:

    为了搜索引擎优化目的回答我自己的问题。

    在研究 Wagtail 源代码后,我发现 Wagtail 在内部使用 PageViewRestriction 模型。

    我最终使用这个 sn-p 来解决我的问题:

    from wagtail.core.models import Page, PageViewRestriction
    
    def filter_pages(user):
        pages = Page.objects.live()
    
        # Unauthenticated users can only see public pages
        if not user.is_authenticated:
            pages = pages.public()
        # Superusers can implicitly view all pages. No further filtering required
        elif not user.is_superuser:
            # Get all page ids where the user's groups do NOT have access to
            disallowed_ids = PageViewRestriction.objects.exclude(groups__id=user.groups.all()).values_list("page", flat=True)
            # Exclude all pages with disallowed ids
            pages = pages.exclude(id__in=disallowed_ids)
    
        return pages
    

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 1970-01-01
      • 2010-11-01
      • 2012-09-14
      • 2014-09-28
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2021-01-24
      相关资源
      最近更新 更多