Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-38048] Permits credentialsId dropdowns to be used from a glo…
…bal configuration screen.
  • Loading branch information
jglick committed Sep 8, 2016
1 parent 7550539 commit 1355c64
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -114,7 +114,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1.16</version>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/hudson/scm/SubversionSCM.java
Expand Up @@ -3013,13 +3013,14 @@ public String getDisplayName() {
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item context, @QueryParameter String remote) {
if (context == null || !context.hasPermission(Item.EXTENDED_READ)) {
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(Item.EXTENDED_READ)) {
return new StandardListBoxModel();
}
return fillCredentialsIdItems(context, remote);
}

public ListBoxModel fillCredentialsIdItems(@Nonnull Item context, String remote) {
public ListBoxModel fillCredentialsIdItems(@CheckForNull Item context, String remote) {
List<DomainRequirement> domainRequirements;
if (remote == null) {
domainRequirements = Collections.<DomainRequirement>emptyList();
Expand Down Expand Up @@ -3075,7 +3076,8 @@ public FormValidation doCheckCredentialsId(StaplerRequest req, @AncestorInPath I
@QueryParameter String remote, @QueryParameter String value) {

// Test the connection only if we may use the credentials (cf. hudson.plugins.git.UserRemoteConfig.DescriptorImpl.doCheckUrl)
if (context == null || !context.hasPermission(CredentialsProvider.USE_ITEM)) {
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
return FormValidation.ok();
}
return checkCredentialsId(req, context, remote, value);
Expand Down Expand Up @@ -3297,7 +3299,8 @@ public String getDisplayName() {

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item context,
@QueryParameter String realm) {
if (context == null || !context.hasPermission(Item.EXTENDED_READ)) {
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(Item.EXTENDED_READ)) {
return new StandardListBoxModel();
}
List<DomainRequirement> domainRequirements;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/scm/SubversionTagAction.java
Expand Up @@ -340,7 +340,7 @@ public String getDisplayName() {
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item context, @AncestorInPath Run run) {
if (context == null || !context.hasPermission(SCM.TAG)) {
if (/* TODO consider making available in global context as well */context == null || !context.hasPermission(SCM.TAG)) {
return new ListBoxModel();
}
Set<StandardCredentials> c = new LinkedHashSet<StandardCredentials>();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/hudson/scm/browsers/Sventon.java
Expand Up @@ -41,6 +41,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import jenkins.model.Jenkins;

/**
* {@link RepositoryBrowser} for Sventon 1.x.
Expand Down Expand Up @@ -99,7 +100,10 @@ public String getDisplayName() {
public FormValidation doCheckUrl(@AncestorInPath Item project,
@QueryParameter(fixEmpty=true) final String value)
throws IOException, ServletException {
if(!project.hasPermission(Item.EXTENDED_READ)) return FormValidation.ok(); // can't check
if (project == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
project != null && !project.hasPermission(Item.EXTENDED_READ)) {
return FormValidation.ok();
}
if(value==null) // nothing entered yet
return FormValidation.ok();

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/hudson/scm/browsers/Sventon2.java
Expand Up @@ -42,6 +42,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import jenkins.model.Jenkins;

/**
* {@link RepositoryBrowser} for Sventon 2.x.
Expand Down Expand Up @@ -120,7 +121,10 @@ public String getDisplayName() {
public FormValidation doCheckUrl(@AncestorInPath Item project,
@QueryParameter(fixEmpty=true) final String value)
throws IOException, ServletException {
if(!project.hasPermission(Item.EXTENDED_READ)) return FormValidation.ok(); // can't check
if (project == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
project != null && !project.hasPermission(Item.EXTENDED_READ)) {
return FormValidation.ok();
}
if(value==null) // nothing entered yet
return FormValidation.ok();

Expand Down
Expand Up @@ -103,6 +103,7 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundSetter;

/**
Expand Down Expand Up @@ -784,13 +785,16 @@ public String getDisplayName() {
*/
@SuppressWarnings("unused") // by stapler
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
@QueryParameter String remoteBase) {
if (context == null || !context.hasPermission(Item.EXTENDED_READ)) {
return new StandardListBoxModel();
@QueryParameter String remoteBase,
@QueryParameter String credentialsId) {
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(Item.EXTENDED_READ)) {
return new StandardListBoxModel().includeCurrentValue(credentialsId);
}
List<DomainRequirement> domainRequirements;
domainRequirements = URIRequirementBuilder.fromUri(remoteBase.trim()).build();
return new StandardListBoxModel()
// TODO JENKINS-35553 update to newer APIs
.withEmptySelection()
.withMatching(
CredentialsMatchers.anyOf(
Expand All @@ -811,7 +815,8 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner cont
public FormValidation doCheckCredentialsId(StaplerRequest req, @AncestorInPath SCMSourceOwner context, @QueryParameter String remoteBase, @QueryParameter String value) {
// TODO suspiciously similar to SubversionSCM.ModuleLocation.DescriptorImpl.checkCredentialsId; refactor into shared method?
// Test the connection only if we may use the credentials
if (context == null || !context.hasPermission(CredentialsProvider.USE_ITEM)) {
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
return FormValidation.ok();
}

Expand Down

0 comments on commit 1355c64

Please sign in to comment.