开发者

Zend_ACL how to get role?

开发者 https://www.devze.com 2023-01-29 03:53 出处:网络
after reading the Zend documentation and some posts here I could not figure out how to get my user role out of a user table.

after reading the Zend documentation and some posts here I could not figure out how to get my user role out of a user table.

At the moment I use Zend_Auth like this in an AuthController:

// Set authentication开发者_如何学编程 adapter and map ID and Cre.
// only admins could log in here
$adapter = new Zend_Auth_Adapter_DbTable($this->db,
            'customers',
            'login',
            'password',
            'MD5(?)');
$adapter->setIdentity($form->getValue('username'))
    ->setCredential($form->getValue('password'));

// Check if authentification is right
$result = Zend_Auth::getInstance()->authenticate($adapter);

if (!$result->isValid()) {
    ..
}

And later check it via an Zend_Controller_Plugin and route depending on the result:

if (Zend_Auth::getInstance()->hasIdentity()) {
        return;
} elseif ($request->getControllerName() == 'auth' || $request->getControllerName() == 'index') {
        return;
} else {
        $request->setControllerName('index');
        $request->setActionName('index');
        return;
}

Now I want to change the route depending on the roll of the user. If the user is an administrator he can reach the AdminController, but how do I get the role out of my user table? The column is called type and it contains a string witch indicates the role.

I hope you can help me.

Greetings,

-lony


Store your auth result row in Zend_Auth using the adapter's getResultRowObject method. See http://framework.zend.com/manual/en/zend.auth.adapter.dbtable.html#zend.auth.adapter.dbtable.advanced.storing_result_row


Thank you Phil, it works!

Only for complition my solution. I added this to the AuthController:

// fetches role and login name out of
// user table and store it in auth session
$data = $adapter->getResultRowObject(array(
                    'role',
                    'username'
                ));
Zend_Auth::getInstance()->getStorage()->write($data);

And now I can access my role (or username) everywhere by typing:

$role = Zend_Auth::getInstance()->getIdentity()->role;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号