-
Notifications
You must be signed in to change notification settings - Fork 250
[JENKINS-19413] Use CredentialsDescriptor.findContextInPath rather than directly StaplerRequest2.findAncestorObject
#651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 @@ | |||||||||||||
| @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,71 +134,65 @@ | |||||||||||||
| Set<String> urls = new HashSet<>(); | ||||||||||||||
| List<StoreItem> result = new ArrayList<>(); | ||||||||||||||
| if (context == null) { | ||||||||||||||
| StaplerRequest2 request = Stapler.getCurrentRequest2(); | ||||||||||||||
| if (request != null) { | ||||||||||||||
| context = request.findAncestorObject(ModelObject.class); | ||||||||||||||
| } | ||||||||||||||
| context = CredentialsDescriptor.findContextInPath(ModelObject.class); | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing code had a check that we are in a Stapler request. if that was needed this will now throw a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can double check but the other overload of this method passed in
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
credentials-plugin/src/main/resources/lib/credentials/select.jelly Lines 115 to 117 in f8963c3
Whereas credentials-plugin/src/main/java/com/cloudbees/plugins/credentials/CredentialsDescriptor.java Line 328 in f8963c3
credentials-plugin/src/main/java/com/cloudbees/plugins/credentials/ContextInPath.java Line 44 in f8963c3
|
||||||||||||||
| } | ||||||||||||||
| if (context != null) { | ||||||||||||||
| for (CredentialsStore store : CredentialsProvider.lookupStores(context)) { | ||||||||||||||
| StoreItem item = new StoreItem(store); | ||||||||||||||
| String url = item.getUrl(); | ||||||||||||||
| if (item.getUrl() != null && !urls.contains(url)) { | ||||||||||||||
| result.add(item); | ||||||||||||||
| urls.add(url); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| if (includeUser) { | ||||||||||||||
| boolean hasPermission = false; | ||||||||||||||
| ModelObject current = context; | ||||||||||||||
| while (current != null) { | ||||||||||||||
| if (current instanceof AccessControlled) { | ||||||||||||||
| hasPermission = ((AccessControlled) current).hasPermission(CredentialsProvider.USE_OWN); | ||||||||||||||
| break; | ||||||||||||||
| } else if (current instanceof ComputerSet) { | ||||||||||||||
| current = Jenkins.get(); | ||||||||||||||
| } else { | ||||||||||||||
| // fall back to Jenkins as the ultimate parent of everything else | ||||||||||||||
| current = Jenkins.get(); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| if (hasPermission) { | ||||||||||||||
| for (CredentialsStore store : CredentialsProvider.lookupStores(User.current())) { | ||||||||||||||
| StoreItem item = new StoreItem(store); | ||||||||||||||
| String url = item.getUrl(); | ||||||||||||||
| if (item.getUrl() != null && !urls.contains(url)) { | ||||||||||||||
| result.add(item); | ||||||||||||||
| urls.add(url); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| return result; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Checks if the current user has permission to create a credential. | ||||||||||||||
| * | ||||||||||||||
| * @param context the context. | ||||||||||||||
| * @param includeUser whether they can use their own credentials store. | ||||||||||||||
| * @return {@code true} if they can create a permission. | ||||||||||||||
| * @since FIXME | ||||||||||||||
| */ | ||||||||||||||
| @Restricted(NoExternalUse.class) | ||||||||||||||
| @SuppressWarnings("unused") // used via jelly | ||||||||||||||
| public boolean hasCreatePermission(ModelObject context, boolean includeUser) { | ||||||||||||||
| if (includeUser) { | ||||||||||||||
| User current = User.current(); | ||||||||||||||
| if (current != null && current.hasPermission(CREATE)) { | ||||||||||||||
| return true; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| 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)) { | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing code had a check that we where in a Stapler request. if that was needed this will now throw a
NullPointerException, did you check that calls to this are only web methods, or that null is never passed for any non web methods?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#651 (comment) again