使用 Doctrine 适配器时如何从 Zend_Auth/Zend_ACL 获取角色?将所有工作放在一起

发布于 2024-10-12 00:19:32 字数 2019 浏览 3 评论 0原文

我正在使用 Zend_Auth 进行一个使用 Doctrine_Event 的项目。我相信每个引导都正确完成并且我可以登录。

我的适配器如下所示:

class Abra_Auth_Adapter_Doctrine implements Zend_Auth_Adapter_Interface {

protected $_resultArray;
private $username;
private $password;

public function  __construct($username, $password) {

    $this->username = $username;
    $this->password = $password;

}

//based on feedbacks as response authenticate has changed to this
public function  authenticate() {
    $q = Doctrine_Query::create()
    ->from("Abra_Model_User u")
    ->leftJoin("u.Role r")
    ->where("u.username=? AND u.password=?", array($this->username,$this->password));
    $result = $q->execute();
    if (count($result) == 1) {
        return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $result->get("Mylibrary_Model_User"), array());//autoloaderNamespaces[] = "Mylibrary_" in application.ini
    } else {
        return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array("Authentication Unsuccessful"));
    }
}

我的 Abra_Controller_Pluging_Acl 现在看起来像这样

class Abra_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

public function preDispatch(Zend_Controller_Request_Abstract $request) {
    parent::preDispatch($request);
    $controller = $request->getControllerName();
    $action = $request->getActionName();
    $module = $request->getModuleName();

    $auth = Zend_Auth::getInstance();
    if($auth->hasIdentity()){
        $identity = $auth->getIdentity();
        $roles = $identity["Role"];
        $role = $roles["name"];
        $role = (empty ($role) || is_null($role))? "regular" : $role ;
    } else {
        $role = "guest";
    }

 }

有 Doctrine_Event 致命错误:spl_autoload() [function.spl-autoload ]: 无法加载 Doctrine_Event 类。我看过这里的帖子,我想知道这是怎么回事会影响我对 Zend_Session 的使用,而且我确实在我的 php 中启用了 apc.dll。非常感谢阅读本文

I'm using Zend_Auth with a project using doctrine.I believe every bootstrapping is done correctly and i can log in.

my adapter looks like this:

class Abra_Auth_Adapter_Doctrine implements Zend_Auth_Adapter_Interface {

protected $_resultArray;
private $username;
private $password;

public function  __construct($username, $password) {

    $this->username = $username;
    $this->password = $password;

}

//based on feedbacks as response authenticate has changed to this
public function  authenticate() {
    $q = Doctrine_Query::create()
    ->from("Abra_Model_User u")
    ->leftJoin("u.Role r")
    ->where("u.username=? AND u.password=?", array($this->username,$this->password));
    $result = $q->execute();
    if (count($result) == 1) {
        return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $result->get("Mylibrary_Model_User"), array());//autoloaderNamespaces[] = "Mylibrary_" in application.ini
    } else {
        return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array("Authentication Unsuccessful"));
    }
}

my Abra_Controller_Pluging_Acl looks like this

class Abra_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

public function preDispatch(Zend_Controller_Request_Abstract $request) {
    parent::preDispatch($request);
    $controller = $request->getControllerName();
    $action = $request->getActionName();
    $module = $request->getModuleName();

    $auth = Zend_Auth::getInstance();
    if($auth->hasIdentity()){
        $identity = $auth->getIdentity();
        $roles = $identity["Role"];
        $role = $roles["name"];
        $role = (empty ($role) || is_null($role))? "regular" : $role ;
    } else {
        $role = "guest";
    }

 }

now having Doctrine_Event Fatal error: spl_autoload() [function.spl-autoload]: Class Doctrine_Event could not be loaded. i've seen this post here and i'm wondering how that can affect my using of Zend_Session, and it's true that i have apc.dll enabled in my php.thanks a lot for reading this

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

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

发布评论

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

评论(1

清欢 2024-10-19 00:19:32

如何获取角色:在您的适配器中,成功登录后,不只返回用户名字段,而是返回整个用户对象怎么样?然后,当您调用 Zend_Auth::getIdentity() 时,整个事情就可用了。

问题 1:如果您将控制器视为资源,并且每个模块的 ACL 规则都不同,那么资源名称也应该反映该模块。这将解决具有相同控制器名称的模块的问题。

问题2:我不确定我的理解是否正确。 Zend_Auth 及其存储将负责将用户身份保存在其自己的会话名称空间中。但是,我遇到了当数据库中的用户记录发生更改时该怎么办的问题 - 例如,用户在登录会话期间修改其个人资料中的全名 - 并且您在站点模板中显示该全名,从 Zend_Auth::getIdentity() 中提取。作为用户,我希望更改能够反映在可见界面中,但更改仅发生在数据库中,而不是在会话中。

我过去所做的是创建一个额外的身份验证适配器来获取新的用户记录并始终返回成功。当用户更新他的个人资料时,我使用这个简单的适配器调用 Zend_Auth::authenticate()。会话存储已更新,一切都很好。

[这种方法几乎肯定是一种黑客行为,所以我有兴趣听到替代方法。我确信我可以直接在会话存储中设置一个值,但是当我上次尝试它时,我无法让它正常工作。因此采取了额外的适配器解决方法。]

How to get the role: In your adapter, on successful login, rather than returning only the username field, how about returning the whole user object? Then the whole thing will be available when you call Zend_Auth::getIdentity().

Question 1: If you treat controllers as resources and the ACL rules are going to be different per module, then the resource names should reflect the module, as well. This will address the issue of modules with identical controller names.

Question 2: I am not sure I am understanding correctly. Zend_Auth and its storage will take care of keeping the uer identity in its own session namespace. However, I have run into the issue of what to do when the user record in the db changes - say, the user modifies his full name in his profile during his logged-in session - and you are displaying that full name in your site template, pulled from Zend_Auth::getIdentity(). As a user, I would expect the change to be reflected in the visible interface, but the change has only occurred back in the db, not in the session.

What I have done in the past is to create an additional auth adapter that fetches the new user record and always returns success. When the user updates his profile, I call Zend_Auth::authenticate() using this trivial adapter. The session storage gets updated and all is well with the world.

[This approach is almost certainly a hack, so I'd be interested in hearing alternative approaches. I'm sure I can set a value in the session storage directly, but when I last tried it, I couldn't make it quite work. So resorted to the additional adapter workaround.]

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