dongliang1654
2019-03-15 14:20
浏览 105
已采纳

Symfony 3.4覆盖内置LdapUserProvider中的单个方法

I'm trying to grant roles to a user according to their LDAP dn when they log in through the LDAP. To do that, I'd like to override the loadUser method from Symfony\Component\Security\Core\User\LdapUserProvider, but I don't really know how to proceed as, if I understand correctly (I'm quite new at using Symfony :p), it's not a service, but part of one? So, is there a way to override that method easily, or do I need to redefine the whole Ldap service?

What I've tried is:

// app/config/services.yml
[...]
Symfony\Component\Ldap\Ldap:
    arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
Symfony\Component\Security\Core\User\LdapUserProvider:
    class: AppBundle\Services\LdapUserProvider
Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
    arguments:
        - host: '%ldap_host%'
          port: '%ldap_port%'
// src/Services/LdapUserProvider.php
namespace AppBundle\Services;

use Symfony\Component\Ldap\Entry;

class LdapUserProvider extends \Symfony\Component\Security\Core\User\LdapUserProvider {
    protected function loadUser($username, Entry $entry)
    {
        $password = null;

        if (null !== $this->passwordAttribute) {
            $password = $this->getAttributeValue($entry, $this->passwordAttribute);
        }

        return new User($username, $password, array('ROLE_TEST'));
    }
}

But of course it doesn't work and I don't get the ROLE_TEST role.

Thanks by advance!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试通过LDAP登录时根据用户的LDAP dn向用户授予角色。 为了做到这一点,我想从Symfony \ Component \ Security \ Core \ User \ LdapUserProvider覆盖loadUser方法,但我真的不知道如何继续,如果我理解正确(我很擅长使用 Symfony:p),它不是一个服务,而是一个服务的一部分? 那么,有没有办法轻松覆盖该方法,还是需要重新定义整个Ldap服务?

我试过的是:

  // app / config / services.yml 
 [...] 
Symfony \ Component \ Ldap \ Ldap:
 arguments:  ['@Symfony \ Component \ Ldap \ Adapter \ ExtLdap \ Adapter'] 
Symfony \ Component \ Security \ Core \ User \ LdapUserProvider:
 class:AppBundle \ Services \ LdapUserProvider 
Symfony \ Component \ Ldap \ Adapter \ ExtLdap \ Adapter  :
 arguments:
  -  host:'%ldap_host%'
 port:'%ldap_port%'
   
 
 
  // src / Services /  LdapUserProvider.php 
namespace AppBun  dle \ Services; 
 
use Symfony \ Component \ Ldap \ Entry; 
 
class LdapUserProvider extends \ Symfony \ Component \ Security \ Core \ User \ LdapUserProvider {
 protected function loadUser($ username,Entry $ entry)
  {
 $ password = null; 
 
 if(null!== $ this-> passwordAttribute){
 $ password = $ this-> getAttributeValue($ entry,$ this-> passwordAttribute); \  n} 
 
返回新用户($ username,$ password,array('ROLE_TEST')); 
} 
} 
 
   
 
 

但是 当然它不起作用,我没有得到ROLE_TEST角色。

感谢提前!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题