cakephp 2.9.7 $this->auth->login() always returns bool(false) for vallid users also -
i have checked sinarios mentioned in stackoverflow related problem.nothing working fine me.please me in solving problem.
table hold users data
create table users ( id int unsigned auto_increment primary key, username varchar(50), password varchar(255), role varchar(20), created datetime default null, modified datetime default null );
app/controller/userscontroller.php file looks this
<?php // app/controller/userscontroller.php app::uses('appcontroller', 'controller'); class userscontroller extends appcontroller { public function beforefilter() { parent::beforefilter(); // allow users register , logout. $this->auth->allow('add', 'logout'); } public function login() { if ($this->request->is('post')) { pr($this->request->data); // echo $this->flash->render('auth'); // die(); var_dump($this->auth->login()); // die(); if ($this->auth->login()) { return $this->redirect($this->auth->redirecturl()); } $this->flash->error(__('invalid username or password, try again')); } } public function logout() { return $this->redirect($this->auth->logout()); } public function index() { $this->user->recursive = 0; $this->set('users', $this->paginate()); } public function view($id = null) { $this->user->id = $id; if (!$this->user->exists()) { throw new notfoundexception(__('invalid user')); } $this->set('user', $this->user->findbyid($id)); } public function add() { if ($this->request->is('post')) { $this->user->create(); if ($this->user->save($this->request->data)) { $this->flash->success(__('the user has been saved')); return $this->redirect(array('action' => 'index')); } $this->flash->error( __('the user not saved. please, try again.') ); } } public function edit($id = null) { $this->user->id = $id; if (!$this->user->exists()) { throw new notfoundexception(__('invalid user')); } if ($this->request->is('post') || $this->request->is('put')) { if ($this->user->save($this->request->data)) { $this->flash->success(__('the user has been saved')); return $this->redirect(array('action' => 'index')); } $this->flash->error( __('the user not saved. please, try again.') ); } else { $this->request->data = $this->user->findbyid($id); unset($this->request->data['user']['password']); } } public function delete($id = null) { // prior 2.5 use // $this->request->onlyallow('post'); $this->request->allowmethod('post'); $this->user->id = $id; if (!$this->user->exists()) { throw new notfoundexception(__('invalid user')); } if ($this->user->delete()) { $this->flash->success(__('user deleted')); return $this->redirect(array('action' => 'index')); } $this->flash->error(__('user not deleted')); return $this->redirect(array('action' => 'index')); } } ?>
app/model/user.php file looks this
<?php app::uses('appmodel', 'model'); app::uses('blowfishpasswordhasher', 'controller/component/auth'); class user extends appmodel { public $validate = array( 'username' => array( 'required' => array( 'rule' => 'notblank', 'message' => 'a username required' ) ), 'password' => array( 'required' => array( 'rule' => 'notblank', 'message' => 'a password required' ) ), 'role' => array( 'valid' => array( 'rule' => array('inlist', array('admin', 'author')), 'message' => 'please enter valid role', 'allowempty' => false ) ) ); public function beforesave($options = array()) { if (isset($this->data[$this->alias]['password'])) { $passwordhasher = new blowfishpasswordhasher(); //echo $passwordhasher->hash($this->data[$this->alias]['password']); //echo $this->data[$this->alias]['password'] ; //exit(); $this->data[$this->alias]['password'] = $passwordhasher->hash( $this->data[$this->alias]['password'] ); } //echo "true"; return true; } } ?>
app/view/users/login.ctp
<div class="users form"> <?php echo $this->flash->render('auth'); ?> <?php echo $this->form->create('user'); ?> <fieldset> <legend> <?php echo __('please enter username , password'); ?> </legend> <?php echo $this->form->input('username'); echo $this->form->input('password'); ?> </fieldset> <?php echo $this->form->end(__('login')); ?> </div>
app/controller/appcontroller.php file looks this
<?php @copyright copyright (c) cake software foundation, inc. (http://cakefoundation.org) * @link http://cakephp.org cakephp(tm) project * @package app.controller * @since cakephp(tm) v 0.2.9 * @license http://www.opensource.org/licenses/mit-license.php mit license */ app::uses('controller', 'controller'); /** * application controller * * add application-wide methods in class below, controllers * inherit them. * * @package app.controller * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller */ class appcontroller extends controller { public function index() { } //public $components = array('debugkit.toolbar'); public function view() { } public $components = array('session', 'flash', 'auth' => array( 'loginredirect' => array( 'controller' => 'posts', 'action' => 'index' ), 'logoutredirect' => array( 'controller' => 'pages', 'action' => 'display', 'home' ), 'authenticate' => array( 'form' => array( 'passwordhasher' => 'blowfish' ) ), 'authorize' => array('controller') ) ); public function isauthorized($user) { // admin can access every action if (isset($user['role']) && $user['role'] === 'admin') { return true; } return false; } public function beforefilter() { $this->auth->allow('index', 'view'); } }
i have created files same in https://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html tutorial.but when go users/login url returns invalid username,or password valid username or password also.
Comments
Post a Comment