Skip to content

Commit

Permalink
[FIXED JENKINS-41478] Add button should be enabled if any stores are …
Browse files Browse the repository at this point in the history
…enabled

- Also disabled stores should be disabled
  • Loading branch information
stephenc committed Jan 26, 2017
1 parent 1e606c9 commit aa6801a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
Expand Up @@ -179,6 +179,37 @@ public List<StoreItem> getStoreItems(ModelObject context, boolean includeUser) {
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) {
StaplerRequest request = Stapler.getCurrentRequest();
if (request != null) {
context = request.findAncestorObject(ModelObject.class);
}
}
for (CredentialsStore store : CredentialsProvider.lookupStores(context)) {
if (store.hasPermission(CREATE)) {
return true;
}
}
return false;
}

/**
* Stapler binding for the resolver URL segment.
*
Expand Down Expand Up @@ -405,7 +436,7 @@ public String getIconClassName() {
* @return {@code true} if the current user can add credentials to this store.
*/
public boolean isEnabled() {
return url != null && store.hasPermission(Permission.CREATE) && !store.getCredentialsDescriptors()
return url != null && store.hasPermission(CREATE) && !store.getCredentialsDescriptors()
.isEmpty();
}

Expand Down
27 changes: 19 additions & 8 deletions src/main/resources/lib/credentials/select.jelly
Expand Up @@ -77,7 +77,7 @@
<st:nbsp/>
<j:set var="storeItems" value="${selectHelper.getStoreItems(context, includeUser)}"/>
<j:choose>
<j:when test="${h.hasPermission(selectHelper.CREATE) and storeItems != null and !storeItems.isEmpty()}">
<j:when test="${selectHelper.hasCreatePermission(context, includeUser) and storeItems != null and !storeItems.isEmpty()}">
<button type="button" class="credentials-add-menu" menualign="${attrs.menuAlign}"
suffix="${attrs.name}">
<l:icon class="icon-credentials-new-credential icon-sm"/>
Expand All @@ -87,13 +87,24 @@
<div class="bd">
<ul class="first-of-type">
<j:forEach var="storeItem" items="${storeItems}">
<li class="yuimenuitem" data-url="${request.contextPath}/${storeItem.url}/dialog"
disabled="${storeItem.enabled?null:'disabled'}">
<span class="yuimenuitemlabel" tooltip="${storeItem.description}">
<l:icon class="${storeItem.iconClassName} icon-sm"/>
${storeItem.displayName}
</span>
</li>
<j:choose>
<j:when test="${storeItem.enabled}">
<li class="yuimenuitem" data-url="${request.contextPath}/${storeItem.url}/dialog">
<span class="yuimenuitemlabel" tooltip="${storeItem.description}">
<l:icon class="${storeItem.iconClassName} icon-sm"/>
${storeItem.displayName}
</span>
</li>
</j:when>
<j:otherwise>
<li class="yuimenuitem" disabled="disabled">
<span class="yuimenuitemlabel" tooltip="${storeItem.description}" disabled="disabled">
<l:icon class="${storeItem.iconClassName} icon-sm"/>
${storeItem.displayName}
</span>
</li>
</j:otherwise>
</j:choose>
</j:forEach>
</ul>
</div>
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/lib/credentials/select/select.js
Expand Up @@ -200,6 +200,17 @@ Behaviour.specify("BUTTON.credentials-add-menu", 'credentials-select', -99, func
}
window.credentials.add(item.srcElement.getAttribute('data-url'));
});
// YUI menu will not parse disabled when using DIV-LI only when using SELECT-OPTION
// but SELECT-OPTION doesn't support images, so we need to catch the rendering and roll our
// own disabled attribute support
menuButton.getMenu().beforeShowEvent.subscribe(function(type,args,value){
var items = this.getItems();
for (var i = 0; i < items.length; i++) {
if (items[i].srcElement.getAttribute('disabled')) {
items[i].cfg.setProperty('disabled', true);
}
}
});
}
e=null;
});
Expand Down

0 comments on commit aa6801a

Please sign in to comment.