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;
精彩评论