logo       

Re: Using Zend_Auth to retrieve a user's role to use in Zend_Acl: msg#00018

php.zend.framework.auth

Subject: Re: Using Zend_Auth to retrieve a user's role to use in Zend_Acl

Subclassing the adapter isn't necessary. You should be able to do this instead:

if (Zend_Auth::getInstance()->hasIdentity()) {
$role = Zend_Auth::getInstance()->getIdentity()->role;
}

See these two tutorials on auth & acls respectively. I found them far more helpful than the one on the zend devzone.



Cheers,
ncw

On 3-Oct-07, at 1:11 PM, Vincent wrote:



On 03/10/2007, David Mintz <david-dMffkYQFJ3MKxoK4Ny/0iQ@xxxxxxxxxxxxxxxx> wrote:
That probably isn't necessary. Here is my authenticate() from the helper I wrote. Suppose "role" is a column from your user table. If you do something like this, you will have $user->role available to you. (This is substantially stolen from the docs). HTH.

    function authenticate($username,$password) {
       
        $db = Zend_Registry::get('db');
       $view = $this->getActionController()->view;
       // I get some config stuff from an ini file and then...
        $authAdapter = new Zend_Auth_Adapter_DbTable($db, $config->tablename, $config->usercol, 'password');
       
        $result = $authAdapter->setIdentity($username)
                ->setCredential($password)
                ->authenticate();
       
        $this->result = $result;

        if ($result->isValid()) {
            $auth = Zend_Auth::getInstance();
            $user = $authAdapter->getResultRowObject(null,'password');
            $auth->getStorage()->write($user);
            $view->user = $user; // so we can easily say "welcome Joe User"
            Zend_Session::regenerateId();
            return true;
           
        } else {
            // authentication failed. let the caller deal with it by calling our getResult();
            return false;
        }
    }

David, that did help a lot, and I think this will help me solve this, so thanks. I can now add a method to my adapter that will return the user's data in an array, and then when logging in, do an $auth->getStorage()->write($role); . This will probably solve it, but I was wondering whether there was a more elegant way, i.e. to automatically make AuthAdapter store this when I call $auth->authenticate($authAdapter);

Again, thanks a lot, I was really getting desperate!

On 10/3/07, Vincent <imnotb-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx> wrote:


After having read some more, I am now thinking of subclassing Zend_Auth_Result to make it include the user's role. I'm not quite sure whether this would be the best way to go about though, so I'd like to ask for some feedback on whether you guys think this to be a good idea.

--
David Mintz
http://davidmintz.org/

The subtle source is clear and bright
The tributary streams flow through the darkness



--
Vincent

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise