From debc588b30714ffa4d048eb6d73ca2c0b4abc7b2 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Tue, 12 Aug 2014 15:36:17 +0100 Subject: [PATCH] when using a case sensitive strategy encoded users can appear --- core/src/main/java/hudson/model/User.java | 4 +- .../main/java/jenkins/model/IdStrategy.java | 63 +++++++++++++++++++ test/src/test/java/hudson/model/UserTest.java | 6 ++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/model/User.java b/core/src/main/java/hudson/model/User.java index 222c69e2ef..8fba2fed29 100644 --- a/core/src/main/java/hudson/model/User.java +++ b/core/src/main/java/hudson/model/User.java @@ -459,6 +459,7 @@ public class User extends AbstractModelObject implements AccessControlled, Descr * Gets all the users. */ public static Collection getAll() { + final IdStrategy strategy = idStrategy(); if(System.currentTimeMillis() -lastScanned>10000) { // occasionally scan the file system to check new users // whether we should do this only once at start up or not is debatable. @@ -471,7 +472,7 @@ public class User extends AbstractModelObject implements AccessControlled, Descr for (File subdir : subdirs) if(new File(subdir,"config.xml").exists()) { - String name = subdir.getName(); + String name = strategy.idFromFilename(subdir.getName()); User.getOrCreate(name, name, true); } @@ -486,7 +487,6 @@ public class User extends AbstractModelObject implements AccessControlled, Descr byNameLock.readLock().unlock(); } Collections.sort(r,new Comparator() { - IdStrategy strategy = idStrategy(); public int compare(User o1, User o2) { return strategy.compare(o1.getId(), o2.getId()); diff --git a/core/src/main/java/jenkins/model/IdStrategy.java b/core/src/main/java/jenkins/model/IdStrategy.java index 1e4e462db2..8ccf691898 100644 --- a/core/src/main/java/jenkins/model/IdStrategy.java +++ b/core/src/main/java/jenkins/model/IdStrategy.java @@ -57,6 +57,16 @@ public abstract class IdStrategy extends AbstractDescribableImpl imp @Nonnull public abstract String filenameOf(@Nonnull String id); + /** + * Converts a filename into the corresponding id. + * @param filename the filename. + * @return the corresponding id. + * @since 1.577 + */ + public String idFromFilename(@Nonnull String filename) { + return filename; + } + /** * Converts an ID into a key for use in a Java Map. * @@ -214,6 +224,59 @@ public abstract class IdStrategy extends AbstractDescribableImpl imp } } + @Override + public String idFromFilename(@Nonnull String filename) { + if (filename.matches("[a-z0-9_. -]+")) { + return filename; + } else { + StringBuilder buf = new StringBuilder(filename.length()); + final char[] chars = filename.toCharArray(); + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if ('a' <= c && c <= 'z') { + buf.append(c); + } else if ('0' <= c && c <= '9') { + buf.append(c); + } else if ('_' == c || '.' == c || '-' == c || ' ' == c || '@' == c) { + buf.append(c); + } else if (c == '~') { + i++; + if (i < chars.length) { + buf.append(Character.toUpperCase(chars[i])); + } + } else if (c == '$') { + StringBuilder hex = new StringBuilder(4); + i++; + if (i < chars.length) { + hex.append(chars[i]); + } else { + break; + } + i++; + if (i < chars.length) { + hex.append(chars[i]); + } else { + break; + } + i++; + if (i < chars.length) { + hex.append(chars[i]); + } else { + break; + } + i++; + if (i < chars.length) { + hex.append(chars[i]); + } else { + break; + } + buf.append(Character.valueOf((char)Integer.parseInt(hex.toString(), 16))); + } + } + return buf.toString(); + } + } + /** * {@inheritDoc} */ diff --git a/test/src/test/java/hudson/model/UserTest.java b/test/src/test/java/hudson/model/UserTest.java index 096ed08a41..f84bc421fd 100644 --- a/test/src/test/java/hudson/model/UserTest.java +++ b/test/src/test/java/hudson/model/UserTest.java @@ -174,6 +174,8 @@ public class UserTest { User user = User.get("john smith"); User user2 = User.get("John Smith"); assertSame("Users should have the same id.", user.getId(), user2.getId()); + assertEquals(user.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user.getId()))); + assertEquals(user2.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user2.getId()))); } @Test @@ -191,6 +193,8 @@ public class UserTest { assertEquals("john smith", User.idStrategy().filenameOf(user.getId())); assertEquals("John Smith", User.idStrategy().keyFor(user2.getId())); assertEquals("~john ~smith", User.idStrategy().filenameOf(user2.getId())); + assertEquals(user.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user.getId()))); + assertEquals(user2.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user2.getId()))); } @Test @@ -212,6 +216,8 @@ public class UserTest { assertEquals("Users should have the same id.", user.getId(), user2.getId()); assertEquals("john.smith@acme.org", User.idStrategy().keyFor(user2.getId())); assertEquals("john.smith@acme.org", User.idStrategy().filenameOf(user2.getId())); + assertEquals(user.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user.getId()))); + assertEquals(user2.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user2.getId()))); } @Test -- GitLab