2014-11-24 57 views
0

我有一個網站在生產中運行正常,並且正在對代碼進行一些更改,因此我將其複製到localhost。該網站有一個用戶管理系統,需要登錄。用戶登錄後,它們將被重定向到mysite.com/admin/index。在Yii應用程序中獲取用戶ID的問題

用戶驗證正在發生,並在UserIdentity類中返回驗證類返回錯誤代碼爲0.但是,當用戶轉到WebUser類時,它會在檢查$ this-> id時返回空值。另外,Yi :: app() - > user-> id不存在。

以下是我的代碼的相關章節:

在main.php

'components'=>array(
     'user'=>array(
      // enable cookie-based authentication 
      // 'allowAutoLogin'=>true, 
      'class' => 'WebUser', 
      'loginUrl'=>array('site/login'), 
     ), 
     // uncomment the following to enable URLs in path-format 
     'urlManager'=>array(
      'urlFormat'=>'path', 
      'showScriptName'=>false, 
      'rules'=>array(
       '<controller:\w+>/<id:\d+>'=>'<controller>/view', 
       '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', 
       '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
      ), 
     ), 

在UserIdentity.php

private $_group; 
private $_id; 

public function getId(){ 
    return $this->_id; 
} 

public function authenticate($seeker = false, $queryid = false){ 
    //Retrieve seeker Login 

    $record=User::model()->findByAttributes(array('login_name'=>$this->username, 'status' => 0)); 

    //Invalid username 
    if($record===null){ 
    $this->errorCode=self::ERROR_USERNAME_INVALID; 
    } 
    //Invalid password 
    else if($record->password !== $record->encryptPassword($this->password) && $record->password !== $record->encryptPassword($this->password, true)){ 
    $this->errorCode=self::ERROR_PASSWORD_INVALID; 
    } 
    //Valid login 
    else{ 
    //Use new encryption 
    if($record->password === $record->encryptPassword($this->password, true)) 
     $record->resetPassword($this->password); 
    $this->_id  = $record->id; 
    $this->_group = $record->group; 
    $this->errorCode = self::ERROR_NONE; 
    } 
    if($this->errorCode == self::ERROR_NONE){ 
    $auth=Yii::app()->authManager; 
    try{ $role=$auth->createRole($this->_group); } catch(Exception $e){} 

    if(!$auth->isAssigned($this->_group,$this->_id)){ 
     if($auth->assign($this->_group,$this->_id)){ 
     Yii::app()->authManager->save(); 
     } 
    } 
    } 

    return !$this->errorCode; 
} 

在我SiteController.php,當我檢查了userid之後登錄時,它會返回正確的值,但在用戶重定向到admin/index後,我檢查用戶標識的值,但不顯示任何值。

有人可以建議我失蹤?下面

回答

1

代碼保存URL狀態,
要求admin/edit/3 - >重定向到login page - >重定向到admin/edit/3
要求login page - >重定向admin/index

$this->redirect(Yii::app()->getUser()->getReturnUrl('admin/index')); 
+0

不明白前調試這一點,或者乾脆把「dump'n'stop代碼」 :)

var_dump( Yii::app()->user->returnUrl == '/index.php', Yii::app()->user->returnUrl ); die(); 

你意思是。 – user1439090 2014-11-24 11:02:28

+0

你留在'site.com/admin/edit/3',你的會話已經過期,按'f5',成功登錄後你回到'site.com/admin/edit/3'而不是'admin/index ' – cetver 2014-11-24 11:07:16

+0

現在,當我在網站/登錄時,我看到登錄表單,並在驗證時將我重定向到admin/index。去網站/管理員/編輯/ 3只是給我一個404. – user1439090 2014-11-24 11:14:28

0

如果僅僅從你的代碼,並沒有其他事項作出決定時,它看起來像這個驗證

Yii::app()->user->returnUrl == '/index.php' 

總是錯誤的。你可以簡單地在這個品脫的調試器,停止你的這行代碼

$this->redirect(Yii::app()->user->returnUrl == '/index.php'? '/admin/index': Yii::app()->user->returnUrl); 
相關問題