【问题标题】:Allow permission to some page cakephp允许某些页面 cakephp 的权限
【发布时间】:2013-11-13 21:54:15
【问题描述】:

我想在 cakephp 中为我的网站创建一些权限,但不能进行权限检查。 我只想例如只允许页面add 其他页面如indexregister 没有访问权限。

这是我的 AppController 组件

public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array('controller'=>'users','action'=>'login', 'admin'=>false),
            'logoutRedirect' => array('controller'=>'users','action'=>'logout'),
            'loginRedirect' => array('controller'=>'shows', 'action'=>'index'),
            'authError' => 'Questa risorsa non sembra appartenere al tuo account, oppure non hai eseguito l\'accesso',
            'autoRedirect' => false,
            'authorize' => array(
                'Controller',
                'Actions' => array(
                    'actionPath' => 'controllers'
                )
            ),
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email')
                )
            )
        )
    );

这是 UserController 中的 beforeFilter:

public function beforeFilter () {
   parent::beforeFilter();  
   $this->Auth->deny('*'); //I have also tried $this->Auth->deny();
   $this->Auth->allow('register');
}

为什么我可以访问其他页面?谢谢

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    来自 Cakephp 书:默认情况下,所有操作都需要授权。但是,在公开操作后,您希望撤销公共访问权限。您可以使用 AuthComponent::deny() 来做到这一点: 您对拒绝所做的事情可能只是因为对 Auth 的工作原理缺乏了解。请检查此http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html $this->Auth->拒绝(); // 将删除所有动作。

    【讨论】:

    • 我加 $this->Auth->deny('index');单个用户可以访问该页面,然后不再工作
    【解决方案2】:

    您应该使用 cakephp 的 Acl 组件,它为您提供了一个完美的场景,您可以在该场景中决定您应该授予哪个页面权限,哪个页面不授予权限

    阅读:- http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html

    然后在 AppController 中调用组件

    class AppController extends Controller {
    
    public $components = array(
        'Acl'
    

    }

    【讨论】:

    • Acl 用于其他目的,我知道使用 Auth 允许您可以在没有 Acl 的情况下管理此访问权限
    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多