forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManageJenkinsAction.java
More file actions
154 lines (135 loc) · 5.73 KB
/
ManageJenkinsAction.java
File metadata and controls
154 lines (135 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model;
import hudson.Extension;
import hudson.Util;
import hudson.util.HudsonIsLoading;
import hudson.util.HudsonIsRestarting;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.management.Badge;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithContextMenu;
import jenkins.model.experimentalflags.NewManageJenkinsUserExperimentalFlag;
import org.apache.commons.jelly.JellyException;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerFallback;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
/**
* Adds the "Manage Jenkins" link to the navigation bar.
*
* @author Kohsuke Kawaguchi
*/
@Extension(ordinal = 998) @Symbol("manageJenkins")
public class ManageJenkinsAction implements RootAction, StaplerFallback, ModelObjectWithContextMenu {
private static final Logger LOGGER = Logger.getLogger(ManageJenkinsAction.class.getName());
@Override
public String getIconFileName() {
if (Jenkins.get().hasAnyPermission(Jenkins.MANAGE, Jenkins.SYSTEM_READ))
return "symbol-settings";
else
return null;
}
@Override
public String getDisplayName() {
return Messages.ManageJenkinsAction_DisplayName();
}
@Override
public String getUrlName() {
return "/manage";
}
@Override
public boolean isPrimaryAction() {
return true;
}
public HttpRedirect doIndex(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException {
try {
var newUiEnabled = new NewManageJenkinsUserExperimentalFlag().getFlagValue();
if (newUiEnabled) {
return new HttpRedirect("configure");
}
req.getView(this, "index.jelly").forward(req, rsp);
} catch (ServletException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Object getStaplerFallback() {
return Jenkins.get();
}
@Override
public ContextMenu doContextMenu(StaplerRequest2 request, StaplerResponse2 response) throws JellyException, IOException {
return new ContextMenu().from(this, request, response, "index");
}
/**
* Workaround to ensuring that links in context menus resolve correctly in the submenu of the top-level 'Dashboard'
* menu.
*/
@Restricted(NoExternalUse.class)
public void addContextMenuItem(ContextMenu menu, String url, String icon, String iconXml, String text, boolean post, boolean requiresConfirmation, Badge badge, String message) {
if (Stapler.getCurrentRequest2().findAncestorObject(this.getClass()) != null || !Util.isSafeToRedirectTo(url)) {
// Default behavior if the URL is absolute or scheme-relative, or the current object is an ancestor (i.e. would resolve correctly)
menu.add(url, icon, iconXml, text, post, requiresConfirmation, badge, message);
return;
}
// If neither is the case, rewrite the relative URL to point to inside the /manage/ URL space
menu.add("manage/" + url, icon, iconXml, text, post, requiresConfirmation, badge, message);
}
/** Unlike {@link Jenkins#getActiveAdministrativeMonitors} this checks for activation lazily. */
@Override
public Badge getBadge() {
if (!(AdministrativeMonitor.hasPermissionToDisplay())) {
return null;
}
var app = Jenkins.get().getServletContext().getAttribute("app");
if (app instanceof HudsonIsLoading || app instanceof HudsonIsRestarting) {
return null;
}
if (Jenkins.get().administrativeMonitors.stream().anyMatch(m -> m.isSecurity() && isActive(m))) {
return new Badge("1+", Messages.ManageJenkinsAction_notifications(),
Badge.Severity.DANGER);
} else if (Jenkins.get().administrativeMonitors.stream().anyMatch(m -> !m.isSecurity() && isActive(m))) {
return new Badge("1+", Messages.ManageJenkinsAction_notifications(),
Badge.Severity.WARNING);
} else {
return null;
}
}
private static boolean isActive(AdministrativeMonitor m) {
try {
return !m.isActivationFake() && m.hasRequiredPermission() && m.isEnabled() && m.isActivated();
} catch (Throwable x) {
LOGGER.log(Level.WARNING, null, x);
return false;
}
}
}