2013-04-08 49 views
0

我做了一个简单的cakephp应用程序。目前我只是使用授权组件 来根据用户发送用户到他们各自的页面。例如,如果角色= 1发送到管理页面,否则如果角色= 2发送到主持人页面。我使用会话和身份验证组件来查看它们如何工作并将数据保存在其中。下面是UserController的login动作Cakephp Auth-> loginredirect问题

public function login(){ 

$this->Session->setFlash($this->Auth->user('role'));//checks for data in auth component if any 



    if($this->request->is('post')){ 


     $results = $this->User->findByEmail($this->request->data['User']['username']); 
     if($results &&$results['User']['password']== md5($this->request->data['User']['password'])) 
     { 
      $this->Session->write('user',$results['User']); 
      $this->Auth->login($results['User']); 
      $this->Session->setFlash('User logged in successfully'.$this->Auth->user('role')); 
      return $this->redirect($this->Auth->redirect()); 
     } 
     else 
     { 
      $this->Session->setFlash('Login is incorrect'); 
     } 
    } 


} 

问题的代码登录工精细的所有数据都存储在会话,并权威性变量,但loginredirect行为怪异。在我的Chrome浏览器中。无论角色是什么,它总是重定向到管理页面,但它闪烁着我在闪存中设置的正确信息。 beforefilter在AppController的

public function beforeFilter(){ 

    $this->Auth->allow('display'); 

    $this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login'); 

    $this->Auth->logoutRedirect = array('controller' => 'Users', 'action' => 'login'); 

    if($this->Auth->user('role') == '1'){ 
      $this->Session->setFlash($this->Auth->user('role').'adminnnnnnnnnnnnnnnnnnnnn'); 
    $this->Auth->loginRedirect = '/admins/index'; 
    } 

    if($this->Auth->user('role') == '2'){ 
     $this->Session->setFlash('moderatorrrrrrrrrrrrrrrrr'); 
     $this->Auth->loginRedirect = '/users/index'; 
    } 
} 

的代码,所以这个问题是在循环之前过滤器运行正常,在setflash显示用户是否是管理员或版主,但由于某种原因它重定向到只有一个页面要么管理员/索引页或用户/索引页面,无论谁登录。这是Chrome浏览器的行为。 在Firefox上,loginredirectcts将用户发送到webroot/index页面,但是再次显示flash消息是正确的。

我不确定我在做什么错在我的代码或cakephp 2.0验证组件中有一个问题验证组件有措施错误。

+0

暂时我使用loginredirect到一个称为仪表板的功能,负责路由 所以在用户登录后,它通过Auth-> loginRedirect重定向到仪表板(),在这里我检查用户角色并使用重定向将特定用户发送到确切位置。 我不确定是否正确的做法,但它的工作原理。任何人都可以告诉这个解决方案是否足够安全,因为我没有使用auth重定向? 由于 – 2013-04-08 07:27:00

+0

这是代码 功能的仪表板(){ \t // $角色= $这 - >会话级>读('用户。角色'); \t \t $ role = $ this-> Auth-> user('role'); \t \t \t这里 \t \t //用户选择逻辑如果($角色== '1'){ \t \t \t $这 - >重定向(阵列( '控制器'=> '管理员', '动作'=> 'index','admin'=> true)); \t \t \t \t \t } \t \t 其他\t如果($角色== '2'){ \t \t \t $这 - >重定向(阵列( '控制器'=> '用户', '动作'=> 'index','admin'=> true)); \t \t \t \t } \t \t 其他\t如果($角色== '9'){ \t \t \t $这 - >重定向(阵列( '控制器'=> '用户', '动作'=>' index','admin'=> true)); \t \t \t $ this-> Session-> setFlash('3'); \t \t} } – 2013-04-08 07:27:52

回答

1

用户登录时它就会通过Auth-> loginRedirect重定向到仪表板(),在这里我检查用户角色并使用重定向到特定的用户发送到确切位置

function dashboard() { 
    //get user's group (role) 
    //$role = $this->Session->read('user.role'); 
    $role=$this->Auth->user('role'); 
     //user selection logic here 
    if($role== '1'){ 
     $this->redirect(array('controller' => 'users','action' => 'admin_index','admin' => false)); 

    } 
    else if($role == '2'){ 
     $this->redirect(array('controller' => 'users','action' => 'admin_index', 'admin' => false)); 

    } 
    else if($role == '9'){ 
     $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => false)); 
     $this->Session->setFlash('3'); 
    } 

    } 

这之后就是另一种方式处理事情我包括在我的用户控制器的仪表板功能,并没有auth登录重定向到这个功能从appcontroller。 希望它解决了面临问题的其他人的问题。谢谢