如果用户已登录,则转到用户/索引。他点击了用户/登录网址

时间:2014-08-21 06:16:45

标签: php cakephp authorization cakephp-2.1 cakephp-2.3

您好我正在使用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');
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我得到了解决方案:我评论了所有App-controller。并在登录方法中添加了以下代码

function login($id=null)
{

    if($this->Session->check('userID')) 
    {
        $this->redirect(array('controller'=>'users','action'=>'index'));
    }

.....其他代码

相关问题