Home » Php » php – How to extend LdapUserProvider

php – How to extend LdapUserProvider

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m try to do the same of the question in post How to extend LdapUserProvider and use a custom LDAP user provider in Symfony?.
I reproduce de sames code but with no result.

This is my service.yml

    App\Security\CustomLDAPUserProvider:
    arguments:
        $ldap: ['@Symfony\Component\Ldap\Ldap']
        $baseDn: 'OU=****,DC=****,DC=****,DC=****'
        $searchDn: 'CN=****,OU=****,DC=***,DC=***,DC=***'
        $searchPassword: '*********'
        $filter: '({uid_key}={username})'
        $uidKey: 'samAccountName'
        $defaultRoles: ['ROLE_USER']
Symfony\Component\Ldap\Ldap:
    arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
    arguments:
        -   host: ******
            port: 389
            options:
                protocol_version: 3
                referrals: false

This is the security.yml

providers:
    app_users:
        id: App\Security\CustomLDAPUserProvider

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: ~

        form_login_ldap:
            # ...
            login_path: /login
            check_path: /login
            provider: app_users
            service: App\Security\CustomLDAPUserProvider
            dn_string: 'DC=****,DC=****,DC=***'
            query_string: '(&(sAMAccountName={username})(|(memberOf=CN=RSUsers,OU=***,DC=***,DC=***,DC=**)(memberOf=CN=Admins. del dominio,CN=Users,DC=***,DC=****,DC=***)))'
            search_dn: 'CN=****,OU=***,DC=***,DC=***,DC=**'
            search_password: '*******'

This is the provider.

class CustomLDAPUserProvider extends LdapUserProvider

{

/* EXTRA PARA LOS GRUPOS */
/** @var array maps ldap groups to roles */
private $groupMapping = [
    'ROLE_USER' => 'Usuarios del dominio',
    'ROLE_ADMIN' => 'Admins. del dominio',
    'ROLE_RSUsers' => 'RSUsers'
];

/** @var string extracts group name from dn string */
private $groupNameRegExp = '/CN=(.+?),/';

protected function loadUser($username, Entry $entry)
{
    $roles = ['ROLE_USER'];
    // Check if the entry has attribute with the group
    if (!$entry->hasAttribute('memberOf')) {
        return new User($username, '', $roles);
    }

    // Iterate through each group entry line
    foreach ($entry->getAttribute('memberOf') as $groupLine) {
        // Extract the group name from the line
        $groupName = $this->getGroupName($groupLine);
        // Check if the group is in the mapping
        if (array_key_exists($groupName, $this->groupMapping)) {
            // Map the group to the role(s) the user will have
            $roles[] = $this->groupMapping[$groupName];
        }
    }


    // Create and return the user object
    return new User($username, null, $roles);
}

/**
 * Get the group name from the DN
 * @param string $dn
 * @return string
 */
private function getGroupName($dn)
{
    $matches = [];
    return preg_match($this->groupNameRegExp, $dn, $matches) ? $matches[1] : '';
}

}

And this is the error.

TypeError
HTTP 500 Internal Server Error
Argument 1 passed to App\Security\CustomLDAPUserProvider::__construct() must implement interface Symfony\Component\Ldap\LdapInterface, array given, called in C:\xampp\htdocs\ultimo\var\cache\dev\ContainerHRnCi5I\App_KernelDevDebugContainer.php on line 1719

I already try any things but no result.
Can some body help me???

How to&Answers: