phpsymfonyldapphp-7.3

Custom UserProvider config symfony 4


I would like to create my own LdapUserProvider to apply different role regarding the username. Accutally my LDAP works really good with the default LdapUserProvider from symfony.

As the LDAP doc say :

The ldap user provider, using the LdapUserProvider class. Like all other user providers, it can be used with any authentication provider.

How can I do to use a customized User provider please?

security:
        providers:
            my_ldap:
                ldap:
                    service: Symfony\Component\Ldap\Ldap
                    base_dn: o=xxx
                    search_dn: cn=xxx Downloader,ou=ApplicationUsers,o=xxx
                    search_password: 'xxx'
                    default_roles: [ROLE_USER]
                    uid_key: uid
                    filter: "{uid_key}={username}"
    
        firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
            main:
                anonymous: true
    #            provider: App\Services\MyLdapUserProvider
                logout:
                    csrf_token_id: logout
                    path: /logout
                    target: /login
                form_login_ldap:
                    csrf_parameter: _csrf_token
                    login_path: login
                    check_path: login
                    service: Symfony\Component\Ldap\Ldap
                    dn_string: 'o=xxx'
                    query_string: 'uid={username}'
                    target_path_parameter: home
                    default_target_path:  /home
    
        access_control:
            - { path: /index, role: IS_AUTHENTICATED_FULLY }
            - { path: ^/admin, roles: ROLE_ADMIN }

In security.yaml this is how I call the service:

security:
        providers:
            myLdap:
                id: App\Services\MyLdapUserProvider

and this is my service.yaml:

services:
        App\Services\MyLdapUserProvider:
            arguments:
                $ldap: '@Symfony\Component\Ldap\LdapInterface'
                $baseDn: 'o=xxx'
                $searchDn: 'cn=xxx Downloader,ou=ApplicationUsers,o=xxx'
                $searchPassword: 'xxx'
                $defaultRoles:
                    'ROLE_USER'
                $uidKey: 'uid'
                $filter: '{uid_key}={username}'
    
        Symfony\Component\Ldap\LdapInterface:
            arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
    
        Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
            arguments:
                - host: ....

I don't uderstand why baseDn is not defined...

Cannot autowire service "App\Services\MyLdapUserProvider": argument "$baseDn" of method "Symfony\Component\Security\Core\User\LdapUserProvider::__construct()" is type-hinted "string", you should configure its value explicitly.


Solution

  • Look at this Symfony Documentation.

    You need to adjust your configuration:

    security:
        providers:
            enter_you_custom_name_here:
                id: AppBundle\Security\User\UserProvider
    

    And create the UserProvider: