如何允许未经身份验证的用户访问CakePHP中的内容?

时间:2013-12-16 22:21:18

标签: php cakephp

我在cakephp 2.x中有一个网站,我希望访客用户(未登录)可以看到相同的页面:
用户/登录
用户/ forgot_password
用户/ reset_password

我的AuthComponent无法访问我的页面。我可以访问用户/登录但不能访问forgot_password和reset_password,如果我尝试继续重定向到登录页面。

这是我的AppController与AuthComponent:

public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array('controller'=>'users','action'=>'login', 'admin'=>false),
            'logoutRedirect' => array('controller'=>'users','action'=>'login'),
            'loginRedirect' => array('controller'=>'projects', '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')
                )
            )
        )
    );

在这种情况下,我无法访问forgot_password和reset_password。

但如果我改成它:

public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => null,
            'logoutRedirect' => array('controller'=>'users','action'=>'login'),
            'loginRedirect' => array('controller'=>'projects', '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')
                )
            )
        )
    );

我已将null设置为loginAction,在这种情况下我可以访问forgot_password和reset_password但页面为空且没有错误,正文为空。这些页面只是html,没有这样的查询:

控制器动作

public function forgot_password(){

}

查看

<div>
    <p>RESTORE PASSWORD</p>
</div>
有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:3)

beforeFilter()回调中,添加$this->Auth->allow('forgot_password');

Api:AuthComponent:allow()

相关问题