LdapGroupsProvider extends ExternalGroupsProvider { public Collection<String> getGroups(String username) { checkPrerequisites(username); Set<String> groups = new HashSet<>(); List<LdapException> exceptions = new ArrayList<>(); for (String serverKey : userMappings.keySet()) { if (!groupMappings.containsKey(serverKey)) { continue; } SearchResult searchResult = searchUserGroups(username, exceptions, serverKey); if (searchResult != null) { try { NamingEnumeration<SearchResult> result = groupMappings .get(serverKey) .createSearch(contextFactories.get(serverKey), searchResult).find(); groups.addAll(mapGroups(serverKey, result)); break; } catch (NamingException e) { LOG.debug(e.getMessage(), e); exceptions.add(new LdapException(format("Unable to retrieve groups for user %s in %s", username, serverKey), e)); } } else { continue; } } checkResults(groups, exceptions); return groups; } LdapGroupsProvider(Map<String, LdapContextFactory> contextFactories, Map<String, LdapUserMapping> userMappings, Map<String, LdapGroupMapping> groupMapping); @Override Collection<String> doGetGroups(Context context); Collection<String> getGroups(String username);  }
@Test public void defaults() throws Exception { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, null); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapGroupsProvider groupsProvider = new LdapGroupsProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings(), settingsManager.getGroupMappings()); Collection<String> groups; groups = groupsProvider.getGroups("tester"); assertThat(groups).containsOnly("sonar-users"); groups = groupsProvider.getGroups("godin"); assertThat(groups).containsOnly("sonar-users", "sonar-developers"); groups = groupsProvider.getGroups("notfound"); assertThat(groups).isEmpty(); }
@Test public void defaultsMultipleLdap() throws Exception { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, infosupportServer); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapGroupsProvider groupsProvider = new LdapGroupsProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings(), settingsManager.getGroupMappings()); Collection<String> groups; groups = groupsProvider.getGroups("tester"); assertThat(groups).containsOnly("sonar-users"); groups = groupsProvider.getGroups("godin"); assertThat(groups).containsOnly("sonar-users", "sonar-developers"); groups = groupsProvider.getGroups("notfound"); assertThat(groups).isEmpty(); groups = groupsProvider.getGroups("testerInfo"); assertThat(groups).containsOnly("sonar-users"); groups = groupsProvider.getGroups("robby"); assertThat(groups).containsOnly("sonar-users", "sonar-developers"); }
@Test public void posix() { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, null); settings.setProperty("ldap.group.request", "(&(objectClass=posixGroup)(memberUid={uid}))"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapGroupsProvider groupsProvider = new LdapGroupsProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings(), settingsManager.getGroupMappings()); Collection<String> groups; groups = groupsProvider.getGroups("godin"); assertThat(groups).containsOnly("linux-users"); }
@Test public void posixMultipleLdap() { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, infosupportServer); settings.setProperty("ldap.example.group.request", "(&(objectClass=posixGroup)(memberUid={uid}))"); settings.setProperty("ldap.infosupport.group.request", "(&(objectClass=posixGroup)(memberUid={uid}))"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapGroupsProvider groupsProvider = new LdapGroupsProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings(), settingsManager.getGroupMappings()); Collection<String> groups; groups = groupsProvider.getGroups("godin"); assertThat(groups).containsOnly("linux-users"); groups = groupsProvider.getGroups("robby"); assertThat(groups).containsOnly("linux-users"); }
@Test public void mixed() { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, infosupportServer); settings.setProperty("ldap.example.group.request", "(&(|(objectClass=groupOfUniqueNames)(objectClass=posixGroup))(|(uniqueMember={dn})(memberUid={uid})))"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapGroupsProvider groupsProvider = new LdapGroupsProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings(), settingsManager.getGroupMappings()); Collection<String> groups; groups = groupsProvider.getGroups("godin"); assertThat(groups).containsOnly("sonar-users", "sonar-developers", "linux-users"); }
@Test public void mixedMultipleLdap() { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, infosupportServer); settings.setProperty("ldap.example.group.request", "(&(|(objectClass=groupOfUniqueNames)(objectClass=posixGroup))(|(uniqueMember={dn})(memberUid={uid})))"); settings.setProperty("ldap.infosupport.group.request", "(&(|(objectClass=groupOfUniqueNames)(objectClass=posixGroup))(|(uniqueMember={dn})(memberUid={uid})))"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapGroupsProvider groupsProvider = new LdapGroupsProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings(), settingsManager.getGroupMappings()); Collection<String> groups; groups = groupsProvider.getGroups("godin"); assertThat(groups).containsOnly("sonar-users", "sonar-developers", "linux-users"); groups = groupsProvider.getGroups("robby"); assertThat(groups).containsOnly("sonar-users", "sonar-developers", "linux-users"); }