1
votes

why i cant get user as object ?

i use code to get user identity :

$identity = Zend_Auth::getInstance()->getIdentity()

echo $identity->usergroup

return Notice: Trying to get property of non-object

or

  $identity = Zend_Auth::getInstance()->getStorage()->read();
        var_dump($identity);

give string '[email protected]' (length=9)

so how to get usergroup

login :

$request = $this->getRequest();

        if ($request->isPost() and $loginForm->isValid($_POST)) {

            $adapter = new Zend_Auth_Adapter_DbTable(
                $db,
                'users',
                'useremail',
                'password'
            );

            $adapter->setIdentity($loginForm->getValue('useremail'));
            $adapter->setCredential($loginForm->getValue('password'));

            $auth   = Zend_Auth::getInstance();
            $result = $auth->authenticate($adapter);

            if ($result->isValid()) {

                $this->_helper->FlashMessenger('Successful Login');
                $this->_redirect('/');
                return;
            }else{
                throw new Exception($result->getMessages()[0]);
            }

user model :

protected $_id;
protected $_useremail;
protected $_usergroup;
protected $_password;
protected $_password_salt;
protected $_realname; 
3

3 Answers

2
votes

you can use,

$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);


if ($result->isValid()) {
            $user = $adapter->getResultRowObject();
            $auth->getStorage()->write($user);
            return true;
        }

if usergroup is in the row that authenticates user, you can use this,

 $auth = Zend_Auth::getInstance();
 $usergroup = $auth->getIdentity()->usergroup;

or another field for that matter like,

$password = $auth->getIdentity()->password;

Or

$email = $auth->getIdentity()->email;

Use this article by rob allen to understand more, here

0
votes

That is the normal behavior, in fact the getIdentity method returns the identity of the user, that is, the username.

0
votes

Please try to Auth session data

$identity = Zend_Auth::getInstance()->getStorage()->read();
var_dump($identity)

Zend_Auth::getInstance()->getIdentity() is return $username name only