Symfony 5.4 LDAP and User-Entity Password mixed

if ($user->getLDAPFlag()) {

    if ($conn = ldap_connect($_ENV['LDAP_HOST'])) {
        ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, $_ENV['LDAP_PROTOCOL_VERSION']);
        ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
        if ($_ENV['LDAP_CERT_CHECK'] == 0)
            ldap_set_option($conn, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
        $dn = $_ENV['LDAP_BIND_DN'];
        $pw = $_ENV['LDAP_BIND_PW'];
        if (ldap_bind($conn, $dn, $pw)) {
            // Search user
            $res = ldap_search($conn, $_ENV['LDAP_SEARCH_DN'], "(&(uid=" . $user->getUserName() . ")(objectClass=inetOrgPerson))", array('dn'));
            $entries = ldap_get_entries($conn, $res);
            if ($entries["count"] == 1)
                return ldap_bind($conn, $entries[0]['dn'], $credentials['password']);
            else if ($entries["count"] > 0)
                throw new CustomUserMessageAuthenticationException('Benutzer im LDAP nicht eindeutig!');
            else
                throw new CustomUserMessageAuthenticationException('Benutzer auf dem LDAP Server nicht gefunden!');
        } else
            // cannot bind
            throw new CustomUserMessageAuthenticationException('Kann nicht an LDAP-Server binden!');
        ldap_unind($conn);
    } else {
        // no LDAP Connection
        throw new CustomUserMessageAuthenticationException('Keine Verbindung zum LDAP-Server');
    }
} else
    // internal password-check
    return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
Topics: PHP Symfony

Code Example Quizzes

Related Code Examples