Search code examples
authenticationsymfonyldapsymfony6

Symfony 6 - LDAP login with entity update


I'm trying to build a login via form against an LDAP, fetch the user from my Entity via Doctrine, and want to be able to update the user on successful login or create one if no one exists in my database. Further, I want to create one if no user exists but the LDAP login was successful or authenticate against the db if the LDAP is not reachable.

I read the documentation over and over again, but I don't get a clue of how to combine those requirements.

I managed to get the LDAP authentication running, but in my controller, I only get an instance of Symfony\Component\Ldap\Security\LdapUser and no UserEntity, and I'm not able to hook into the login process so I could handle the login myself. I've tried a custom user provider, but I can't find a method like "afterSuccessfulLogin" where I could update my entity. Further, this wouldn't help my login against the Database in the second case.

What would be the correct "Symfonyway" to do so?

Thanks in advance.

EDIT:

I think it's the customuserprovider I have to use, but I don't know how to accomplish my goal with it. I'm currently not using it in the main firewall because I don't know how to edit the customuserprovider. is this even the right approach? I changed the dn credentials, im not trying against the example ldap.

security:
  # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
  password_hashers:
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
  # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
  providers:
    main_user_provider:
      entity:
        class: App\Entity\User
        property: email
    authority_user_provider:
      entity:
        class: App\Entity\Authority
        property: email
    company_ldap:
      ldap:
        service: Symfony\Component\Ldap\Ldap
        base_dn: dc=example,dc=com
        search_dn: "CN=admin,CN=Users,DC=example,DC=com"
        search_password:########
        default_roles: ROLE_USER
    custom:
      id: App\Security\CustomUserProvider
  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    authorityPortal:
      pattern: ^/authority/
      lazy: true
      provider: authority_user_provider
      form_login:
        login_path: app_authority_login
        check_path: app_authority_login
        default_target_path: app_authority_portal
        enable_csrf: true
      logout:
        path: app_logout
      custom_authenticator: App\Security\AuthorityAuthenticator
    main:
      lazy: true
      provider: company_ldap
      form_login_ldap:
        service: Symfony\Component\Ldap\Ldap
        dn_string: "CN=Users,DC=example,DC=com"
        query_string: '(sAMAccountName={user_identifier})'
        search_dn: "CN=admin,CN=Users,DC=example,DC=com"
        search_password: "#######"
        login_path: app_login
        check_path: app_login
        default_target_path: app_workbench_index
        #enable_csrf: true
      logout:
        path: app_logout


Solution

  • I got it running thanks to this post with a similar problem as mentioned by Leroy in the comments:

    Symfony LDAP with custom User Entity and auto creation of DB user