Symfony ChainProvider FOSUser LDAPUser

I would like to use two UserProviders on my website. First, I would like to have the LDAPProvider tested and if the user doesn’t exists in the LDAP, the FOSUserProvider would be called to see if the user exists in the database. I red that I need to use chainProviders. Here’s what I’ve done so far.
The LDAP connection works fine. The security.yaml:

security:
providers:
    chain_provider:
        chain:
            providers: [fos_userbundle, intranet_ldap]
    from_database:
        entity:
            class: AppEntityUser
            property: username
    fos_userbundle:
        id: fos_user.user_provider.username #fos_user.user_manager
    intranet_ldap:
        #Test avec surcharge LdapUserProvider
        id: AppSecurityLdapUserProvider
firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:          true
            #switch_user: true
        main:
            pattern:            ^/
            context:            user
            anonymous:          true
            form_login_ldap:
                service:        SymfonyComponentLdapLdap
                provider:       intranet_ldap
                login_path:     /login
                check_path:     /login_check
                dn_string:      'DC=xxx-group, DC=lan'
                query_string:   '(&(sAMAccountName={username}))'
                use_forward:    false
                always_use_default_target_path: true
                default_target_path: /profile
                use_referer:    true
            logout:
                path:  /logout
                target: /

And in fos_user.yaml:

fos_user:
  db_driver: orm # other valid values are 'mongodb' and 'couchdb'
  firewall_name: main
  user_class: AppEntityUser

  group:
    group_class:   AppEntityGroup
    group_manager: sonata.user.orm.group_manager # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)
  service:
    user_manager: sonata.user.orm.user_manager

I doesn’t work. I guess I forgot something. The LDAP is still okay, but not the users from the database.

I was wondering if it was related to the form_login. The form_login from FOSUserBundle is used for the admin and the form_login_ldap for the main. How to use them both?
Also, I have a class LdapUser and a User class which extends SonataUserBundleEntityBaseUser, maybe it should extends FOSUserEntityBaseUser?

Thanks for your help

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *