Instead of writing $identity
to $authStorage
you can save a custom object or model.
Here is an example:
<?php class Application_Model_UserSession implements Zend_Acl_Role_Interface { public $userId; public $username; protected $_data; public function __construct($userId, $username) { $this->userId = $userId; $this->username = $username; } public function __set($name, $value) { $this->_data[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->_data)) { return $this->_data[$name]; } else { return null; } } public function updateStorage() { $auth = Zend_Auth::getInstance(); $auth->getStorage()->write($this); } public function getRoleId() {
Now in your login controller you can do:
if($result->isValid()){ $identity = new Application_Model_UserSession(0, $username);
Typically, I have an account object, which I also store in the UserSession object, and provides easy access to the username and userId through public properties.
Now you can get an object at any time:
$identity = Zend_Auth::getInstance()->getIdentity();
Just remember to make sure this is Application_Model_UserSession.
source share