Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-26578] More refined workaround
  • Loading branch information
stephenc committed May 24, 2016
1 parent 10ee0d2 commit cbb9fda
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 44 deletions.
Expand Up @@ -94,42 +94,44 @@ public String getDisplayName() {
return Messages.CredentialsSelectHelper_DisplayName();
}

/**
/**
* Returns the {@link StoreItem} instances for the current Stapler request.
*
* @param includeUser {@code true} to also include any User scoped stores.
* @return the {@link StoreItem} instances for the current Stapler request.
* @since 2.0
* @since 2.0.5
*/
@Restricted(NoExternalUse.class)
public List<StoreItem> getStoreItems(boolean includeUser) {
public List<StoreItem> getStoreItems(ModelObject context, boolean includeUser) {
List<StoreItem> result = new ArrayList<StoreItem>();
StaplerRequest request = Stapler.getCurrentRequest();
if (request != null) {
ModelObject context = request.findAncestorObject(ModelObject.class);
if (context != null) {
for (CredentialsStore store : CredentialsProvider.lookupStores(context)) {
result.add(new StoreItem(store));
}
if (context == null) {
StaplerRequest request = Stapler.getCurrentRequest();
if (request != null) {
context = request.findAncestorObject(ModelObject.class);
}
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.getActiveInstance();
} else {
// fall back to Jenkins as the ultimate parent of everything else
current = Jenkins.getActiveInstance();
}
}
if (context != null) {
for (CredentialsStore store : CredentialsProvider.lookupStores(context)) {
result.add(new StoreItem(store));
}
}
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.getActiveInstance();
} else {
// fall back to Jenkins as the ultimate parent of everything else
current = Jenkins.getActiveInstance();
}
if (hasPermission) {
for (CredentialsStore store : CredentialsProvider.lookupStores(User.current())) {
result.add(new StoreItem(store));
}
}
if (hasPermission) {
for (CredentialsStore store : CredentialsProvider.lookupStores(User.current())) {
result.add(new StoreItem(store));
}
}
}
Expand Down
Expand Up @@ -31,23 +31,39 @@
<j:set var="descriptor" value="${it.credentialDescriptor}"/>
<j:set var="instance" value="${null}"/>
<f:form action="createCredentials" method="POST" name="newCredentials">
<!-- TODO revert to dropdownDescriptorSelector when baseline is 1.645+ which supports `capture` attribute -->
<j:set var="descriptors" value="${it.store.credentialsDescriptors}" />
<f:dropdownList name="credentials" title="${%Kind}" help="${descriptor.getHelpFile('credentials')}">
<j:set var="current" value="${instance[credentials]}"/>
<j:set var="current" value="${current!=null ? current : null}"/>
<j:forEach var="descriptor" items="${descriptors}" varStatus="loop">
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}"
selected="${current.descriptor==descriptor || (current==null and descriptor==attrs.default)}" staplerClass="${descriptor.clazz.name}"
lazy="descriptor,it">
<l:ajax>
<j:set var="instance" value="${current.descriptor==descriptor ? current : null}" />
<st:include from="${descriptor}" page="${descriptor.configPage}" />
</l:ajax>
</f:dropdownListBlock>
</j:forEach>
</f:dropdownList>
<!--f:dropdownDescriptorSelector field="credentials" title="${%Kind}" lazy="it"/-->
<j:choose>
<j:when test="${descriptors.size()==1}">
<f:entry title="${%Kind}" help="${descriptor.getHelpFile('credentials')}">
<i>${descriptors[0].displayName}</i>
</f:entry>
<f:rowSet name="credentials">
<j:set var="descriptor" value="${descriptors[0]}"/>
<j:set var="instance" value="${null}"/>
<f:class-entry descriptor="${descriptor}"/>
<st:include from="${descriptor}" page="${descriptor.configPage}"/>
</f:rowSet>
</j:when>
<j:otherwise>
<!-- TODO revert to dropdownDescriptorSelector when baseline is 1.645+ which supports `capture` attribute -->
<f:dropdownList name="credentials" title="${%Kind}" help="${descriptor.getHelpFile('credentials')}">
<j:set var="current" value="${instance[credentials]}"/>
<j:set var="current" value="${current!=null ? current : null}"/>
<j:forEach var="descriptor" items="${descriptors}" varStatus="loop">
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}"
selected="${current.descriptor==descriptor || (current==null and descriptor==attrs.default)}"
staplerClass="${descriptor.clazz.name}"
lazy="descriptor,it">
<l:ajax>
<j:set var="instance" value="${current.descriptor==descriptor ? current : null}"/>
<st:include from="${descriptor}" page="${descriptor.configPage}"/>
</l:ajax>
</f:dropdownListBlock>
</j:forEach>
</f:dropdownList>
<!--f:dropdownDescriptorSelector field="credentials" title="${%Kind}" lazy="it"/-->
</j:otherwise>
</j:choose>

<f:bottomButtonBar>
<f:submit value="${%OK}"/>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/credentials/select.jelly
Expand Up @@ -81,7 +81,7 @@
<div class="credentials-add-menu-items yuimenu">
<div class="bd">
<ul class="first-of-type">
<j:forEach var="storeItem" items="${selectHelper.getStoreItems(includeUser)}">
<j:forEach var="storeItem" items="${selectHelper.getStoreItems(it, includeUser)}">
<li class="yuimenuitem" data-url="${storeItem.url}/dialog" disabled="${storeItem.enabled?null:'disabled'}">
<span class="yuimenuitemlabel" tooltip="${storeItem.description}">
<l:icon class="${storeItem.iconClassName} icon-sm"/>
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/lib/credentials/select/select.js
Expand Up @@ -241,3 +241,12 @@ Behaviour.specify("INPUT.credentials-select", 'credentials-select', -100, functi
}).bind($(x));
x.onchange();
});
(function() {
// HACK: can be removed once base version of Jenkins has fix of https://issues.jenkins-ci.org/browse/JENKINS-26578
// need to apply the new behaviours to existing objects
var controls = document.getElementsByClassName('credentials-select-control');
var count = controls.length;
for (var i = 0; i < count; i++) {
Behaviour.applySubtree(controls[i]);
}
})();

0 comments on commit cbb9fda

Please sign in to comment.