Zend_Auth :: getInstance() - > getIdentity()返回字符串

时间:2014-01-12 07:30:25

标签: php zend-framework

为什么我不能将用户作为对象?

我使用代码获取用户身份:

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

echo $identity->usergroup

返回注意:尝试获取非对象的属性

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

给出字符串'test@g.pl'(长度= 9)

所以如何获得用户组

登录:

$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]);
            }

用户模型:

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

3 个答案:

答案 0 :(得分:2)

你可以使用,

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


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

如果usergroup位于对用户进行身份验证的行中, 你可以用这个,

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

或其他相关领域,如

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

或者

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

使用rob allen撰写的这篇文章了解更多内容,here

答案 1 :(得分:0)

这是正常行为,实际上getIdentity方法返回用户的身份,即用户名。

答案 2 :(得分:0)

请尝试验证会话数据

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

Zend_Auth::getInstance()->getIdentity()仅返回$ username name