提交 af42534d 编写于 作者: N Nicolas De Loof

[JENKINS-11759] extension point to resolve jenkins user from SCM id

上级 6dfd8db3
......@@ -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<CannonicalIdResolver> resolvers = new ArrayList<CannonicalIdResolver>(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<CannonicalIdResolver> implements Comparable<CannonicalIdResolver> {
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 <code>null</code> if no user ID matched the input
*/
public abstract String resolveCannonicalId(String idOrFullName);
public int getPriority() {
return 1;
}
}
}
/*
* 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: <a hef="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
@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<User.CannonicalIdResolver> getDescriptor() {
return DESCRIPTOR;
}
public static final Descriptor<User.CannonicalIdResolver> DESCRIPTOR = new Descriptor<User.CannonicalIdResolver>() {
public String getDisplayName() {
return "compute default user ID";
}
};
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册