diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsDescriptor.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsDescriptor.java index 08d60001..78af69ea 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsDescriptor.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsDescriptor.java @@ -337,111 +337,111 @@ public static T findContextInPath(@NonNull StaplerReques o = ((CredentialsStoreAction.CredentialsWrapper) o).getStore().getContext(); } else if (o instanceof CredentialsStoreAction.DomainWrapper) { o = ((CredentialsStoreAction.DomainWrapper) o).getStore().getContext(); - } else if (o instanceof Descriptor && i == 1) { // URL is /descriptorByName/... - // TODO this is a https://issues.jenkins-ci.org/browse/JENKINS-19413 workaround - - // we need to try an infer from the Referer as this is likely a doCheck or a doFill method - String referer = request.getReferer(); - String rootPath = request.getRootPath(); - if (referer != null && rootPath != null && referer.startsWith(rootPath)) { - // strip out any query portion of the referer URL. - String path = URI.create(referer.substring(rootPath.length())).getPath().substring(1); - - // TODO have Stapler expose a method that can walk a path and produce the ancestors and use that - - // what now follows is an example of a really evil hack, consequently this means... - // - // 7.. , - // MMM. MMM. - // MMMMM .MMMMMM - // MMMM. MMMMM. - // OMMM MMZ - // MMM MM - // .MMMM $. . .MM, - // MMMMM MMM MM MMM - // .MMMMM. MMMMD 8MMM. MMMM - // MMMMMMM.MMMM MMMMM. MMMMMM - // MMMMMMMM.M . MMM. MMMMMMM - // MMMMMMMMM. MMMMMMMM - // MMMMMMMMM . .. MMMMMMMMMMM - // MMMMMMMM IMMMM Z.MMMMMMMMMM , - // .MMMMMMM .M:M MMMMMMMMMMM M - // I MMMMMMM. MMMMMMMMMO M - // MMMM MMMMMMM .MMMMMMMMM. . - // :MMMMMM.MMMMMMM. MMMMMMMM .MMMM - // MMMMMMMMMMMMMMMM MMMMMMM MMMMMMMM - // MMMMMMMMM.MMMMMMM MMMMMMM MMMMMMMMMM. - // MMMMMMMMMMMMMMMM? MMMMMM MMMMMMMMMMM - // MMMMMMMMMM . . MMMMMIMMMMMMMMMMMM. - // MMMMMMMMMM .. :MMMMMMMMMMMM. - // DMMMMMMMMMM MMMMMMMMMMMMM. - // MMMMMMMMMM.M. MMMMMMMMMMMM. - // MMMMMM, .... - // - // I AM A SAD PANDA - - List pathSegments = new ArrayList<>(Arrays.asList(StringUtils.split(path, "/"))); - // strip out any leading junk - while (!pathSegments.isEmpty() && StringUtils.isBlank(pathSegments.get(0))) { - pathSegments.remove(0); + } + if (type.isInstance(o) && o instanceof ModelObject && CredentialsProvider.hasStores((ModelObject) o)) { + return type.cast(o); + } + } + // TODO this is a https://issues.jenkins-ci.org/browse/JENKINS-19413 workaround + // we need to try an infer from the Referer as this is likely a doCheck or a doFill method or select.jelly from a lazy-load fragment + String referer = request.getReferer(); + String rootPath = request.getRootPath(); + if (referer != null && rootPath != null && referer.startsWith(rootPath)) { + // strip out any query portion of the referer URL. + String path = URI.create(referer.substring(rootPath.length())).getPath().substring(1); + + // TODO have Stapler expose a method that can walk a path and produce the ancestors and use that + + // what now follows is an example of a really evil hack, consequently this means... + // + // 7.. , + // MMM. MMM. + // MMMMM .MMMMMM + // MMMM. MMMMM. + // OMMM MMZ + // MMM MM + // .MMMM $. . .MM, + // MMMMM MMM MM MMM + // .MMMMM. MMMMD 8MMM. MMMM + // MMMMMMM.MMMM MMMMM. MMMMMM + // MMMMMMMM.M . MMM. MMMMMMM + // MMMMMMMMM. MMMMMMMM + // MMMMMMMMM . .. MMMMMMMMMMM + // MMMMMMMM IMMMM Z.MMMMMMMMMM , + // .MMMMMMM .M:M MMMMMMMMMMM M + // I MMMMMMM. MMMMMMMMMO M + // MMMM MMMMMMM .MMMMMMMMM. . + // :MMMMMM.MMMMMMM. MMMMMMMM .MMMM + // MMMMMMMMMMMMMMMM MMMMMMM MMMMMMMM + // MMMMMMMMM.MMMMMMM MMMMMMM MMMMMMMMMM. + // MMMMMMMMMMMMMMMM? MMMMMM MMMMMMMMMMM + // MMMMMMMMMM . . MMMMMIMMMMMMMMMMMM. + // MMMMMMMMMM .. :MMMMMMMMMMMM. + // DMMMMMMMMMM MMMMMMMMMMMMM. + // MMMMMMMMMM.M. MMMMMMMMMMMM. + // MMMMMM, .... + // + // I AM A SAD PANDA + + List pathSegments = new ArrayList<>(Arrays.asList(StringUtils.split(path, "/"))); + // strip out any leading junk + while (!pathSegments.isEmpty() && StringUtils.isBlank(pathSegments.get(0))) { + pathSegments.remove(0); + } + if (pathSegments.size() >= 2) { + String firstSegment = pathSegments.get(0); + if ("user".equals(firstSegment)) { + User user = User.getById(pathSegments.get(1), true); + if (type.isInstance(user) && CredentialsProvider.hasStores(user)) { + // we have a winner + return type.cast(user); } - if (pathSegments.size() >= 2) { - String firstSegment = pathSegments.get(0); - if ("user".equals(firstSegment)) { - User user = User.getById(pathSegments.get(1), true); - if (type.isInstance(user) && CredentialsProvider.hasStores(user)) { - // we have a winner - return type.cast(user); + } else if ("job".equals(firstSegment) || "item".equals(firstSegment) || "view" + .equals(firstSegment)) { + int index = 0; + while (index < pathSegments.size()) { + String segment = pathSegments.get(index); + if ("view".equals(segment)) { + // remove the /view/ + pathSegments.remove(index); + if (index < pathSegments.size()) { + // remove the /view/{name} + pathSegments.remove(index); } - } else if ("job".equals(firstSegment) || "item".equals(firstSegment) || "view" - .equals(firstSegment)) { - int index = 0; + } else if ("job".equals(segment) || "item".equals(segment)) { + // remove the /job/ + pathSegments.remove(index); + // skip the name + index++; + } else { + // we have gone as far as we can parse the item path structure while (index < pathSegments.size()) { - String segment = pathSegments.get(index); - if ("view".equals(segment)) { - // remove the /view/ - pathSegments.remove(index); - if (index < pathSegments.size()) { - // remove the /view/{name} - pathSegments.remove(index); - } - } else if ("job".equals(segment) || "item".equals(segment)) { - // remove the /job/ - pathSegments.remove(index); - // skip the name - index++; - } else { - // we have gone as far as we can parse the item path structure - while (index < pathSegments.size()) { - // remove the remainder - pathSegments.remove(index); - } - } + // remove the remainder + pathSegments.remove(index); } - Jenkins jenkins = Jenkins.get(); - while (!pathSegments.isEmpty()) { - String fullName = StringUtils.join(pathSegments, "/"); - Item item = jenkins.getItemByFullName(fullName); - if (item != null) { - if (type.isInstance(item) && CredentialsProvider.hasStores(item)) { - // we have a winner - return type.cast(item); - } - } - // walk back up and try one level less deep - pathSegments.remove(pathSegments.size() - 1); + } + } + Jenkins jenkins = Jenkins.get(); + while (!pathSegments.isEmpty()) { + String fullName = StringUtils.join(pathSegments, "/"); + Item item = jenkins.getItemByFullName(fullName); + if (item != null) { + if (type.isInstance(item) && CredentialsProvider.hasStores(item)) { + // we have a winner + return type.cast(item); } } + // walk back up and try one level less deep + pathSegments.remove(pathSegments.size() - 1); } - // ok we give up, we are not thirsty for more, we'll let "normal" ancestor in path logic continue } } - if (type.isInstance(o) && o instanceof ModelObject && CredentialsProvider.hasStores((ModelObject) o)) { - return type.cast(o); - } + // ok we give up, we are not thirsty for more + } + if (type.isAssignableFrom(Jenkins.class)) { + return type.cast(Jenkins.get()); } return null; - } /** diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsSelectHelper.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsSelectHelper.java index 92501a70..bfde5fe8 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsSelectHelper.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsSelectHelper.java @@ -62,7 +62,6 @@ import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.Localizable; -import org.kohsuke.stapler.Stapler; import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse2; import org.kohsuke.stapler.interceptor.RequirePOST; @@ -119,14 +118,7 @@ public String getDisplayName() { @CheckForNull @Restricted(NoExternalUse.class) public ModelObject resolveContext(Object context) { - if (context instanceof ModelObject) { - return (ModelObject) context; - } - StaplerRequest2 request = Stapler.getCurrentRequest2(); - if (request != null) { - return request.findAncestorObject(ModelObject.class); - } - return null; + return context instanceof ModelObject mo ? mo : CredentialsDescriptor.findContextInPath(ModelObject.class); } /** @@ -142,10 +134,7 @@ public List getStoreItems(ModelObject context, boolean includeUser) { Set urls = new HashSet<>(); List result = new ArrayList<>(); if (context == null) { - StaplerRequest2 request = Stapler.getCurrentRequest2(); - if (request != null) { - context = request.findAncestorObject(ModelObject.class); - } + context = CredentialsDescriptor.findContextInPath(ModelObject.class); } if (context != null) { for (CredentialsStore store : CredentialsProvider.lookupStores(context)) { @@ -203,10 +192,7 @@ public boolean hasCreatePermission(ModelObject context, boolean includeUser) { } } if (context == null) { - StaplerRequest2 request = Stapler.getCurrentRequest2(); - if (request != null) { - context = request.findAncestorObject(ModelObject.class); - } + context = CredentialsDescriptor.findContextInPath(ModelObject.class); } for (CredentialsStore store : CredentialsProvider.lookupStores(context)) { if (store.hasPermission(CREATE)) {