Skip to content

Commit 4ae70e8

Browse files
committed
Global dismiss will disable for all users
1 parent a057576 commit 4ae70e8

File tree

9 files changed

+163
-120
lines changed

9 files changed

+163
-120
lines changed

core/src/main/java/hudson/model/AdministrativeMonitor.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,42 @@ public final String getSearchUrl() {
124124
}
125125

126126
/**
127-
* Mark this monitor as disabled, to prevent this from showing up in the UI.
127+
* Mark this monitor as disabled for the user, to prevent this from showing up in the UI.
128128
*
129129
* <p>
130-
* This is a per-user setting if security is enabled. This means that it should not be used to disable
131-
* the monitor programmatically.
130+
* This is a per-user setting if security is enabled. Use {@link #disableGlobally(boolean)}
131+
* to disable the monitor for all users.
132132
* </p>
133133
*/
134134
public void disable(boolean value) throws IOException {
135-
AbstractCIBase jenkins = Jenkins.get();
136135
User user = User.current();
137136

138137
if (user != null) {
139138
AdministrativeMonitorsProperty property = AdministrativeMonitorsProperty.get(user);
140139
property.disableMonitor(id, value);
141140
} else {
142-
Set<String> set = jenkins.getDisabledAdministrativeMonitors();
143-
if (value) {
144-
set.add(id);
145-
} else {
146-
set.remove(id);
147-
}
148-
jenkins.setDisabledAdministrativeMonitors(set);
149-
jenkins.save();
141+
disableGlobally(value);
142+
}
143+
}
144+
145+
/**
146+
* Mark this monitor as disabled globally, to prevent this from showing up in the UI for all users.
147+
*
148+
* <p>
149+
* This is a global setting. A monitor that is disabled globally will not show up in the UI for any user,
150+
* regardless of their per-user setting.
151+
* </p>
152+
*/
153+
public void disableGlobally(boolean value) throws IOException {
154+
AbstractCIBase jenkins = Jenkins.get();
155+
Set<String> set = jenkins.getDisabledAdministrativeMonitors();
156+
if (value) {
157+
set.add(id);
158+
} else {
159+
set.remove(id);
150160
}
161+
jenkins.setDisabledAdministrativeMonitors(set);
162+
jenkins.save();
151163
}
152164

153165
/**
@@ -157,18 +169,28 @@ public void disable(boolean value) throws IOException {
157169
* This flag implements the ability for the admin to say "no, thank you" to the monitor that
158170
* he wants to ignore.
159171
* </p>
160-
* <p>
161-
* This is a per-user setting if security is enabled. This means that it should not be used to decide if some
162-
* check should be performed or not.
163-
* If such behavior is desired, implementers should provide their own configuration mechanism.
164-
* </p>
172+
* @return true if this monitor is enabled, false if disabled globally or by the user.
165173
*/
166174
public boolean isEnabled() {
167175
User user = User.current();
176+
boolean enabledGlobally = isEnabledGlobally();
168177
if (user != null) {
169178
AdministrativeMonitorsProperty property = AdministrativeMonitorsProperty.get(user);
170-
return property.isMonitorEnabled(id);
179+
return property.isMonitorEnabled(id) && enabledGlobally;
171180
}
181+
return enabledGlobally;
182+
}
183+
184+
/**
185+
* Returns true if this monitor is enabled globally, regardless of per-user settings.
186+
*
187+
* <p>
188+
* Monitors that want to avoid execution should use this check.
189+
* </p>
190+
*
191+
* @return true if enabled globally, false otherwise.
192+
*/
193+
public boolean isEnabledGlobally() {
172194
return !Jenkins.get().getDisabledAdministrativeMonitors().contains(id);
173195
}
174196

core/src/main/java/hudson/util/DoubleLaunchChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected void execute() {
9595
File timestampFile = new File(home, ".owner");
9696

9797
long t = timestampFile.lastModified();
98-
if (t != 0 && lastWriteTime != 0 && t != lastWriteTime) {
98+
if (t != 0 && lastWriteTime != 0 && t != lastWriteTime && isEnabledGlobally()) {
9999
try {
100100
collidingId = Files.readString(Util.fileToPath(timestampFile), Charset.defaultCharset());
101101
} catch (IOException e) {

core/src/main/java/jenkins/monitor/OperatingSystemEndOfLifeAdminMonitor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public OperatingSystemEndOfLifeAdminMonitor() throws IOException {
9191
}
9292

9393
private void fillOperatingSystemList() throws IOException {
94-
if (Jenkins.getInstanceOrNull() != null) {
95-
/* If not enabled, do not read the data files or perform any checks */
94+
if (Jenkins.getInstanceOrNull() != null && !isEnabledGlobally()) {
9695
LOGGER.log(Level.FINEST, "Operating system end of life monitor is not enabled, reading no data");
9796
return;
9897
}

core/src/main/java/jenkins/user/AdministrativeMonitorsProperty.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Set;
1616
import java.util.logging.Level;
1717
import java.util.logging.Logger;
18+
import jenkins.model.Jenkins;
1819
import net.sf.json.JSONArray;
1920
import net.sf.json.JSONObject;
2021
import org.jenkinsci.Symbol;
@@ -68,6 +69,10 @@ public boolean isMonitorEnabled(String monitorId) {
6869
}
6970
}
7071

72+
public boolean isMonitorGloballyDisabled(String monitorId) {
73+
return Jenkins.get().getDisabledAdministrativeMonitors().contains(monitorId);
74+
}
75+
7176
public void disableMonitor(String monitorId, boolean enabled) throws IOException {
7277
synchronized (dismissedMonitors) {
7378
if (enabled) {

core/src/main/resources/jenkins/management/AdministrativeMonitorsConfiguration/config.groovy

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,24 @@ import hudson.model.AdministrativeMonitor
2929
f = namespace(lib.FormTagLib)
3030
st = namespace("jelly:stapler")
3131

32-
div(class: "${app.useSecurity ? 'jenkins-hidden' : null}") {
33-
f.section(title: _("Administrative monitors"), description: _("blurb")) {
34-
f.advanced(title: _("Administrative monitors")) {
35-
f.entry() {
36-
for (AdministrativeMonitor am : new ArrayList<>(AdministrativeMonitor.all())
37-
.sort({ o1, o2 -> o1.getDisplayName() <=> o2.getDisplayName() })) {
38-
div(style: "margin-bottom: 0.625rem") {
39-
div(class: "jenkins-checkbox-help-wrapper") {
40-
f.checkbox(name: "administrativeMonitor",
41-
title: am.displayName,
42-
checked: instance.isMonitorEnabled(am.id),
43-
json: am.id)
44-
if (am.isSecurity()) {
45-
span(style: 'margin-left: 0.5rem', class: 'jenkins-badge', _("Security"))
46-
}
47-
}
48-
div(class: "jenkins-checkbox__description") {
49-
st.include(it: am, page: "description", optional: true)
32+
f.section(title: _("Administrative monitors"), description: _("blurb")) {
33+
f.advanced(title: _("Administrative monitors")) {
34+
f.entry() {
35+
for (AdministrativeMonitor am : new ArrayList<>(AdministrativeMonitor.all())
36+
.sort({ o1, o2 -> o1.getDisplayName() <=> o2.getDisplayName() })) {
37+
div(style: "margin-bottom: 0.625rem") {
38+
div(class: "jenkins-checkbox-help-wrapper") {
39+
f.checkbox(name: "administrativeMonitor",
40+
title: am.displayName,
41+
checked: instance.isMonitorEnabled(am.id),
42+
json: am.id)
43+
if (am.isSecurity()) {
44+
span(style: 'margin-left: 0.5rem', class: 'jenkins-badge', _("Security"))
5045
}
5146
}
47+
div(class: "jenkins-checkbox__description") {
48+
st.include(it: am, page: "description", optional: true)
49+
}
5250
}
5351
}
5452
}

core/src/main/resources/jenkins/management/AdministrativeMonitorsConfiguration/config.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ blurb = Administrative monitors are warnings shown to Jenkins administrators \
22
about the state of the Jenkins instance. It is generally strongly \
33
recommended to keep all administrative monitors enabled, but if you are \
44
not interested in specific warnings, uncheck them here to permanently \
5-
hide them.
5+
hide them for all administrators.

core/src/main/resources/jenkins/user/AdministrativeMonitorsProperty/config.jelly

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
<div class="jenkins-!-margin-bottom-1">
88
<div class="jenkins-checkbox-help-wrapper">
99
<j:set var="readOnlyMode" value="${!am.hasRequiredPermission()}"/>
10-
<f:checkbox name="administrativeMonitor" title="${am.displayName}" json="${am.id}" checked="${instance.isMonitorEnabled(am.id)}"/>
10+
<f:checkbox name="administrativeMonitor" title="${am.displayName}" json="${am.id}"
11+
checked="${instance.isMonitorEnabled(am.id)}"/>
12+
<j:if test="${instance.isMonitorGloballyDisabled(am.id)}">
13+
<sup style="margin-left: 0.1rem" class="jenkins-!-warning-color" tooltip="${%This monitor is globally disabled}">*</sup>
14+
</j:if>
1115
<j:if test="${am.security}">
1216
<span style="margin-left: 0.5rem" class="jenkins-badge">${%Security}</span>
1317
</j:if>

core/src/main/resources/jenkins/user/AdministrativeMonitorsProperty/config.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ blurb = Administrative monitors are warnings shown to Jenkins administrators \
22
about the state of the Jenkins instance. It is generally strongly \
33
recommended to keep all administrative monitors enabled, but if you are \
44
not interested in specific warnings, uncheck them here to permanently \
5-
hide them.
5+
hide them for yourself. Some monitors are only shown when you have the \
6+
<em>Administer</em> permission, while other are also accessible with \
7+
<em>Manage</em> or <em>SystemRead</em> permissions. You can disable only \
8+
the monitors that are accessible with your permissions.

0 commit comments

Comments
 (0)