Skip to content

Commit

Permalink
[JENKINS-11759] paass context for user ID retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Aug 18, 2012
1 parent f9f091c commit 80fcc48
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
29 changes: 26 additions & 3 deletions core/src/main/java/hudson/model/User.java
Expand Up @@ -289,8 +289,26 @@ public synchronized void doSubmitDescription( StaplerRequest req, StaplerRespons
* (by creating a new {@link User} object if none exists.)
* If false, this method will return null if {@link User} object
* with the given name doesn't exist.
* @deprecated use {@link User#get(String, boolean, java.util.Map)}
*/
public static @Nullable User get(String idOrFullName, boolean create) {
public static User get(String idOrFullName, boolean create) {
return get(idOrFullName, create, Collections.emptyMap());
}

/**
* Gets the {@link User} object by its id or full name.
*
* @param create
* If true, this method will never return null for valid input
* (by creating a new {@link User} object if none exists.)
* If false, this method will return null if {@link User} object
* with the given name doesn't exist.
*
* @param context
* contextual environment this user idOfFullName was retrieved from,
* that can help resolve the user ID
*/
public static User get(String idOrFullName, boolean create, Map context) {

This comment has been minimized.

Copy link
@jglick

jglick Aug 20, 2012

Member

Raw types! What are the keys and values? Use a generic type, e.g. <String,String> or <String,?>. (In the latter case, keys such as REALM must document the expected value type using @link.

This comment has been minimized.

Copy link
@ndeloof

ndeloof Aug 20, 2012

Author Contributor

good point, will do

This comment has been minimized.

Copy link
@kutzi

kutzi Mar 3, 2013

Member

The raw types are still in there!
BTW: I find it quite hard to understand what this context should be. It doesn't really help that all the places in Jenkins core are still using the old, deprecated method variant


if(idOrFullName==null)
return null;
Expand All @@ -301,7 +319,7 @@ public synchronized void doSubmitDescription( StaplerRequest req, StaplerRespons

String id = null;
for (CannonicalIdResolver resolver : resolvers) {
id = resolver.resolveCannonicalId(idOrFullName);
id = resolver.resolveCannonicalId(idOrFullName, context);
if (id != null) break;
}
// DefaultUserCannonicalIdResolver will always return a non-null id if all other CannonicalIdResolver failed
Expand Down Expand Up @@ -642,6 +660,11 @@ public List<Action> getTransientActions() {

public static abstract class CannonicalIdResolver extends AbstractDescribableImpl<CannonicalIdResolver> implements Comparable<CannonicalIdResolver> {

/**
* context key for realm (domain) where idOrFullName has been retreived from.
* Can be used (for example) to distinguish ambiguous committer ID using the SCM URL.
*/
public static final String REALM = "realm";

public int compareTo(CannonicalIdResolver o) {
// reverse priority order
Expand All @@ -654,7 +677,7 @@ public int compareTo(CannonicalIdResolver o) {
* extract user ID from idOrFullName with help from contextual infos.
* can return <code>null</code> if no user ID matched the input
*/
public abstract @Nullable String resolveCannonicalId(String idOrFullName);
public abstract @Nullable String resolveCannonicalId(String idOrFullName, Map context);

This comment has been minimized.

Copy link
@jglick

jglick Mar 4, 2013

Member

If you are going to make an incompatible change, at least correct the typo (should be “canonical” not “cannonical”).


public int getPriority() {
return 1;
Expand Down
Expand Up @@ -29,6 +29,8 @@
import hudson.model.Descriptor;
import hudson.model.User;

import java.util.Map;

/**
* Default User.CannonicalIdResolver to escape unsupported characters and generate user ID.
* Compared to other implementations, this resolver will always return an ID
Expand All @@ -39,7 +41,7 @@
public class DefaultUserCannonicalIdResolver extends User.CannonicalIdResolver {

@Override
public String resolveCannonicalId(String idOrFullName) {
public String resolveCannonicalId(String idOrFullName, Map context) {
String id = idOrFullName.replace('\\', '_').replace('/', '_').replace('<','_')
.replace('>', '_'); // 4 replace() still faster than regex
if (Functions.isWindows()) id = id.replace(':','_');
Expand Down

0 comments on commit 80fcc48

Please sign in to comment.