private UserEntity getUserEntity(Authentication authentication) {
    UserEntity userEntity = null;

    // Find user with the matching DN
    String dn = getUserDN(authentication);
    if (!StringUtils.isEmpty(dn)) {
      userEntity = getUserEntityForDN(dn);
    }

    // If a user was not found with the exact authentication properties (LDAP/dn), look up the user
    // using the configured LDAP username attribute and ensure that user has an empty-keyed LDAP
    // authentication entity record.
    if (userEntity == null) {
      String userName = AuthorizationHelper.resolveLoginAliasToUserName(authentication.getName());
      userEntity = getUsers().getUserEntity(userName);

      if (userEntity != null) {
        Collection<UserAuthenticationEntity> authenticationEntities = getAuthenticationEntities(userEntity, UserAuthenticationType.LDAP);
        UserEntity _userEntity = userEntity; // Hold on to the user entity value for now.
        userEntity = null;  // Guilty until proven innocent

        if (!CollectionUtils.isEmpty(authenticationEntities)) {
          for (UserAuthenticationEntity entity : authenticationEntities) {
            if (StringUtils.isEmpty(entity.getAuthenticationKey())) {
              // Proven innocent!
              userEntity = _userEntity;
              break;
            }
          }
        }
      }
    }

    return userEntity;
  }
Gets the {@link UserEntity} related to the authentication information
<p>
First the DN is retrieved from the user authentication information and a {@link UserAuthenticationEntity}
is queried for where the type value is LDAP and key value case-insensitively matches the DN.
If a record is found, the related {@link UserEntity} is returned.
<p>
Else, a {@link UserEntity} with the user name is queried. If one is found and it has a
{@link UserAuthenticationEntity} where the type value is LDAP and key is empty, the related
{@link UserEntity} is returned
<p>
Else, <code>null</code> is returned.

@param authentication the user's authentication data
@return a {@link UserEntity}
ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProvider.java