【问题标题】:Silverstripe 3 - Unable to implement controller access security from CMSSilverstripe 3 - 无法从 CMS 实施控制器访问安全性
【发布时间】:2014-11-07 18:33:11
【问题描述】:

下午好,

我还是 silverstripe 的新手,我正在尝试找出一些非常简单的任务。

目前,我正在尝试从我的页面控制器功能中实现安全限制,该功能已在我的 DataObject 中创建并通过 CMS 进行配置。

但是,无论我是否授予用户查看对象的权限,用户都会看到它。

请看下面的例子:

class MyComponent extends DataObject implements PermissionProvider{
  ///>... this is just a snippet not the full class ...

  ///>@Override
  public function canView($member = null){
      return Permission::check('COMPONENT_VIEW');
  }//canView

  /**
   * \brief the rest of the permission functions follow the same format as above 
   * i.e: canEdit, canDelete, canCreate
   */

  ///>@Override
  function providePermissions(){
     return array(
        'COMPONENT_VIEW' => 'Can view a component object',
        'COMPONENT_EDIT' => 'Can edit a component object',
        'COMPONENT_DELETE' => 'Can delete a component object',
        'COMPONENT_CREATE' => 'Can create a component object',
     );
  }//providePermissions


}//class

好的,所以上面的课程效果很好;我可以从 CMS 管理部分为用户在组内打开|关闭权限。

这就是问题所在,请参见下面的代码:

///>Controller class snippet
class My_Controller extends Page_Controller{

      public function ListMyComponents(){
          $components = MyComponent::get()->filter(array('Status' => 'Enable'));

          ///>NOTE: How can I check to see if the user has access to view the component???
          ///> I've even tried, Member::canView(Member::currentUser()); It doesn't work!

          return $components;
      }//ListMyComponents
}//class

///>ss template file snippet
<% if ListMyComponents %>
   <% loop ListMyComponents %>
       $Title
   <% end_loop %>
<% end_if %>

感谢您的帮助。

【问题讨论】:

    标签: security controller content-management-system silverstripe


    【解决方案1】:

    我想通了。 基本上,我也可以在 Controller 中执行 Permission::check。解决方法见以下代码:

    public function ListMyComponents(){
        $components = null;
        if(Permission::check('COMPONENT_VIEW')){
           $components = MyComponent::get()->filter(array('Status' => 'Enable'));
        }
        return $components;
    }//ListMyComponents
    

    感谢那些可能一直在研究解决这个问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2014-09-09
      相关资源
      最近更新 更多