繁体   English   中英

CakePHP 2.4 Auth-> login()始终返回TRUE

[英]CakePHP 2.4 Auth->login() always return TRUE

我正在为我的妈妈开发一个带有Cake 2.4的简单登录应用程序。 这是用户模型的代码。

App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class User extends AppModel{
        public $validate = array(
                'username' => array(
                        'required' => array(
                                'rule' => array('notEmpty'),
                                'message' => 'A username is required'
                        )
                ),
                'password' => array(
                        'required' => array(
                                'rule' => array('notEmpty'),
                                'message' => 'A password is required'
                        )
                )
        );

        public function beforeSave($options = array()) {
                if(isset($this->data[$this->alias]['password'])) {
                        $passwordHasher = new SimplePasswordHasher();
                        $this->data[$this->alias]['password'] = $passwordHasher->hash(
                        $this->data[$this->alias]['password']
                        );
                }
                return true;
        }
}

我的UsersController代码是...

class UsersController extends AppController{
        public $helpers = array('Html','Form');

        public function beforeFilter(){
                parent::beforeFilter();
                $this->Auth->allow('add');
        }
        public function add(){
                if($this->request->is('post')){
                        $this->User->create();
                        if($this->User->save($this->request->data)){
                                $this->Session->setFlash(__('The user has been saved'));
                                return $this->redirect(array('action' => 'index'));
                        }
                        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
        }
        public function login(){
                $this->layout = 'mlayout';
                if($this->request->is('post')){
                        debug($this->Auth->login());
                        var_dump($this->request->data);
                        if($this->Auth->login()){
                                $this->Session->setFlash('Logged In');
                                //return $this->redirect($this->Auth->redirectUrl());
                        }else{
                                $this->Session->setFlash(__('Usuario o Contraseña invalido, intentelo de nuevo.'));
                        }
                }
        }
        public function logout(){
                return $this->redirect($this->Auth->logout());
        }
}

AppController代码:

class AppController extends Controller{

        public $components = array('Session',
                'Auth' => array(
                        'loginRedirect' => array(
                                'controller' => 'homes',
                                'action' => 'index'
                        ),
                        'logoutRedirect' => array(
                                'controller' => 'homes',
                                'action' => 'index'
                        )
                )
        );

        public function beforeFilter(){
                $this->Auth->allow('index','view','login');
        }
}

Login.ctp

                <table id="formtable">
                <form id="UserForm" method="post" action="/mercadito/users/login">
                <tr>
                        <td align="right">Login -> </td>
                        <td><input type="text" name="username" style="width: 150px; height: 30px;"/></td>
                </tr>
                <tr>
                        <td align="right">Contraseña -> </td>
                        <td><input type="password" name="password" style="width: 150px; height: 30px;"/></td>
                </tr>
                <tr>
                        <td></td>
                        <td align="center"><input type="submit" value="ENTRAR" style="width: 100px; height: 30px;"/></td>
                </tr>
                </form>
                </table>

debug($ this-> Auth-> login())返回:/app/Controller/UsersController.php(第23行)true

var_dump()返回:array(2){[“” username“] => string(9)” ddfdsffsd“ [” password“] => string(9)” fdsfdssfd“}

无论登录表单中的输入如何,每次都会发生这种情况。

您需要配置授权处理程序以在控制器中使用beforeFilter,请参阅: 配置授权处理程序

'authorize'=>'Controller'添加到您的AppController中:

class AppController extends Controller{

     public $components = array('Session',
            'Auth' => array(
                    'loginRedirect' => array(
                            'controller' => 'homes',
                            'action' => 'index'
                    ),
                    'logoutRedirect' => array(
                            'controller' => 'homes',
                            'action' => 'index'
                    ),
                    'authorize' => 'Controller'
            )
    );

    public function beforeFilter(){
            $this->Auth->allow('index','view','login');
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM