From af42534defea4503347dfc0ec442fadf51cf3336 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Sat, 18 Aug 2012 09:19:32 +0200 Subject: [PATCH] [JENKINS-11759] extension point to resolve jenkins user from SCM id --- core/src/main/java/hudson/model/User.java | 48 ++++++++++---- .../DefaultUserCannonicalIdResolver.java | 65 +++++++++++++++++++ 2 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 core/src/main/java/jenkins/model/DefaultUserCannonicalIdResolver.java diff --git a/core/src/main/java/hudson/model/User.java b/core/src/main/java/hudson/model/User.java index 5f7eadc663..b7b1c80687 100644 --- a/core/src/main/java/hudson/model/User.java +++ b/core/src/main/java/hudson/model/User.java @@ -26,12 +26,7 @@ package hudson.model; import com.infradna.tool.bridge_method_injector.WithBridgeMethods; import com.thoughtworks.xstream.XStream; -import hudson.CopyOnWrite; -import hudson.FeedAdapter; -import hudson.Functions; -import hudson.Util; -import hudson.XmlFile; -import hudson.BulkChange; +import hudson.*; import hudson.model.Descriptor.FormException; import hudson.model.listeners.SaveableListener; import hudson.security.ACL; @@ -77,7 +72,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; -import javax.annotation.Nullable; /** * Represents a user. @@ -295,12 +289,21 @@ public class User extends AbstractModelObject implements AccessControlled, Descr * If false, this method will return null if {@link User} object * with the given name doesn't exist. */ - public static @Nullable User get(String idOrFullName, boolean create) { + public static User get(String idOrFullName, boolean create) { + if(idOrFullName==null) return null; - String id = idOrFullName.replace('\\', '_').replace('/', '_').replace('<','_') - .replace('>','_'); // 4 replace() still faster than regex - if (Functions.isWindows()) id = id.replace(':','_'); + + // sort resolvers by priority + List resolvers = new ArrayList(Jenkins.getInstance().getExtensionList(CannonicalIdResolver.class)); + Collections.sort(resolvers); + + String id = null; + for (CannonicalIdResolver resolver : resolvers) { + id = resolver.resolveCannonicalId(idOrFullName); + if (id != null) break; + } + // DefaultUserCannonicalIdResolver will always return a non-null id if all other CannonicalIdResolver failed String idkey = id.toLowerCase(Locale.ENGLISH); @@ -635,5 +638,28 @@ public class User extends AbstractModelObject implements AccessControlled, Descr } return Collections.unmodifiableList(actions); } + + public static abstract class CannonicalIdResolver extends AbstractDescribableImpl implements Comparable { + + + public int compareTo(CannonicalIdResolver o) { + // reverse priority order + int i = getPriority(); + int j = o.getPriority(); + return i>j ? -1 : (i==j ? 0:1); + } + + /** + * extract user ID from idOrFullName with help from contextual infos. + * can return null if no user ID matched the input + */ + public abstract String resolveCannonicalId(String idOrFullName); + + public int getPriority() { + return 1; + } + + } } + diff --git a/core/src/main/java/jenkins/model/DefaultUserCannonicalIdResolver.java b/core/src/main/java/jenkins/model/DefaultUserCannonicalIdResolver.java new file mode 100644 index 0000000000..fec33f2468 --- /dev/null +++ b/core/src/main/java/jenkins/model/DefaultUserCannonicalIdResolver.java @@ -0,0 +1,65 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2012, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt, + * Tom Huybrechts, Vincent Latombe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package jenkins.model; + +import hudson.Extension; +import hudson.Functions; +import hudson.model.Descriptor; +import hudson.model.User; + +/** + * Default User.CannonicalIdResolver to escape unsupported characters and generate user ID. + * Compared to other implementations, this resolver will always return an ID + * + * @author: Nicolas De Loof +*/ +@Extension +public class DefaultUserCannonicalIdResolver extends User.CannonicalIdResolver { + + @Override + public String resolveCannonicalId(String idOrFullName) { + String id = idOrFullName.replace('\\', '_').replace('/', '_').replace('<','_') + .replace('>', '_'); // 4 replace() still faster than regex + if (Functions.isWindows()) id = id.replace(':','_'); + return id; + } + + @Override + public int getPriority() { + return Integer.MIN_VALUE; + } + + @Override + public Descriptor getDescriptor() { + return DESCRIPTOR; + } + + public static final Descriptor DESCRIPTOR = new Descriptor() { + public String getDisplayName() { + return "compute default user ID"; + } + }; + +} -- GitLab