To only show the project information to people who otherwise would be able to obtain it by viewing the job configuration, we can set up the action so the link is only shown to those with the Item.CONFIGURE
permission.
(...)
@Override
public String getIconFileName() {
return this.project.hasPermission(Item.CONFIGURE) ? "clipboard.png" : null; (1)
}
(...)
1 |
Returning null is a documented way for getIconFileName to make an action not appear in the side panel. |
This will not prevent direct access via the URL however, so we need make sure to restrict who can access the action.
A reliable way to do this is to implement StaplerProxy
, an interface intended to allow objects to forward HTTP request processing to another object. By implementing the getTarget()
method and returning this
, the request will continue to be processed by the same object, but we’re able to check user permissions before that happens.
(...)
import org.kohsuke.stapler.StaplerProxy;
public class SampleAction implements Action, StaplerProxy {
(...)
@Override
public Object getTarget() {
this.project.checkPermission(Item.CONFIGURE); (1)
return this;
}
}
1 |
This throws an AccessDeniedException if the check fails, resulting in the user seeing an error message (or, if not already logged in, a login screen). |