CakePHP 2.x认证自定义用户名(代码到位,但不工作!)



对不起,我不想问,但我花了几个小时来研究这个问题,但没有任何运气。

CakePHP(运行最新版本)似乎拒绝使用字段设置(这样我就可以使用数据库中的电子邮件列作为用户名)。如果我将其设置为"email",这是我希望从数据库中使用的字段,它只是拒绝登录,说明不正确的详细信息。由于某种原因,在DebugKit中无法从SQL中获得任何输出。虽然当它被设置为用户名如下所示,它的工作很好,只是在数据库中使用"temp"列。我试过把它放在组件变量中,但也没有运气。我能做错什么吗?打开调试,在日志或浏览器中看不到任何错误。

模型包含一个电子邮件列。

控制器/AppController.php

class AppController extends Controller {
        public $components = array(
                'Session',
                'DebugKit.Toolbar',
                'Auth' => array(
                        'allow' => array('login','logout'),
                        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                        'authorize' => 'Controller'
                )
        );
        function beforeFilter() {
                Security::setHash('md5');
                $this->Auth->authenticate = array(
                        'Form' => array(
                                'fields' => array(
                                        'username' => 'username',
                                ),
                        ),
                );
        }
}

控制器/UserController.php

class UsersController extends AppController {
        public $uses = array('User');
        public function beforeFilter() {
                parent::beforeFilter();
        }
        public function isAuthorized($user){
                return true;
        }
        public function login() {
                $this->layout = 'login';
                if ($this->request->is('post')) {
                        if ($this->Auth->login()) {
                                $this->redirect($this->Auth->redirect());
                        } else {
                                $this->Session->setFlash('Invalid username or password, try again','flash_error');
                        }
                }
        }
        public function logout() {
                $this->layout = 'login';
                $this->Session->setFlash('Successfully logged out!','flash_success');
                $this->redirect($this->Auth->logout());
        }
}

视图/用户/login.ctp

<?php
        $this->set('title_for_layout', 'Login');
        echo $this->Session->flash();
        echo $this->Session->flash('auth','flash_info');
        echo $this->Form->create('User', array(
                'action' => 'login'
        ));
        echo $this->Form->input('username',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Email:'
        ));
        echo $this->Form->input('password',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Password:'
        ));
        echo $this->Form->submit('Login', array(
                'class' => 'submit',
                'before' => '<p>',
                'after' => '</p>'
        ));
        echo $this->Form->end();
?>

您需要将表单上字段的名称从username更改为email。仅仅将标签设置为"email"是不够的。

        echo $this->Form->input('email',array(
            'between' => '<br/>',
            'before' => '<p>',
            'after' => '</p>',
            'class' => 'text',
            'label' => 'Email:'

尝试更新appController中的组件代码,以便像这样将认证值添加到Auth数组中:

public $components = array(
            'Session',
            'DebugKit.Toolbar',
            'Auth' => array(
                    'allow' => array('login','logout'),
                    'loginAction' => array('controller' => 'users', 'action' => 'login'),
                    'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                    'authorize' => 'Controller',
                    'authenticate' => array(
                        'Form' => array(
                                'fields' => array('username' => 'email')
                            )
                    )
            )
    );

最新更新