Skip to content
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

Add permission concept for links #168

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import hudson.security.Permission;

@ExportedBean
public class AppNavLink extends AbstractLink {

private String url;
private String label;
private Logo logo;

private boolean external;

private transient String color = "";
private Permission permission;

@DataBoundConstructor
public AppNavLink(String url, String label, Logo logo) {
public AppNavLink(String url, String label, Logo logo, Permission permission) {
this.url = url;
this.label = label;
this.logo = logo;
this.permission = permission;
}

@Exported
Expand Down Expand Up @@ -83,6 +84,13 @@ public void setLogo(Logo logo) {
this.logo = logo;
}

public Permission getPermission() {
return permission;
}

public void setPermission(Permission permission) {
this.permission = permission;
}

@Exported
@Override
Expand All @@ -92,6 +100,9 @@ public String getType() {

@Exported
public String getLinkUrl() {
if (permission != null && !Jenkins.get().hasPermission(permission)) {
return null;
}
try {
URI uri = new URI(url);
if (!uri.isAbsolute()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;

@Extension
@org.jenkinsci.Symbol("customHeader")
Expand Down Expand Up @@ -65,6 +68,9 @@ public class CustomHeaderConfiguration extends GlobalConfiguration {

private static final transient Symbol star = new Symbol("symbol-star plugin-ionicons-api");

public static final PermissionGroup PERMISSIONS = new PermissionGroup(CustomHeaderConfiguration.class, Messages._CustomHeaderConfiguration_PermissionsTitle());
public static final Permission VIEW_LINK = new Permission(PERMISSIONS, "ViewLink", Messages._CustomHeaderConfiguration_ViewLinkPermissionDescription(), null, PermissionScope.JENKINS);

@DataBoundConstructor
public CustomHeaderConfiguration() {
load();
Expand Down Expand Up @@ -190,7 +196,13 @@ public void deleteSystemMessage(String id) {
}

public List<AbstractLink> getLinks() {
return links;
User user = User.current();
if (user != null) {
return links.stream()
.filter(link -> link.getPermission() == null || user.hasPermission(link.getPermission()))
.collect(Collectors.toList());
}
return Collections.emptyList();
}

@DataBoundSetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
<f:entry field="external" title="${%External}">
<f:checkbox/>
</f:entry>
<f:entry field="permission" title="${%Permission}">
<f:dropdownDescriptorSelector field="permission"/>
</f:entry>
<f:dropdownDescriptorSelector field="logo" title="${%The logo to show}" descriptors="${descriptor.logoDescriptors}"/>
</j:jelly>
Loading