Class AbstractIdCredentialsListBoxModel<T extends AbstractIdCredentialsListBoxModel<T,C>,C extends IdCredentials>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<ListBoxModel.Option>
hudson.util.ListBoxModel
com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel<T,C>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<ListBoxModel.Option>, Collection<ListBoxModel.Option>, List<ListBoxModel.Option>, RandomAccess, org.kohsuke.stapler.HttpResponse
Direct Known Subclasses:
StandardListBoxModel, StandardUsernameListBoxModel

public abstract class AbstractIdCredentialsListBoxModel<T extends AbstractIdCredentialsListBoxModel<T,C>,C extends IdCredentials> extends ListBoxModel
ListBoxModel with support for credentials.

This class is convenient for providing the config.groovy or config.jelly fragment for a collection of objects of some IdCredentials subtype.

If you want to let the user configure a credentials object, do the following:

First, create a field that stores the credentials ID and defines a corresponding parameter in the constructor:

 private String credentialsId;

 @DataBoundConstructor
 public MyModel( .... , String credentialsId) {
     this.credentialsId = credentialsId;
     ...
 }
 public String getCredentialsId() {return credentialsId;}
 

Your config.groovy should have the following entry to render a drop-down list box:

 f.entry(title:_("Credentials"), field:"credentialsId") {
     f.select()
 }
 

Finally, your Descriptor implementation should have the doFillCredentialsIdItems method, which lists up the credentials available in this context:

 public ListBoxModel doFillCredentialsIdItems(@QueryParam String value) {
     if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { // or whatever permission is appropriate for
     this page
         // Important! Otherwise you expose credentials metadata to random web requests.
         return new StandardUsernameListBoxModel().includeCurrentValue(value);
     }
     return new StandardUsernameListBoxModel()
             .includeEmptySelection()
             .include(StandardUsernameCredentials.class,...))
             .includeCurrentValue(value);
 }
 

Exactly which overloaded version of the include(Item, Class) depends on the context in which your model operates. Here are a few common examples:

System-level settings
If your model is a singleton in the whole Jenkins instance, things that belong to the root Jenkins (such as agents), or do not have any ancestors serving as the context, then use Jenkins.get() as the context.
Job-level settings
If your model is a configuration fragment added to a Item (such as its major subtype Job), then use that Item as the context. For example:
 public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item context, @QueryParameter String source) {
     if (context == null || !context.hasPermission(Item.CONFIGURE)) {
         return new StandardUsernameListBoxModel().includeCurrentValue(value);
     }
     return new StandardUsernameListBoxModel()
             .includeEmptySelection()
             .includeAs(Tasks.getAuthenticationOf(context), context, StandardUsernameCredentials.class,
                 URIRequirementBuilder.fromUri(source).build())
             .includeCurrentValue(value);
 }
 
Since:
1.6
See Also: