[英]How to authorize user that tries to login
I am using Auth for the first time and everytime I try to login, using the correct credentials, the login fails and I get the action login failure error message. 我是第一次使用Auth,每次尝试使用正确的凭据登录时,登录都会失败,并且会收到操作登录失败错误消息。 How can I "mark" the user as an allowed user, to have access to the pages with prefix admin
? 如何将用户“标记”为允许用户访问具有前缀admin
的页面?
I am using CakePhp 2.4.4 我正在使用CakePhp 2.4.4
AppController AppController的
class AppController extends Controller {
public $components = array('DebugKit.Toolbar',
'Session','Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login', 'admin' => false),
'logoutAction' => array('controller' => 'users', 'action' => 'logout', 'admin' => false),
'loginRedirect'=> '/admin',
'logoutRedirect' => array('controller' => 'users', 'action' => 'login', 'admin' => false),
'authError' => 'Não tem permissão para aceder a esta área. Por favor faça login.',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username', 'password' => 'password'
),
'userModel' => 'User'
)
)//,
//'authorize' =>array('Controller'),
//'passwordHasher' => array(
// 'className' => 'Simple',
// 'hashType' => 'sha1'
//)
)
/*,'Auth' => array(
'loginRedirect' => array('controller' => 'admins', 'action' => 'admin_index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index'),
'authorize' => array('Controller')
)*/
);
public function beforeFilter(){
if($this->isPrefix('admin')){
if($this->isAuthorized('admin')){
if($this->Session->check('Auth.User.group_id')){
if($this->Session->read('Auth.User.group_id')==1){
//authorizes user to access pages with admin prefix
}else{
$this->Session->setFlash(__('Você não tem permissão para acessar essa URL'));
$this->Redirect('/login');
}
}else{
$this->Session->setFlash(__('Você não tem permissão para acessar essa URL'));
$this->Redirect('/login');
}
}else{
$this->Session->setFlash(__('Você não tem permissão para acessar essa URL'));
$this->Redirect('/login');
}
//$this->layout='admin';
}else{
$this->Auth->allow('index','ShowImages','ShowShowbill','ShowVideos','ShowContactUs','contact','login','DisplayMusic','DisplayEntertainment','DisplayPromotion','DisplayStaff','DisplayEquipments');
}
/*if($this->isPrefix('admin')){
if($this->Auth->loggedIn()){
if($this->Session->check('Auth.User.group_id')){
if($this->Session->read('Auth.User.group_id')==1){
}else{
$this->Session->setFlash(__('Você não tem permissão para acessar essa URL'));
$this->Redirect('/');
}
}
}
$this->layout='admin';
}else{
$this->Auth->allow('index','ShowImages','ShowShowbill','ShowVideos','ShowContactUs','contact','login','DisplayMusic','DisplayEntertainment','DisplayPromotion','DisplayStaff','DisplayEquipments');
}*/
//$this->Auth->allow('index','ShowImages','ShowShowbill','ShowVideos','ShowContactUs','login','ShowContactUs','timthumb');
//$this->Auth->allow('image');
}
public function isPrefix($prefix) {
return isset($this->request->params['prefix']) && $this->request->params['prefix'] == $prefix;
}
public function isAuthorized($user){
if(isset($user['role'])&&$user['role']==='admin'){
return true;
}
return false;
}
public $helpers = array('Html' ,
'Form' ,
'Timthumb.Timthumb',
'Paginator',
'Session',
'Js',
'Fancybox.Fancybox',
'Paginator',
);
} }
UsersController UsersController
public function login(){
if($this->request->is('post')){
//$user = array('User' => array('username'=> $this->request->data['User']['username'],'password'=> $this->request->data['User']['password']));
//debug(AuthComponent::user($this->data[$this->alias]['username']));
//debug(AuthComponent::password($this->data[$this->alias]['password']));
if($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
}
else{
$this->Session->setFlash(
__('O nome de utilizador ou a password estão incorrectos.'),
'default',array(),
'auth'
);
}
}
}
View 视图
<style>
#authMessage{
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
color: #b94a48;
background-color: #f2dede;
border-color: #eed3d7;
}
#flashMessage{
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
color: #b94a48;
background-color: #f2dede;
border-color: #eed3d7;
}
</style>
<h2>Login</h2>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Session->flash();?>
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo "<div class=\"alert alert-info\">Por favor insira um nome de Administrador e a password.</div>"; ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php
echo $this->Form->submit(__('Login'), array('class' => 'btn btn-success','formnovalidate' => true)) ;
echo $this->Form->end();
?>
</div>
Correct and working code has be: 正确且有效的代码为:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect'),'flash_error', array(), 'auth');
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.