使用Doctrine适配器时如何从Zend_Auth / Zend_ACL获取角色?让所有的工作在一起 [英] how to get role from Zend_Auth/Zend_ACL when using a Doctrine adapter? getting all work together

查看:174
本文介绍了使用Doctrine适配器时如何从Zend_Auth / Zend_ACL获取角色?让所有的工作在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的适配器看起来像这样:

我正在使用Zend_Auth与项目使用doctrine.I相信每个引导都是正确的,我可以登录。 p>

  class Abra_Auth_Adapter_Doctrine implements Zend_Auth_Adapter_Interface {

protected $ _resultArray;
private $ username;
私人$密码;

public function __construct($ username,$ password){

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

}

//基于反馈作为响应验证已更改为此
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_
} else {
返回新的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($请求);
$ controller = $ request-> getControllerName();
$ action = $ request-> getActionName();
$ module = $ request-> getModuleName();

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

}

现在有Doctrine_Event致命错误:spl_autoload ()[function.spl-autoload]:无法加载Doctrine_Event类。我看到这个在这里发布,我想知道如何可能会影响我使用Zend_Session,这是真的,我的app.dll启用了我的php.thanks很多阅读这个

解决方案

p>如何获取角色:在您的适配器中,成功登录后,而不是仅返回用户名字段,返回整个用户对象的方式如何?然后,当您调用 Zend_Auth :: getIdentity()



时,整个事情将可用问题1:如果您将控制器视为资源,并且ACL规则将在每个模块中不同,则资源名称也应反映模块。这将解决具有相同控制器名称的模块的问题。



问题2:我不知道我是否正确理解。 Zend_Auth及其存储将保留在自己的会话命名空间中保留用户身份。但是,我遇到了在数据库用户记录更改时该怎么办的问题 - 比如,用户在登录会话期间修改了他的个人资料中的全名,而且您在网站模板中显示了全名从 Zend_Auth :: getIdentity()中拉出。作为用户,我希望更改可以反映在可见界面中,但更改只发生在数据库中,而不是在会话中。



过去我所做的是创建一个额外的验证适配器来获取新的用户记录,并始终返回成功。当用户更新他的个人资料时,我使用这个平凡的适配器调用$ code> Zend_Auth :: authenticate()。会话存储更新,一切都与世界一致。



[这种方法几乎肯定是一个黑客,所以我有兴趣听取其他方法。我相信我可以直接在会话存储中设置一个值,但是当我上次尝试它时,我无法做到这一点。所以请求额外的适配器解决方法。]


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

解决方案

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.]

这篇关于使用Doctrine适配器时如何从Zend_Auth / Zend_ACL获取角色?让所有的工作在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆