0

我想让我的 users 控制器Auth访问login(),logout()add()action ,但我是否使用并不重要,$this->Auth->allow('logout');我会收到消息:You are not authorized to access that location. login()并且add()工作正常。

这是我的 AppContoller.php:

class AppController extends Controller {

    public $components = array(

        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'User',
                    'fields' => array(
                    'username' => 'email', 'password' => 'password')
                    )

            ), 

            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'landing')
        ), 'Session'
    );

    public function beforeFilter() {
        $this->Auth->allow('add', 'login');
    }

}

这是我的 UsersController.php 的相关部分:

    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');


    public function beforeFilter() {

        parent::beforeFilter();
        $this->Auth->allow('logout');

    }
    public function logout() {
        $this->set('title_for_layout', 'Logout');
        $this->redirect($this->Auth->logout());
    }

有人看到这里的问题吗?我感谢您的帮助。

4

1 回答 1

0

您似乎可以毫无问题地访问注销操作,但注销重定向会破坏您的会话并将您重定向到page视图。

您似乎无法在未登录的情况下访问该页面。(您可以尝试在不登录的情况下访问该 URL)

在您的添加beforeFilter功能PagesController

public function beforeFilter(){
    parent::beforeFilter();

    $this->Auth->allow();
}

PagesController comes by default with CakePHP 2.2, if you don't have it, just copy and paste any other controller and add this function deleting all the rest.

EDITED: If the PagesController was already there, just add the beforeFilter function.

于 2012-12-14T10:51:33.467 回答