问题登录cakephp
issue Login cakephp
我是 cakephp 新手,我有 1 个问题,请帮助我!
我有 1 个 table 名称 quan_tri_viens 相同的用户,但我不使用 table 用户
我阅读并遵循说明
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
但是我无法登录
型号: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>
型号: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;
}
}
型号: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');
}
}
上次:用户名或密码无效,请重试
机智: ID:123
通过:123 -> $2a$10$y2RsvsN5N0COAdnAEhNeW.BYNTfqk.RBISReRHb.a12qrEKTYb6Ui
添加 userModel
设置为您的 login/users 使用 'User' 以外的模型:
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'userModel' => 'QuanTriVien' // <-- ADD THIS
)
)
详情here.
我是 cakephp 新手,我有 1 个问题,请帮助我! 我有 1 个 table 名称 quan_tri_viens 相同的用户,但我不使用 table 用户
我阅读并遵循说明 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
但是我无法登录
型号: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>
型号: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;
}
}
型号: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');
}
}
上次:用户名或密码无效,请重试 机智: ID:123 通过:123 -> $2a$10$y2RsvsN5N0COAdnAEhNeW.BYNTfqk.RBISReRHb.a12qrEKTYb6Ui
添加 userModel
设置为您的 login/users 使用 'User' 以外的模型:
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'userModel' => 'QuanTriVien' // <-- ADD THIS
)
)
详情here.