@Test public void activeDirectory() { Settings settings = new Settings() .setProperty("ldap.user.baseDn", "cn=users") .setProperty("ldap.user.objectClass", "user") .setProperty("ldap.user.loginAttribute", "sAMAccountName"); LdapUserMapping userMapping = new LdapUserMapping(settings); LdapSearch search = userMapping.createSearch(null, "tester"); assertThat(search.getBaseDn(), equalTo("cn=users")); assertThat(search.getRequest(), equalTo("(&(objectClass=user)(sAMAccountName={0}))")); assertThat(search.getParameters(), equalTo(new String[] {"tester"})); assertThat(search.getReturningAttributes(), equalTo(null)); assertThat( userMapping.toString(), equalTo( "LdapUserMapping{" + "baseDn=cn=users," + " objectClass=user," + " loginAttribute=sAMAccountName," + " realNameAttribute=cn," + " emailAttribute=mail}")); }
@Test public void realm() { Settings settings = new Settings() .setProperty("ldap.realm", "example.org") .setProperty("ldap.userObjectClass", "user") .setProperty("ldap.loginAttribute", "sAMAccountName"); LdapUserMapping userMapping = new LdapUserMapping(settings); assertThat(userMapping.getBaseDn(), equalTo("dc=example,dc=org")); }
/** * @return details for specified user, or null if such user doesn't exist * @throws SonarException if unable to retrieve details */ public UserDetails doGetUserDetails(Context context) { // If there are no userMappings available, we can not retrieve user details. String username = context.getUsername(); LOG.debug("Requesting details for user {}", username); if (userMappings.isEmpty()) { String errorMessage = "Unable to retrieve user details: No user mappings found."; LOG.debug(errorMessage); throw new SonarException(errorMessage); } UserDetails details = null; SonarException sonarException = null; for (String serverKey : userMappings.keySet()) { SearchResult searchResult = null; try { LdapUserMapping ldapUserMapping = userMappings.get(serverKey); searchResult = ldapUserMapping .createSearch(contextFactories.get(serverKey), username) .returns( ldapUserMapping.getEmailAttribute(), ldapUserMapping.getRealNameAttribute()) .findUnique(); } catch (NamingException e) { // just in case if Sonar silently swallowed exception LOG.debug(e.getMessage(), e); sonarException = new SonarException( "Unable to retrieve details for user " + username + " in " + serverKey, e); } if (searchResult != null) { try { details = mapUserDetails(serverKey, searchResult); // if no exceptions occur, we found the user and mapped his details. break; } catch (NamingException e) { // just in case if Sonar silently swallowed exception LOG.debug(e.getMessage(), e); sonarException = new SonarException( "Unable to retrieve details for user " + username + " in " + serverKey, e); } } else { // user not found LOG.debug("User {} not found in " + serverKey, username); continue; } } if (details == null && sonarException != null) { // No user found and there is an exception so there is a reason the user could not be found. throw sonarException; } return details; }
@Test public void defaults() { LdapUserMapping userMapping = new LdapUserMapping(new Settings()); assertThat(userMapping.getBaseDn(), equalTo(null)); assertThat(userMapping.getObjectClass(), equalTo("inetOrgPerson")); assertThat(userMapping.getLoginAttribute(), equalTo("uid")); assertThat(userMapping.getRealNameAttribute(), equalTo("cn")); assertThat(userMapping.getEmailAttribute(), equalTo("mail")); assertThat( userMapping.toString(), equalTo( "LdapUserMapping{" + "baseDn=null," + " objectClass=inetOrgPerson," + " loginAttribute=uid," + " realNameAttribute=cn," + " emailAttribute=mail}")); }