【问题标题】:Using CakePHP Auth on a custom model在自定义模型上使用 CakePHP Auth
【发布时间】:2014-01-21 20:10:02
【问题描述】:

我使用员工作为模型来处理用户名和密码。它确实重定向到正确的控制器和操作,但 $this->Auth->login() 似乎无法正常工作。有没有人发现问题?

EmployeesController

public function index() {
        //Set layout to login
        $this->layout = 'login';

        //Set salt
        //Convert user password to md5
        $this->request->data['password'] = md5($this->request->data['password'].$salt); 

         //if already logged-in, redirect
        if($this->Session->check('Auth.User')){
            $this->redirect(array('action' => 'index'));      
        }
        if ($this->request->is('post')) {
                echo "here";
            if ($this->Auth->login()) {
                echo $this->Auth->user('username');
                $this->redirect($this->Auth->redirectUrl());
            } else {
                echo "Invlaid Username";
            }
        } 

    }

AppController

public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array(
                'controller' => 'Employees',
                'action' => 'index',
            ),
            'authenticate' => array(
                'all' => array('userModel' => 'Employee'),
                'Form' => array(
                    'userModel' => 'Employee',
                    'fields' => array(
                        'username' => 'employee_id',
                        'password' => 'password',
                    )
                )
            )   
        ),
    );

print_r of $this->request->data

数组([员工] => 数组([用户名] => tmoorlag [密码] => a8c407a6218250985ccd24ff9ec6))

【问题讨论】:

    标签: php cakephp authentication


    【解决方案1】:

    应该是

    $this->request->data['Employee']['password']  = md5($this->request->data['Employee']['password'].$salt);
    

    【讨论】:

    • 仍然没有变化。如何告诉 Auth 使用默认用户模型以外的模型?
    • try: 'authenticate' => array('all' => array('userModel' => 'Emplyee'), 'Form' => array(...)。 “invalid username”错误?能否在尝试登录前调试$this->request->data,看看数据是否正确?
    • 进行了更改。它说用户名无效,我在顶部给出了 print_r 的结果。
    • 您为什么要更改密码?在进行查询之前,Auth 应该自行对密码进行哈希处理。您在创建用户时是否也在使用相同的哈希函数/盐?
    • 是的,我们没有从 CakePHP 开始,所以只是使用 md5() 这可能是问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 2016-07-02
    • 2018-07-26
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    相关资源
    最近更新 更多