【问题标题】:How to set default security access for a Page Type in Silverstripe?如何在 Silverstripe 中为页面类型设置默认安全访问?
【发布时间】:2014-03-06 23:10:36
【问题描述】:

我有一个自定义页面类型,需要使用以下默认安全设置创建(因为它们会出现在 CMS 中:

谁可以查看此页面?
只有这些人(从列表中选择)
管理员、内容作者

在数据模型中,这将涉及 SiteTree 中的 CanViewType = OnlyTheseUsers - 以及 SiteTree_ViewerGroups 中的两行,其中 SiteTreeID = ID 和 GroupID = 1 & 2。

有没有办法通过框架做到这一点,还是我应该使用 OnBeforeWrite 直接操作数据库?

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

    您可以使用populateDefaults() 函数设置默认的CanViewTypeViewerGroups

    public function populateDefaults() {
        $this->CanViewType = 'OnlyTheseUsers';
        $this->ViewerGroups()->add(Group::get()->byID(1));
        $this->ViewerGroups()->add(Group::get()->byID(2));
    
        parent::populateDefaults();
    }
    

    这将添加 2 个用户组并将可以查看类型设置为“OnlyTheseUsers”。

    另一种解决方案是将canView 函数设置为仅允许组 1 或组 2 中的成员查看页面:

    public function canView($member = NULL) {
        $currentUser = Member::currentUser();
        if ($currentUser && ($currentUser->inGroup(1) || $currentUser->inGroup(2))) {
            return true;
        }
        return false;
    }
    

    【讨论】:

    • 如果无法在 CMS 中设置,这是一个很好的后备方案。谢谢。
    • 谢谢!只是想要我需要,除了 $this->CanViewType = 'LoggedInUsers';
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2014-12-06
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多