Zend framework Zend_ACL如何获得角色?

Zend framework Zend_ACL如何获得角色?,zend-framework,zend-auth,zend-acl,Zend Framework,Zend Auth,Zend Acl,在阅读了Zend文档和这里的一些帖子之后,我不知道如何从用户表中获取我的用户角色 目前,我在AuthController中使用Zend_Auth,如下所示: // Set authentication adapter and map ID and Cre. // only admins could log in here $adapter = new Zend_Auth_Adapter_DbTable($this->db, 'customers',

在阅读了Zend文档和这里的一些帖子之后,我不知道如何从用户表中获取我的用户角色

目前,我在AuthController中使用Zend_Auth,如下所示:

// 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()) {
    ..
}
// 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);
然后通过Zend_Controller_插件进行检查,并根据结果进行路由:

if (Zend_Auth::getInstance()->hasIdentity()) {
        return;
} elseif ($request->getControllerName() == 'auth' || $request->getControllerName() == 'index') {
        return;
} else {
        $request->setControllerName('index');
        $request->setActionName('index');
        return;
}
现在我想根据用户的滚动来改变路线。如果用户是管理员,他可以访问AdminController,但是如何从用户表中获取角色?该列称为type,它包含一个表示角色的字符串

我希望你能帮助我

您好


-lony

使用适配器的
getResultRowObject
方法将您的验证结果行存储在Zend_auth中。请参见

谢谢你,菲尔,它起作用了

只是为了完成我的解决方案。我将此添加到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()) {
    ..
}
// 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);
现在,我可以通过键入以下内容随时随地访问我的角色(或用户名):

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