我的app控制器:
class AppController extends Controller {
var $uses = array('Header','SubHeader','User','UserGroup');
//public $uses = array('Admin');
public function beforeFilter() {
}
public function beforeRender() {
}
public function beforeSave($options = array())
{
}
// Check Users Authorization
public function isAuthorized($type=array())
{
return true;
}
}
我的routes.php:
Router::connect('/', array('controller' => 'indexs', 'action' => 'index'));
Router::connect('/admin', array('admin'=>true ,'controller' => 'users', 'action' => 'login'));
我的UsersController:
var $name = 'Users';
var $uses = array('Header','SubHeader','User','UserGroup');
public $components = array('Session', 'Security', 'RequestHandler', 'Email', 'Cookie', 'Auth' =>
array('authorize' => array('Controller'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboards', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => "You can't access that page.",
'authenticate'=> array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'username','password' => 'password')
))
));
admin_login:
public function admin_login() {
$this->layout = "admin";
if($this->request->is('post')){
// echo $this->Auth->password($this->data['Users']['password']);
debug($this->request->data['Users']['password']);
debug($this->Auth->login());
if($this->Auth->login()){
//echo 'hkiiiii';
$name = $this->Auth->user('username');
$sid = $this->Auth->user('id');
$this->Session->write('b', $name);
$this->Session->write('sid', $sid);
$this->redirect($this->Auth->redirect());
}
else{
//echo 'hki';
$this->Session->setFlash(__("Your email/password combination was incorrect.", true), 'default', array('class'=>'gagal'));
$this->redirect($this->referer());
}
}
}
使用管理员前缀登录时遇到问题。
我正在使用 cakephp 2.4.6 。
debug($this->request->data['User']['password'])
正在返回正确的密码,但是
debug($this->Auth->login())
返回false。
答案 0 :(得分:0)
在app控制器的beforeFilter中编写代码:
if(isset($ this-> request-> params [' admin'])){
$this->Auth->userModel = 'User';
$this->Auth->authenticate = array('Form' => array('scope' =>array('User.status' => Configure::read('App.Status.active'),'User.role_id' => Configure::read('App.Admin.role'))));
$this->Auth->loginError = "Login failed. Invalid username or password";
$this->Auth->loginAction = array('admin' => true, 'controller' => 'admins', 'action' => 'login');
$this->Auth->loginRedirect = array('admin' => true, 'controller' => 'admins', 'action' => 'dashboard');
$this->Auth->authError = 'You must login to view this information.';
$this->Auth->autoRedirect = true;
$this->Auth->allow('admin_login');
}