0

我试图在 Auth 组件中使用 Employee 模型而不是 User 模型。我的代码是:

应用控制器

public $components = array('Session','Auth');
function beforeFilter(){
    Security::setHash('md5');
    $this->Auth->userModel = 'Employee';
    $this->Auth->fields = array('username'=>'code','password'=>'password');
    $this->Auth->loginAction = array('controller'=>'employees','action'=>'login');
    $this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home');
    $this->Auth->loginError = 'Invalid employee code or password, please try again';
    $this->Auth->logoutRedirect = array('controller'=>'employees','action'=>'login');
}
function beforeRender(){
    $this->set('Employee',$this->Auth->user());
}

员工控制器

function login(){
    $this->layout = 'login';
}
function logout(){
    $this->Redirect($this->Auth->logout());
}
function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('signup');
}
function beforeRender(){
    parent::beforeRender();
}

登录.ctp

<?php
echo $this->Form->create('Employee',array('/employees/login'));
echo $this->Form->input('code',array('label'=>'Employee code'));
echo $this->Form->input('password');
echo $this->Form->submit('Sign in',array('class'=>'b-button'));
echo $this->Form->end();
?>

问题是,当我单击登录按钮时,页面只是刷新。没有重定向,没有错误消息,什么都没有。

我在做什么有什么错误吗?

编辑

function login(){
    $this->layout = 'login';
    if($this->request->is('post')){
        if($this->Auth->login($this->request->data)){
            $this->Redirect('/pages/home');
        }else{
            $this->Session->setFlash('incorrect');
        }
    }
}
function logout(){
    $this->redirect($this->Auth->logout());
}

现在,无论输入的员工代码或密码如何,它都允许登录。事实上,即使登录字段为空,它也会登录。

4

3 回答 3

1

尝试以下操作:

echo $this->Form->create('Employee', array('controller' => 'employees', 'action' => 'login'));

并在您的EmployeesController.php 的登录方法中检查用户的真实性

public function login()
{       
    if ($this->request->is('post'))
    {
        if ($this->Auth->login($this->request->data))
        {               
         /*... do what you want...*/
        }
        else
        {
          this->Session->setFlash('Your username or password was incorrect.');
        }
    }
} 
于 2012-08-07T08:03:41.417 回答
0

我有类似的问题。最后是解决方案。

a) 在 $components 中定义表名和文件。过滤前不要使用

public $components = array('Session', 'Acl', 'Auth' => array('authenticate' => array('Form' => array('userModel' => 'Employee','fields' => array('username' => 'code'), 'password' => 'password'))), 'Cookie');

b) 切勿使用

$this->Auth->login($this->request->data)

这会为您发布的任何垃圾创建会话。而是使用

$this->Auth->login()

c) cakephp 的散列方法也不是 md5。您可能想要删除该行。

于 2012-08-15T12:10:19.613 回答
0
echo $this->Form->create('Employee',array('/employees/login'));

尝试:

echo $this->Form->create('Employee',array('action'=>'login'));
于 2012-08-07T07:34:11.590 回答