0

在 post 方法中传递凭据后,我无法让 Auth 组件登录。

我正在使用 CakePHP2.*

我正在尝试编写 Web 服务。

请在我编写的用于在 AppController 中配置 Auth 组件的代码下方以及在 User 模型的 UserController 下方

类 AppController 扩展控制器 {

public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'user', 'action' => 'view'),
        'logoutRedirect' => array('controller' => 'user', 'action' => 'home'),
        'authenticate' => array('Form' => array('fields' => array('username'=>'username','password'=>'password'))),            
        'userScope'=> array('User.active_yn' => 1),             
        'userModel'=>'User',
        'loginAction'=>array('controller' => 'user', 'action' => 'login'),
        'autoRedirect'=>true,
        'authError'=>'You dont have access to that area. Please login first.',
        'loginError'=>'Username or password entered is incorrect. Please try again.',
        'authorize' => array('Controller') // Added this line
    )
);

public function isAuthorized($user) {
    // Admin can access every action
    if (isset($user['active_yn']) && $user['active_yn'] === 1) { //admin
        return true;
    }

    // Default deny
    return false;
}

public function beforeFilter() { }

}

类 UserController 扩展 AppController {

public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');    
public $validate = array(
        'email' => array('rule' => 'notEmpty')
);

public function index() {
    $this->set('User', $this->User->find('all'));
}

public function view($id = null) {

    if (!$id) {
        throw new NotFoundException(__('Invalid User'));
    }

    $User = $this->User->findById($id);
    if (!$User) {
        throw new NotFoundException(__('Invalid User'));
    }
    $this->set('User', $User);
} 


public function login() {


    if ($this->request->is('post')) {

        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }

    $this->set('request', $this->request->data);

}


public function logout() {
    return $this->redirect($this->Auth->logout());
}


public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('view','login','logout');
}

}

4

1 回答 1

0

Auth 配置中的控制器名称必须是复数形式

于 2013-10-22T15:09:58.873 回答