1

我使用 Cake 提供的哈希创建了一个用户 .. 但是当我登录时,显示“用户名或密码无效”。但没关系。

$this->Auth->login();总是返回假...

控制器

class MastersController extends AppController{

public function login(){
    if ($this->request->is('post')) {
        debug($this->Auth->login());
        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());
}}

应用控制器

class AppController extends Controller {

    public $components = array('Session', 'Cookie', 'Auth');
    function beforeFilter() {
        $this->loadModel('Master');

        $this->Auth->userModel = 'Master';
        $this->Auth->allow('*');
        // Action da tela de login
        $this->Auth->loginAction = array(
                'masters' => false,
                'controller' => 'masters',
                'action' => 'login'
        );

        // Action da tela após o login (com sucesso)
        $this->Auth->loginRedirect = array(
                'masters' => true,
                'controller' => 'masters',
                'action' => 'index'
        );

        // Action para redirecionamento após o logout
        $this->Auth->logoutRedirect = array(
                'masters' => false,
                'controller' => 'pages',
                'action' => 'login'
        );
        $this->Auth->authorize = array('controller');
        if (!isset($this->params['masters']) || !$this->params['masters'])
            $this->Auth->allow('*','login');

        $this->Auth->loginError = __('Usuário e/ou senha incorreto(s)', true);
        $this->Auth->authError = __('Você precisa fazer login para acessar esta página', true);
    }

    public function isAuthorized($masters){
        return TRUE;
    }}

查看登录.ctp

 echo 'Formulário de login';
echo $this->Session->flash('auth');
echo $this->Session->flash();
echo $this->Form->create('Master', array('controller'=>'masters','action'=>'login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Entrar');

模型

class Master extends AppModel{
public $name = 'Master';
public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Usuario requerido.'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Senha requerida.'
        )
    )
);

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;
}

我不知道为什么它会给出这个错误..这一切似乎都很好!按照他的要求,我更改了一封 Security.salt 的信件.. 帮帮我:) 我工作需要它

4

2 回答 2

1
debug($this->Auth->login());
if ($this->Auth->login()) {}

是个坏主意。第一个将让您登录,然后第二个调用将 - 当然 - 返回 false (因为您已经登录)。

如果您确实需要以这种方式进行测试,请停止代码:

debug($this->Auth->login()); die();
于 2013-08-07T09:01:18.587 回答
0

我遇到了同样的问题,为我解决的问题是将 $this->alias 更改为 User,所以 beforeSave() 现在看起来像

public function beforeSave($options = array()) {
    if (isset($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    return true;
}
于 2014-12-14T08:38:02.940 回答