In Zend_Auth, can I get a domain-model User object instead of stdClass?

喜欢而已 提交于 2019-12-25 04:35:10

问题


While working on the login part of an application, I find myself running into an issue:

My model is based on Active Record, by extending Zend_Db_Table_Row objects. Whenever I have to deal with a user, I'd want to do this through the User object (being an extended table row). However, Zend_Auth_Adapter_DbTable::getResultRowObject() returns a stdClass and I can not find a way to tell Zend_Auth_Adapter_DbTable to use a specific Db_Table.

My three options that I've found so far:

  1. Write a custom Zend_Auth_Adapter, to return the proper class object (using Zend_Db_Table internally).

  2. After authentication perform some reading of the stdClass and some writing of the User object - can you create a Zend_Db_Table_Row class without actually using the Zend_Db_Table to create it?

  3. Move away from Active Record into complete Domain Driven development, separating my data access away from the user object. Then write a second mapper to fill the User object with stdClass instead of the table row.

My two questions: Which of these would you recommend? And is there something I am missing?

I'd hate to do any of these and then find out I'm missing something obvious in the Framework. Does anyone know if there is a 'proper' way to be doing this?

Regards, Sander


回答1:


There is no "proper" way to do what you want. The approaches you show should all work.

Personally I'm in favor of storing just an ID for the user in Zend_Auth - if you were to store a full object with all user details, it would fall out of sync if the user was to change their info.

In a project I once wrote a controller plugin which provided the user to the controllers. It basically took the ID from Zend_Auth and built a user object based on it. It also created an anonymous user object in case there was no ID. Not saying this is the best approach, but it's transparent and worked.




回答2:


In particullar, I use solution number 2, I have a custom Auth object that wraps Zend_Auth, so on login, I have this:

public function login($user, $password) {
    if(!($this->adapter instanceof Zend_Auth_Adapter_DbTable)) {
        throw new Gecko_Auth_Exception("The Adapter is not valid or not initialized");
    }

    $auth = Zend_Auth::getInstance();

    $this->adapter->setIdentity($user);
    $this->adapter->setCredential($password);

    $result = $auth->authenticate($this->adapter);
    if( $result->isValid() ) {
        $data = $this->adapter->getResultRowObject(null, $this->passwordColumn);
        $userObject = new self::$userClass($data);
        $auth->getStorage()->write($userObject);
        return true;
    } else {
        return false;
    }
}

Then I can call Zend_Auth::getInstance to retreive the custom UserClass object so I can have it mapped yo my custom Zend_Db_Table_Row class.

Hope it helps



来源:https://stackoverflow.com/questions/1272683/in-zend-auth-can-i-get-a-domain-model-user-object-instead-of-stdclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!