2012-03-18 59 views
0

我是cakephp的新手,並使用全新的2.1構建我的第一個項目。cakephp /不會在使用authComponent時在會話中存儲所有用戶數據

我使用身份驗證和會話組件來構建簡單的登錄系統。一切正常。 我使用的表單身份驗證,自定義請求的字段使用電子郵件作爲登錄。

當我嘗試

var_dump($this->Session->read('Auth.User')); 

我可以看到存儲在會話中的所有我的用戶數據,但我並不需要所有的人都在這裏。當然,我可以只用我需要的數據子集重寫會話,我的會話存儲在memecache中,而且我不能浪費內存。

我希望能夠輕鬆地更新cakephp,所以我提議不要重寫cakephp組件的部分。

我正在尋找一種解決方案,讓我在會議中指定女巫領域。 謝謝。

回答

4

您可以編寫自己的身份驗證組件是這樣的:

class MyAuthComponent extends AuthComponent { 

var $storeFields = array(); 

protected function cleanupUserData($user) { 
    if (empty($this->storeFields)) return $user; 

    retrun array_intersect_key($user, array_flip($this->storeFields)); 
} 

    public function login($user = null) { 
     $this->_setDefaults(); 

     if (empty($user)) { 
      $user = $this->identify($this->request, $this->response); 
     } 
     if ($user) { 
      $this->Session->renew(); 
     $user = $this->cleanupUserData($user); 
     $this->Session->write(self::$sessionKey, $user); 
     } 
     return $this->loggedIn(); 
    } 
} 

,然後寫在控制器是這樣的:

$this->MyAuth->storeFields = array('email', 'name', 'dob');