【问题标题】:CanView effect on DataExtension - Silverstripe 3.1.12CanView 对 DataExtension 的影响 - Silverstripe 3.1.12
【发布时间】:2015-07-02 13:03:29
【问题描述】:

我正在开发一个 Silverstripe 模块模块,允许用户订阅一个网站,该网站还发送月刊。

单个组中将有 2 个级别的用户。该组中的所有用户都可以登录该网站,但某些内容仅对有效且付费订阅的用户可见。

我有一个扩展主要 Page 对象的 DataExtension。这允许我有一个布尔字段来指示仅限订阅者的内容。我的意图是添加“canView”功能,该功能将对用户的订阅状态进行必要的检查,以隐藏非活动成员的链接,当然为管理员返回 true。

class MemberPageExtension extends DataExtension {
    static $db = array(
        'SubscribersOnly' => 'Boolean'
    );
    public function canView(){
        // perform subscription checks here
        return false ; // result will be dependent on subscription status
    }
}

不幸的是 canView() 似乎无法在 DataExtension 上工作/可用,所以现在我对如何实现这一点有点困惑。

有没有办法让页面上的 DataExtension 可以使用“can”函数,或者在不需要模板中的 if 语句的情况下产生类似的效果?

【问题讨论】:

标签: silverstripe


【解决方案1】:

引用official documentation

如果扩展需要修改现有方法,那就有点 更棘手。它要求您要自定义的方法具有 在要修改的地方提供了一个 Extension Hook 数据。

我刚刚检查了cms/code/model/SiteTree.php 代码,发现了以下几行:

// Standard mechanism for accepting permission changes from extensions
$extended = $this->extendedCan('canView', $member);
if($extended !== null) return $extended;

所以...是的,您可以修改 canView 行为,并通过声明 canView 函数(就像您所做的那样)。

您的问题出在其他地方:您必须向我们展示canView 是如何定义的。

附录

快速测试:我刚刚将此代码保存为 mysite/code/Page.php 在裸 SilverStripe 安装中。

<?php

class Page extends SiteTree {
}

class Page_Controller extends ContentController {
}

class MemberPageExtension extends DataExtension {

    public function canView(){
        // perform subscription checks here
        return false ; // result will be dependent on subscription status
    }
}

$instance = new Page();
Debug::show($instance->canView());

Page::add_extension('MemberPageExtension');

$instance = new Page();
Debug::show($instance->canView());

第一个调用返回true,第二个返回false

再次重申:您的问题出在其他地方。

【讨论】:

  • 感谢您的回复。我编辑了原始帖子以展示我通常如何使用canView() 函数。但是在这种情况下它不起作用
  • 提供的代码不显示真实代码:它只是一个占位符函数。我刚刚添加了一个代码示例,显示 canView 在扩展有效上。看来你已经对这个问题下定决心了,这对于调试来说非常糟糕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多