How to set auth variable in zend framework

We are developing a site using the work of ZendFrame.

We need to set the auth variable (role) to control the user type.

To do this, we use the variable this->view->auth->getIdentity()->role to control the user type.

In one pursuit, we are trying to assign a user type statically.

What is the code for assigning a role to an auth variable.

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

+4
source share
1 answer

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

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

As far as user data is concerned, I usually store the database row corresponding to the user, with the necessary fields, such as First name, Last name, Email address, Role, to name a few. If you store Zend_Db_TableRow in Zend_Auth , you can access it as follows:

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

A quick reminder of 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 

You can read the official documentation for more details: Zend_Auth

+8
source

Source: https://habr.com/ru/post/1382265/


All Articles