Zend_ACL如何获取角色?

发布于 2024-10-06 05:37:38 字数 1123 浏览 7 评论 0原文

阅读 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()) {
    ..
}

然后通过 Zend_Controller_Plugin 检查它并根据结果进行路由:

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

现在我想根据用户的滚动来更改路由。如果用户是管理员,他可以访问 AdminController,但如何从用户表中获取角色?该列称为类型,它包含一个指示角色的字符串。

我希望你能帮助我。

问候,

-洛尼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-10-13 05:37:38

使用适配器的 getResultRowObject 方法将您的身份验证结果行存储在 Zend_Auth 中。请参阅 http: //framework.zend.com/manual/en/zend.auth.adapter.dbtable.html#zend.auth.adapter.dbtable.advanced.storing_result_row

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

北斗星光 2024-10-13 05:37:38

谢谢菲尔,它有效!

仅用于完成我的解决方案。我将其添加到 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);

现在我可以通过键入以下内容在任何地方访问我的角色(或用户名):

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文