Sankar Subburaj
Sankar Subburaj

Reputation: 5042

How to set auth variable in zend framework

We are developing a site using ZendFrame work.

We need to set a auth variable(role) to manage user-type.

For that we use a variable this->view->auth->getIdentity()->role to manage user-type.

In one chase we are trying to assign the user-type statically.

What is the code to assign the auth variable 'role'.

We tried with this->view->auth->getIdentity()->role = 'test', but it through warning.

Upvotes: 4

Views: 3335

Answers (1)

JF Dion
JF Dion

Reputation: 4054

The following should allow you to create a new Zend_Auth identity upon successful login :

$identity = Zend_Auth::getInstance()->getStorage();
$identity->write($userData);

As for the user data, I usually store the database row corresponding to the user with the required fields like Name, surname, email, role, to name a few. If you store a Zend_Db_TableRow in the Zend_Auth you will be able to access it like this :

Zend_Auth::getInstance()->getIdentity()->role;

Quick reminder on Zend_Auth:

$auth = Zend_Auth::getInstance(); // get the `Zend_Auth` instance (build on the singleton pattern)

$auth->hasIdentity(); // return true if an indentity exists and false if it doesn't

$auth->getIdentity(); // allow you to get the identity content (array, Zend_Db_TableRow and such)

$auth->write($data): // allow to create a new identity

$auth->clearIdentity(); // destroy any content stored in the identity. Zend_Session::destroy() can also be used since identity is stored in a session variable

For more you can read the official documentation : Zend_Auth

Upvotes: 8

Related Questions