issue Login cakephp

72 Views Asked by At

i'm new cakephp and i have 1 question, please help me! I have 1 table name quan_tri_viens same users, but i don't use table users

i read and follow the instructions http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

but i can not sign in

Model: QuanTriViensController

<?php
App::uses('AppController', 'Controller');

class QuanTriViensController extends AppController {

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('add', 'logout','login');
    }

    public function login() {
        if ($this->request->is('post')) 
        {
            if ($this->Auth->login())
            {
                return $this->redirect($this->Auth->redirectUrl());
            }
        $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }



    public function view($id = null) {
        $this->QuanTriVien->id = $id;
        if (!$this->QuanTriVien->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('quantrivien', $this->QuanTriVien->read(null, $id));
    }

    public function index() {
        $this->QuanTriVien->recursive = 0;
        $this->set('quantriviens', $this->paginate());
    }
    public function add() {
        if ($this->request->is('post')) {
            $this->QuanTriVien->create();
            if ($this->QuanTriVien->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.')
            );
        }
    }
}

AppController

<?php
class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'tins',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );

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

}

login.ctp

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('QuanTriVien'); ?>
    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

Model: QuanTriVien.php

<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class QuanTriVien 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'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );

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

}

Model: AppModel.php

<?php

App::uses('Model', 'Model');
class AppModel extends Model {

public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'posts',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );

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

Last :Invalid username or password, try again witd: id: 123 pass: 123 -> $2a$10$y2RsvsN5N0COAdnAEhNeW.BYNTfqk.RBISReRHb.a12qrEKTYb6Ui

1

There are 1 best solutions below

0
On BEST ANSWER

Add the userModel setting to use a model other than 'User' for your login/users:

'authenticate' => array(
    'Form' => array(
        'passwordHasher' => 'Blowfish',
        'userModel' => 'QuanTriVien' // <-- ADD THIS
    )
)

Details here.