From 3d699cb14d6c891338567c562f7fe8d81e318b1c Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 24 Apr 2012 07:48:39 -0500 Subject: [PATCH] Allow for admin teams with spaces in their name. --- src/com/gitblit/LdapUserService.java | 22 ++++++------- src/com/gitblit/utils/StringUtils.java | 32 +++++++++++-------- .../gitblit/tests/LdapUserServiceTest.java | 5 +-- tests/com/gitblit/tests/StringUtilsTest.java | 25 +++++++++++---- .../resources/ldapUserServiceSampleData.ldif | 8 +++++ 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/com/gitblit/LdapUserService.java b/src/com/gitblit/LdapUserService.java index 9fcef9d6..86b61364 100644 --- a/src/com/gitblit/LdapUserService.java +++ b/src/com/gitblit/LdapUserService.java @@ -20,6 +20,7 @@ import java.io.File; import java.net.URI; import java.net.URISyntaxException; import java.security.GeneralSecurityException; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -174,17 +175,16 @@ public class LdapUserService extends GitblitUserService { } private void setAdminAttribute(UserModel user) { - String adminString = settings.getString(Keys.realm.ldap_admins, ""); - String[] admins = adminString.split(" "); - user.canAdmin = false; - for (String admin : admins) { - if (admin.startsWith("@")) { // Team - if (user.getTeam(admin.substring(1)) != null) - user.canAdmin = true; - } else - if (user.getName().equalsIgnoreCase(admin)) - user.canAdmin = true; - } + user.canAdmin = false; + List admins = settings.getStrings(Keys.realm.ldap_admins); + for (String admin : admins) { + if (admin.startsWith("@")) { // Team + if (user.getTeam(admin.substring(1)) != null) + user.canAdmin = true; + } else + if (user.getName().equalsIgnoreCase(admin)) + user.canAdmin = true; + } } private void getTeamsFromLdap(LDAPConnection ldapConnection, String simpleUsername, SearchResultEntry loggingInUser, UserModel user) { diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index 0460ac9f..2c357241 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/src/com/gitblit/utils/StringUtils.java @@ -327,20 +327,24 @@ public class StringUtils { * @return list of strings */ public static List getStringsFromValue(String value, String separator) { - List strings = new ArrayList(); - try { - String[] chunks = value.split(separator); - for (String chunk : chunks) { - chunk = chunk.trim(); - if (chunk.length() > 0) { - strings.add(chunk); - } - } - } catch (PatternSyntaxException e) { - throw new RuntimeException(e); - } - return strings; - } + List strings = new ArrayList(); + try { + String[] chunks = value.split(separator + "(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); + for (String chunk : chunks) { + chunk = chunk.trim(); + if (chunk.length() > 0) { + if (chunk.charAt(0) == '"' && chunk.charAt(chunk.length() - 1) == '"') { + // strip double quotes + chunk = chunk.substring(1, chunk.length() - 1).trim(); + } + strings.add(chunk); + } + } + } catch (PatternSyntaxException e) { + throw new RuntimeException(e); + } + return strings; + } /** * Validates that a name is composed of letters, digits, or limited other diff --git a/tests/com/gitblit/tests/LdapUserServiceTest.java b/tests/com/gitblit/tests/LdapUserServiceTest.java index 777d86cf..43af24f2 100644 --- a/tests/com/gitblit/tests/LdapUserServiceTest.java +++ b/tests/com/gitblit/tests/LdapUserServiceTest.java @@ -69,7 +69,7 @@ public class LdapUserServiceTest { backingMap.put("realm.ldap.accountPattern", "(&(objectClass=person)(sAMAccountName=${username}))"); backingMap.put("realm.ldap.groupBase", "OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain"); backingMap.put("realm.ldap.groupPattern", "(&(objectClass=group)(member=${dn}))"); - backingMap.put("realm.ldap.admins", "UserThree @Git_Admins"); + backingMap.put("realm.ldap.admins", "UserThree @Git_Admins \"@Git Admins\""); MemorySettings ms = new MemorySettings(backingMap); @@ -92,7 +92,8 @@ public class LdapUserServiceTest { assertNotNull(userTwoModel); assertNotNull(userTwoModel.getTeam("git_users")); assertNull(userTwoModel.getTeam("git_admins")); - assertFalse(userTwoModel.canAdmin); + assertNotNull(userTwoModel.getTeam("git admins")); + assertTrue(userTwoModel.canAdmin); UserModel userThreeModel = ldapUserService.authenticate("UserThree", "userThreePassword".toCharArray()); assertNotNull(userThreeModel); diff --git a/tests/com/gitblit/tests/StringUtilsTest.java b/tests/com/gitblit/tests/StringUtilsTest.java index 2e00fa3e..91bfa672 100644 --- a/tests/com/gitblit/tests/StringUtilsTest.java +++ b/tests/com/gitblit/tests/StringUtilsTest.java @@ -112,13 +112,24 @@ public class StringUtilsTest { @Test public void testStringsFromValue() throws Exception { - List strings = StringUtils.getStringsFromValue("A B C D"); - assertEquals(4, strings.size()); - assertEquals("A", strings.get(0)); - assertEquals("B", strings.get(1)); - assertEquals("C", strings.get(2)); - assertEquals("D", strings.get(3)); - } + List strings = StringUtils.getStringsFromValue("\"A A \" B \"C C\" D \"\" \"E\""); + assertEquals(6, strings.size()); + assertEquals("A A", strings.get(0)); + assertEquals("B", strings.get(1)); + assertEquals("C C", strings.get(2)); + assertEquals("D", strings.get(3)); + assertEquals("", strings.get(4)); + assertEquals("E", strings.get(5)); + + strings = StringUtils.getStringsFromValue("\"A A \", B, \"C C\", D, \"\", \"E\"", ","); + assertEquals(6, strings.size()); + assertEquals("A A", strings.get(0)); + assertEquals("B", strings.get(1)); + assertEquals("C C", strings.get(2)); + assertEquals("D", strings.get(3)); + assertEquals("", strings.get(4)); + assertEquals("E", strings.get(5)); + } @Test public void testStringsFromValue2() throws Exception { diff --git a/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif b/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif index 22d68699..84ee243e 100644 --- a/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif +++ b/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif @@ -25,6 +25,13 @@ cn: Git_Admins sAMAccountName: Git_Admins member: CN=UserOne,OU=US,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain +dn: CN=Git Admins,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain +objectClass: top +objectClass: group +cn: Git Admins +sAMAccountName: Git_Admins_With_Space +member: CN=UserTwo,OU=US,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain + dn: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain objectClass: top objectClass: group @@ -64,6 +71,7 @@ objectClass: person sAMAccountName: UserTwo userPassword: userTwoPassword memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain +memberOf: CN=Git Admins,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain dn: CN=UserThree,OU=Canada,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain objectClass: user -- 2.39.5