2011-06-15 182 views
0

我正在使用基於服務器的身份驗證,因此我試圖在Yii中實現自定義登錄系統。爲了學習這個系統,我嘗試創建一個虛擬身份驗證類,它會自動登錄一個用戶。我包括在配置類,但我不知道如何登錄用戶英寸Yii:無登錄頁面處理登錄

有沒有辦法自動登錄第一次使用的應用程序(例如作爲會話創建?)或有沒有更好的方法來實現這一目標?

此基礎上是一個自定義的驗證類:

class MyAuthentication 
    extends CApplicationComponent 
    implements IUserIdentity { 

    private $_username = ''; 
    private $_authenticated = False; 
    ... 
    public function authenticate() 
    { 
     $this->_username = 'exampleUser'; 
     $this->_authenticated = True; 
     return True; 
    } 

    public function getIsAuthenticated() 
    { 
     return $this->_authenticated; 
    } 

回答

1

由於我的身份驗證基於服務器端變量,因此我可以使用accessRules數組的expression功能來檢查該變量是否爲真。我寫了一個自定義類的幫助:

class User 
{ 

    public function is_authenticated() 
    { 
    return True; 
    } 

} 

和更新,我的規則來使用表達式檢查,而不是用戶的檢查,as described by the documentation

public function accessRules() 
{ 
    return array(
     array('allow', 
      'actions'=>array('index','view', 'new'), 
      'expression'=>'User::is_authenticated()', 
      ... 
1

好像你靠近。像這樣的東西應該是你的榜樣:

class UserIdentity extends CUserIdentity 
{ 
    private $_username = ''; 
    private $_authenticated = FALSE; 

    public function authenticate() 
    { 
     $this->_username = 'exampleUser'; 
     $this->_authenticated = TRUE; 
     return $this->_authenticated; 
    } 
    public function getIsAuthenticated() 
    { 
     return $this->_authenticated; 
    } 
} 
1

爲控制器創建一個過濾器,並檢查用戶是否已通過身份驗證。如果不是,則認證用戶。