我正在使用cakephp 2.6.7 在AppController.php中
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password' //Default is 'password' in the userModel
),
'userModel' => 'Admin',
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
),
'loginAction' => array(
'controller' => 'admins',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'admins', 'action' => 'deshboard'),
'logoutRedirect' => array('controller' => 'admins', 'action' => 'login'),
'authError' => "You can't acces that page",
'authorize' => 'Controller'
)
);
public function beforeFilter() {
//parent::beforeFilter();
$this->Auth->allow('index');
}
在AdminsController.php中
<?php
class AdminsController extends AppController {
var $layout = 'admin';
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('create');
}
function login() {
$this->loadModel('Admin');
$this->layout = "admin-login";
// if already logged in check this step
if ($this->Auth->loggedIn()) {
return $this->Auth->loginRedirect; //(array('action' => 'deshboard'));
}
// after submit login form check this step
if ($this->request->is('post')) {
if ($this->Auth->login()) {
echo 'ok<br/>';
echo 'last query: '.$this->Admin->getLastQuery();
exit;
return $this->Auth->loginRedirect;
} else {
echo 'Not ok<br/>';
echo 'last query: '.$this->Admin->getLastQuery();
exit;
$this->Session->setFlash('Invalid username/password combination OR you are blocked, try again');
return $this->Auth->logoutRedirect;
}
}
}
}
模型/ admin.php的
<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class Admin extends AppModel {
var $name = "admin";
var $belongsTo = array('Role');
public $validate = array(
'email' => array(
'rule' => 'isUnique',
'required' => true,
'message' => 'Email already exist'
),
'password' => array(
'rule' => array('minLength', '6'),
'message' => 'password must be minimum 6 characters long'
)
);
function hashPassword() {
if (!empty($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
}
function beforeSave($options = array()) {
$this->hashPassword();
return true;
}
}
?>
在View / admin / login.ctp
中<div class="container-fluid">
<div class="loginContainer">
<?php echo $this->Session->flash(); ?>
<?php
echo $this->Form->create('Admin', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'class' => 'form-horizontal',
'role' => 'form',
'id'=>'loginForm',
'url' => array('controller' => 'admins', 'action' => 'login'),
));
?>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<label class="form-label span12" for="username">
Email:
<span class="icon16 icomoon-icon-user-3 right gray marginR10"></span>
</label>
<?php
echo $this->Form->input('email', array(
'class' => 'span12',
'id' => 'username',
'type' => 'text',
));
?>
</div>
</div>
</div>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<label class="form-label span12" for="password">
Password:
<span class="icon16 icomoon-icon-locked right gray marginR10"></span>
<span class="forgot"><a href="#">Forgot your password?</a></span>
</label>
<?php
echo $this->Form->input('password', array(
'class' => 'span12',
'id' => 'password',
'type' => 'password',
));
?>
</div>
</div>
</div>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<div class="form-actions">
<div class="span12 controls">
<?php
echo $this->Form->button(
'Login', array('class' => 'btn marginR10', 'type' => 'submit')
);
?>
</div>
</div>
</div>
</div>
</div>
<?php echo $this->Form->end(); ?>
</div>
</div><!-- End .container-fluid -->
问题 $ this-&gt; auth-&gt; login()始终返回false。
I checked the last query after $this->auth->login() called. The query is:
last query: SELECT `Admin`.`id`, `Admin`.`role_id`, `Admin`.`name`, `Admin`.`email`, `Admin`.`password`, `Admin`.`mobile`, `Admin`.`area`, `Admin`.`status`, `Admin`.`comment`, `Admin`.`created`, `Role`.`id`, `Role`.`name`, `Role`.`created`, `Role`.`modified` FROM `amrajegeachi`.`admins` AS `Admin` LEFT JOIN `amrajegeachi`.`roles` AS `Role` ON (`Admin`.`role_id` = `Role`.`id`) WHERE `Admin`.`email` = 'sattar.kuet@gmail.com' LIMIT 1
在Where子句中缺少密码。我想知道我的代码有什么问题。
答案 0 :(得分:0)
在AppController BeforeFilter操作中设置变量,
$this->set('isLoggedIn',$this->Auth->loggedIn());
使用相同的变量检入其他控制器,这不会产生问题。