0

我在登录表单中遇到问题 $this->Auth->login() 总是返回 false。我为用户帐户使用不同的表名。以下是我的代码

HgAdminscontroller.php

    <?php
    // app/Controller/UsersController.php
    class HgAdminsController extends AppController 
{
    public function beforeFilter() 
    {
        parent::beforeFilter();
        //$this->Auth->allow('add', 'logout');
    }
    public function index() {
        $this->HgAdmin->recursive = 0;
        $this->set('users', $this->paginate());
    }
    /*
public function add() {
if ($this->request->is('post')) {
$this->HgAdmin->create();
if ($this->HgAdmin->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}*/
    public function login() {

    if ($this->request->is('post')) {
    echo AuthComponent::password($this->data['password']);
    debug($this->data);
        if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
        } else {
        $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
    }
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}
?>

appController.php
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
    class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
        'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
        )
    );
    public function beforeFilter() {
        $this->Auth->allow('index', 'view');
//      Security::setHash('md5');
        $this->Auth->userModel = 'hg_admins';
        $this->Auth->loginAction = array( 'controller' => 'HgAdmins', 'action' => 'login' );
    }
//...
}

HgAdmin.php //model
<?php 
class HgAdmin extends AppModel {
    var $name= "hg_admin";
    public $validate = array(
        'username' => array(
        'required' => array(
        'rule' => array('notEmpty'),
        'message' => 'A username is required'
        )
    ),
        'password' => array(
        'required' => array(
        'rule' => array('notEmpty'),
        'message' => 'A password is required'
        )
    )
    );

?>

Login view

<h2>Login Here</h2>
<?php echo $this->Session->flash('auth'); 
echo $this->form->create('HgAdmin');
echo $this->Form->input("username");
echo $this->Form->input("password");
echo $this->form->end(__("Login"));
?>

我检查了数据库密码值是否与来自 AuthComponent::password($this->data['password']); 所以不知道为什么它是失败的。如果我使用 cakephp 的添加功能来添加用户,则添加的用户使用相同的密码,但无法使用该密码登录。

4

2 回答 2

3
public $components = array(
 'Auth' => array(
    'loginAction' => array(
        'controller' => 'writers',
        'action' => 'login'

    ),
    'loginRedirect' => array(
        'controller' => 'home',
        'action' => 'index'
    ),
    'logoutRedirect' =>array (
        'controller' => 'home',
        'action' => 'index'
    ),
     'authError' => 'You have to login to access this page',
    'authenticate' => array(
        'Form' => array(
            'userModel' =>'Writer',
            'fields' => array('username' => 'email')
        )
    ),
    'scope' =>array('User.status' => '1'),
    'authorize' => array('Controller')
),
  'Session',
);

您必须使用要在其上应用身份验证的模型的名称,即我在 db 中为我的表编写器使用了 Writer。检查更改!

于 2012-11-21T11:54:49.350 回答
0

@huzefam 答案是正确的,但我只想添加以防有人使用 Bcrypt 身份验证。这里我使用 Accounts 表。应该添加这样的东西。希望这有帮助

public $components = array(
        'Auth' => array(
            'loginAction' => array(
                'controller' => 'accounts',
                'action' => 'admin_login'

            ),
            'loginRedirect' => array(
                'controller' => 'orders',
                'action' => 'admin_index'
            ),
            'logoutRedirect' =>array (
                'controller' => 'accounts',
                'action' => 'admin_login'
            ),
            'authError' => 'You have to login to access this page!',
            'authenticate' => array(
                'Blowfish' => array(
                    'userModel' =>'Account',
                    'fields' => array('password' => 'password')
                )
            ),
            'authorize' => array('Controller')
        ),
        'RequestHandler',
        'Paginator',
        'Session'
    );
于 2016-01-27T03:50:43.127 回答