Symfony 5: ldap authentication with custom user entity

I want to implement the following authentication scenario in symfony 5:

  • User sends a login form with username and password, authentication is processed against an LDAP server
    • if authentication against the LDAP server is successful :
      • if there is an instance of my AppEntityUser that as the same username as the ldap matching entry, refresh some of its attributes from the ldap server and return this entity
      • if there is no instance create a new instance of my AppEntityUser and return it

I have implemented a guard authenticator which authenticates well against the LDAP server but it’s returning me an instance of SymfonyComponentLdapSecurityLdapUser and I don’t know how to use this object to make relation with others entities!

For instance, let’s say I have a Car entity with an owner property that must be a reference to an user.

How can I manage that ?

Here is the code of my security.yaml file:

security:
    encoders:
        AppEntityUser:
            algorithm: auto

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: AppEntityUser
                property: email
        my_ldap:
            ldap:
                service: SymfonyComponentLdapLdap
                base_dn: "%env(LDAP_BASE_DN)%"
                search_dn: "%env(LDAP_SEARCH_DN)%"
                search_password: "%env(LDAP_SEARCH_PASSWORD)%"
                default_roles: ROLE_USER
                uid_key: uid
                extra_fields: ['mail']
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            lazy: true
            provider: my_ldap
            guard:
                authenticators:
                    - AppSecurityLdapFormAuthenticator

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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