cakephp中的登录错误(或不存在)


Login bug (or not) in cakephp

我是CakePHP的初学者,遇到了一个问题,我不知道这是否是一个bug。我只能在注册用户后登录,如果我注销并尝试再次登录,它不起作用,它会说我的用户名或密码是错误的:(没错,t.t,我确信数据在数据库中。这很奇怪,因此我总是必须注册一个新用户才能访问我的系统。)。我的代码如下:

AppController:

class AppController extends Controller {
   public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'lista_tudo'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'lista_tudo'),
            'authorize' => array('Controller')
        )
    );
    function beforeFilter() {
        $this->Auth->allow('login','add');
    }
    public function isAuthorized($user) {
        return true; // Admin pode acessar todas actions
}
}

用户控制器:

    public $uses = array('User','UserGroup','Group');
    public $name = "Users";
    public $helpers = array('Html','Form','Meu');
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout','login','add');
    }
    public function login() {
        var_dump($this->Auth->loggedIn());
        if($this->request->is('post')){
            if ($this->Auth->login()) {
                $this->Session->setFlash('Logado com sucesso');
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash('Usuário e/ou senha inválido');
            }
        }
    }
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}

登录视图

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php echo __('Please enter your username and password'); ?></legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end('Login');?>
<?php echo $this->Html->link('Logout',array('action'=>'logout')); ?>
</div>

用户型号:

public $name='User';
        public $hasMany = array(
                'UserGroup'=>array(
                        'className'=>'UserGroup'
                    )
            );
        public $validate = array(
            'username'=>array(
                    'rule'=>'isUnique',
                    'message'=>'Usuário já existe'
                )
        );
        public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
        }

我认为您在注册后会自动登录,因此此时不必输入用户名和密码即可登录。

你能确认这一点并粘贴你的注册表视图和控制器代码吗?

密码的别名在模型中看起来是错误的,通常您会使用以下内容:

$this->Form->echo('pwd');

在您的注册表中,然后:

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['pwd'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd']); // Move hashed value from pwd to password
    }
    return true;
}

在您的模型中。

另请参阅这篇精彩的文章:http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/