您好我正在使用Auth组件进行身份验证(哪些页面可以访问哪些用户)。我使用MD5密码自己登录。
问题是,当我点击usrs /登录URL时,它仍然显示登录屏幕即使我已登录。应该做什么。我试过以下,但没有工作。
//In User controller
function beforeFilter()
{
//Here set Which pages should be accessable to various users
$adminPages =array('index','logout','changeProfPic','add','edit','delete','resetpwd','updatepwd');
$allUsersPages = array('login','forgot','resetpwd','updatepwd','index','logout');
$withoutLoginPages = array('login','forgot','resetpwd');
//Pages for owner only
if($this->Session->check('userID') && ($this->Session->read('role')== SUPER_ADMIN || $this->Session->read('role')== ADMIN))
{
$this->Auth->allow($adminPages);
}
else if($this->Session->check('userID') && ($this->Session->read('role')== STAFF || $this->Session->read('role')== USER))
{
$this->Auth->allow($allUsersPages);
}
else
{
$this->Auth->allow($withoutLoginPages);
}
//For all the users
}
//在同一控制器中登录功能。
function login($id=null)
{
//If form is posted
if(!empty($this->data))
{
//$hashedPassword=Security::hash($this->data['User']['password'],NULL,TRUE); //Hash password
$hashedPassword= md5($this->data['User']['password']); //Hash password
$conditionsLogIn = array(
'user_name' => trim($this->data['User']['username']),
'password' => $hashedPassword
);
$userDetails=$this->User->find('first',array('conditions'=>$conditionsLogIn));
if($userDetails)
{
$this->Session->write('fname',$userDetails['User']['first_name']);
$this->Session->write('lname',$userDetails['User']['last_name']);
$this->Session->write('role',$userDetails['User']['user_type_id']);
$this->Session->write('userID',$userDetails['User']['id']);
$this->Session->write('userType',$userDetails['UserType']['name']);
$this->redirect(array('controller'=>'users','action'=>'index'));
}
else
{
$this->Session->setFlash('Incorrect Username or Password.','default', array ('class' => 'msgflashError'),'invalidFlag1');
}
}
}
答案 0 :(得分:0)
我得到了解决方案:我评论了所有App-controller
。并在登录方法中添加了以下代码
function login($id=null)
{
if($this->Session->check('userID'))
{
$this->redirect(array('controller'=>'users','action'=>'index'));
}
.....其他代码