Skip to content

Commit b6e1ace

Browse files
Integrated the new PermissionManager API to the existing structure
Signed-off-by: Pratham Vaghela <prathamcomeslast@gmail.com>
1 parent 71870b1 commit b6e1ace

File tree

6 files changed

+126
-123
lines changed

6 files changed

+126
-123
lines changed

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/MaintenanceAction.java

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.IOException;
1515
import java.time.LocalDateTime;
1616
import java.util.ArrayList;
17-
import java.util.Arrays;
1817
import java.util.Collections;
1918
import java.util.HashMap;
2019
import java.util.List;
@@ -61,33 +60,12 @@ public boolean isVisible() {
6160
if (!MaintenanceHelper.getInstance().isValidTarget(target.toKey())) {
6261
return false;
6362
}
64-
65-
if (isAgent()) {
66-
Computer computer = Jenkins.get().getComputer(target.getName());
67-
return computer != null
68-
&& (computer.hasPermission(Computer.DISCONNECT)
69-
|| computer.hasPermission(Computer.CONFIGURE)
70-
|| computer.hasPermission(Computer.EXTENDED_READ))
71-
&& computer.getNode() != null;
72-
}
73-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
63+
return PermissionManager.canView(target);
7464
} catch (IOException e) {
7565
return false;
7666
}
7767
}
7868

79-
protected void checkPermission(Permission... permissions) {
80-
if (isAgent()) {
81-
Computer c = Jenkins.get().getComputer(target.getName());
82-
if (c == null) {
83-
throw new IllegalStateException("Agent '" + target.getName() + "' no longer exists");
84-
}
85-
c.checkAnyPermission(permissions);
86-
} else { // For cloud
87-
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
88-
}
89-
}
90-
9169
@Override
9270
public String getIconFileName() {
9371
if (isVisible()) {
@@ -100,7 +78,7 @@ public String getIconFileName() {
10078
@Override
10179
public String getDisplayName() {
10280
if (isVisible()) {
103-
if (hasPermissions()) {
81+
if (PermissionManager.canModify(target)) {
10482
return Messages.MaintenanceAction_maintenanceWindows();
10583
} else {
10684
return Messages.MaintenanceAction_view();
@@ -179,58 +157,30 @@ public Computer getAgentComputer() {
179157
}
180158

181159
/**
182-
* Checks if the user has permissions to access MaintenanceWindows.
160+
* Checks if the user can view maintenance windows for this target (called by jelly).
183161
*
184-
* @return true if they do.
162+
* @return true if they can view.
185163
*/
186164
public boolean hasPermissions() {
187-
if (isAgent()) {
188-
Computer c = Jenkins.get().getComputer(target.getName());
189-
return c != null
190-
&& (c.hasPermission(Computer.DISCONNECT)
191-
|| c.hasPermission(Computer.CONFIGURE)
192-
|| c.hasPermission(Computer.EXTENDED_READ));
193-
} else {
194-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
195-
}
165+
return PermissionManager.canView(target);
196166
}
197167

198168
/**
199-
* Checks the given permissions.
169+
* Checks if the user can add or edit maintenance windows for this target (called by jelly).
200170
*
201-
* @param permissions A group of permissions to be checked.
202-
* @return true if all permissions are granted.
171+
* @return true if they can modify.
203172
*/
204-
public boolean hasPermissions(Permission... permissions) {
205-
if (isAgent()) {
206-
Computer c = Jenkins.get().getComputer(target.getName());
207-
return c != null && Arrays.stream(permissions).allMatch(c::hasPermission);
208-
} else {
209-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
210-
}
173+
public boolean hasModifyPermission() {
174+
return PermissionManager.canModify(target);
211175
}
212176

213177
/**
214-
* Checks if the user has permissions to delete MaintenanceWindows.
178+
* Checks if the user can delete maintenance windows for this target.
215179
*
216-
* @return true if they do.
180+
* @return true if they can delete.
217181
*/
218182
public boolean hasDeletePermission() {
219-
if (isAgent()) {
220-
Computer c = Jenkins.get().getComputer(target.getName());
221-
return c != null && (c.hasPermission(Computer.DISCONNECT) || c.hasPermission(Computer.CONFIGURE));
222-
} else {
223-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
224-
}
225-
}
226-
227-
/**
228-
* Checks if the user has permissions to delete Cloud windows.
229-
*
230-
* @return true if they do.
231-
*/
232-
public boolean hasCloudDeletePermission() {
233-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
183+
return PermissionManager.canDelete(target);
234184
}
235185

236186
/**
@@ -318,7 +268,7 @@ public Set<RecurringMaintenanceWindow> getRecurringMaintenanceWindows() {
318268
*/
319269
@POST
320270
public HttpResponse doAdd(StaplerRequest2 req) throws IOException, ServletException {
321-
checkPermission(CONFIGURE_AND_DISCONNECT);
271+
PermissionManager.checkCanModify(target);
322272

323273
JSONObject src = req.getSubmittedForm();
324274
MaintenanceWindow mw = req.bindJSON(MaintenanceWindow.class, src);
@@ -336,7 +286,7 @@ public HttpResponse doAdd(StaplerRequest2 req) throws IOException, ServletExcept
336286
*/
337287
@POST
338288
public void doAddRecurring(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException {
339-
checkPermission(CONFIGURE_AND_DISCONNECT);
289+
PermissionManager.checkCanModify(target);
340290

341291
JSONObject src = req.getSubmittedForm();
342292
RecurringMaintenanceWindow rmw = req.bindJSON(RecurringMaintenanceWindow.class, src);
@@ -351,7 +301,7 @@ public void doAddRecurring(StaplerRequest2 req, StaplerResponse2 rsp) throws IOE
351301
*/
352302
@JavaScriptMethod
353303
public String[] deleteMultiple(String[] ids) {
354-
checkPermission(CONFIGURE_AND_DISCONNECT);
304+
PermissionManager.checkCanDelete(target);
355305
List<String> deletedList = new ArrayList<>();
356306
for (String id : ids) {
357307
try {
@@ -372,7 +322,7 @@ public String[] deleteMultiple(String[] ids) {
372322
@JavaScriptMethod
373323
public Map<String, Boolean> getMaintenanceStatus() {
374324
Map<String, Boolean> statusList = new HashMap<>();
375-
if (hasPermissions()) {
325+
if (PermissionManager.canView(target)) {
376326
try {
377327
for (MaintenanceWindow mw : MaintenanceHelper.getInstance().getMaintenanceWindows(target.toKey())) {
378328
if (!mw.isMaintenanceOver()) {
@@ -393,7 +343,7 @@ public Map<String, Boolean> getMaintenanceStatus() {
393343
*/
394344
@JavaScriptMethod
395345
public String[] deleteMultipleRecurring(String[] ids) {
396-
checkPermission(CONFIGURE_AND_DISCONNECT);
346+
PermissionManager.checkCanDelete(target);
397347
List<String> deletedList = new ArrayList<>();
398348
for (String id : ids) {
399349
try {
@@ -414,7 +364,7 @@ public String[] deleteMultipleRecurring(String[] ids) {
414364
@JavaScriptMethod
415365
public boolean deleteMaintenance(String id) {
416366
try {
417-
checkPermission(CONFIGURE_AND_DISCONNECT);
367+
PermissionManager.checkCanDelete(target);
418368
if (Util.fixEmptyAndTrim(id) == null) {
419369
return false;
420370
}
@@ -439,7 +389,7 @@ public boolean deleteMaintenance(String id) {
439389
@JavaScriptMethod
440390
public boolean deleteRecurringMaintenance(String id) {
441391
try {
442-
checkPermission(CONFIGURE_AND_DISCONNECT);
392+
PermissionManager.checkCanDelete(target);
443393
if (Util.fixEmptyAndTrim(id) == null) {
444394
return false;
445395
}
@@ -466,7 +416,7 @@ public boolean deleteRecurringMaintenance(String id) {
466416
*/
467417
@POST
468418
public synchronized HttpResponse doConfigSubmit(StaplerRequest2 req) throws IOException, ServletException {
469-
checkPermission(Computer.CONFIGURE);
419+
PermissionManager.checkCanModify(target);
470420

471421
JSONObject src = req.getSubmittedForm();
472422

@@ -497,7 +447,7 @@ public synchronized HttpResponse doConfigSubmit(StaplerRequest2 req) throws IOEx
497447
public void doEnable(StaplerResponse2 rsp) throws IOException {
498448
Computer c = getAgentComputer();
499449
if (c != null) {
500-
c.checkPermission(Computer.CONFIGURE);
450+
PermissionManager.checkCanModify(target);
501451
MaintenanceHelper.getInstance().injectRetentionStrategy(c);
502452
}
503453
rsp.sendRedirect(".");
@@ -513,7 +463,7 @@ public void doEnable(StaplerResponse2 rsp) throws IOException {
513463
public void doDisable(StaplerResponse2 rsp) throws IOException {
514464
Computer c = getAgentComputer();
515465
if (c != null) {
516-
c.checkPermission(Computer.CONFIGURE);
466+
PermissionManager.checkCanModify(target);
517467
MaintenanceHelper.getInstance().removeRetentionStrategy(c);
518468
}
519469

@@ -533,10 +483,14 @@ public void doIndex(StaplerRequest2 req, StaplerResponse2 rsp)
533483
rsp.sendError(HttpServletResponse.SC_NOT_FOUND); // 404
534484
return;
535485
}
536-
c.checkAnyPermission(Computer.EXTENDED_READ, Computer.CONFIGURE, Computer.DISCONNECT);
537486
} else {
538-
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
487+
Cloud cloud = getCloud();
488+
if (cloud == null) {
489+
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
490+
return;
491+
}
539492
}
493+
PermissionManager.checkCanView(target);
540494

541495
req.getView(this, "index.jelly").forward(req, rsp);
542496
}

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/MaintenanceLink.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,18 @@ public String getDescription() {
5353

5454
@Override
5555
public String getDisplayName() {
56-
boolean hasClouds = !getCloudTargets().isEmpty();
57-
boolean hasAgents = !getAgentTargets().isEmpty();
56+
List<MaintenanceAction> all = getTargets();
57+
boolean hasAgents = all.stream().anyMatch(MaintenanceAction::isAgent);
58+
boolean hasClouds = all.stream().anyMatch(MaintenanceAction::isCloud);
59+
5860
if (hasAgents && !hasClouds) {
5961
return Messages.MaintenanceLink_displayName_agent();
6062
}
63+
6164
if (hasClouds && !hasAgents) {
6265
return Messages.MaintenanceLink_displayName_cloud();
6366
}
67+
6468
return Messages.MaintenanceLink_displayName();
6569
}
6670

@@ -110,11 +114,13 @@ public List<MaintenanceAction> getTargets() {
110114
}
111115
}
112116

113-
return targetList;
117+
return targetList.stream()
118+
.filter(action -> PermissionManager.canView(action.getTarget()))
119+
.toList();
114120
}
115121

116122
/**
117-
* Gets all Agents' maintenance actions.
123+
* Gets all Agents' maintenance actions (called by jelly).
118124
*
119125
* @return List of all Agent actions.
120126
*/
@@ -126,7 +132,7 @@ public List<MaintenanceAction> getAgentTargets() {
126132
}
127133

128134
/**
129-
* Gets all Clouds' maintenance actions.
135+
* Gets all Clouds' maintenance actions (called by jelly).
130136
*
131137
* @return List of all Cloud actions.
132138
*/
@@ -222,7 +228,8 @@ private String getVerb(int count, String target) {
222228
*/
223229
@JavaScriptMethod
224230
public boolean deleteMaintenance(String id, String targetKey) {
225-
if (hasPermission(targetKey)) {
231+
MaintenanceTarget target = MaintenanceTarget.fromKey(targetKey);
232+
if (PermissionManager.canDelete(target)) {
226233
try {
227234
MaintenanceHelper.getInstance().deleteMaintenanceWindow(targetKey, id);
228235
return true;
@@ -245,7 +252,8 @@ public String[] deleteMultiple(JSONObject json) {
245252
List<String> deletedList = new ArrayList<>();
246253
for (Entry<String, String> entry : mwList.entrySet()) {
247254
String targetKey = entry.getValue();
248-
if (hasPermission(targetKey)) {
255+
MaintenanceTarget target = MaintenanceTarget.fromKey(targetKey);
256+
if (PermissionManager.canDelete(target)) {
249257
String id = entry.getKey();
250258
try {
251259
MaintenanceHelper.getInstance().deleteMaintenanceWindow(targetKey, id);
@@ -268,16 +276,14 @@ public Map<String, Boolean> getMaintenanceStatus() {
268276
Map<String, Boolean> statusList = new HashMap<>();
269277
for (MaintenanceAction action : getTargets()) {
270278
try {
271-
if (!action.hasPermissions()) {
279+
if (!PermissionManager.canView(action.getTarget())) {
272280
continue;
273281
}
274282

275283
MaintenanceTarget target = action.getTarget();
276-
if (target != null) {
277-
for (MaintenanceWindow mw : MaintenanceHelper.getInstance().getMaintenanceWindows(target.toKey())) {
278-
if (!mw.isMaintenanceOver()) {
279-
statusList.put(mw.getId(), mw.isMaintenanceScheduled());
280-
}
284+
for (MaintenanceWindow mw : MaintenanceHelper.getInstance().getMaintenanceWindows(target.toKey())) {
285+
if (!mw.isMaintenanceOver()) {
286+
statusList.put(mw.getId(), mw.isMaintenanceScheduled());
281287
}
282288
}
283289
} catch (IOException ioe) {
@@ -287,11 +293,6 @@ public Map<String, Boolean> getMaintenanceStatus() {
287293
return statusList;
288294
}
289295

290-
private boolean hasPermission(String targetKey) {
291-
MaintenanceAction action = new MaintenanceAction(MaintenanceTarget.fromKey(targetKey));
292-
return action.hasPermissions();
293-
}
294-
295296
@Restricted(NoExternalUse.class)
296297
public FormValidation doCheckLabel(@QueryParameter String value) {
297298
return LabelExpression.validate(value);

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/PermissionManager.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hudson.model.Computer;
44
import hudson.security.AccessDeniedException3;
55
import hudson.security.Permission;
6+
import java.util.Arrays;
67
import jenkins.model.Jenkins;
78

89
/**
@@ -58,6 +59,28 @@ public static boolean canDelete(MaintenanceTarget target) {
5859
return canModify(target); // same threshold for now
5960
}
6061

62+
/**
63+
* Returns true if the current user has the given permissions for this target.
64+
*
65+
* @param target the target to check permissions for
66+
* @param checkAll if true, all permissions must be granted; otherwise any permission is sufficient
67+
* @param permissions the permissions to check for
68+
*
69+
* @return true if the user has the given permissions
70+
*/
71+
public static boolean hasPermissions(MaintenanceTarget target, boolean checkAll, Permission... permissions) {
72+
return switch (target.getType()) {
73+
case AGENT -> {
74+
Computer c = getComputer(target);
75+
yield c != null
76+
&& (checkAll ? Arrays.stream(permissions).allMatch(c::hasPermission)
77+
: Arrays.stream(permissions).anyMatch(c::hasPermission));
78+
}
79+
case CLOUD -> checkAll ? Arrays.stream(permissions).allMatch(Jenkins.get()::hasPermission)
80+
: Arrays.stream(permissions).anyMatch(Jenkins.get()::hasPermission);
81+
};
82+
}
83+
6184
/**
6285
* Throws AccessDeniedException if the user cannot VIEW.
6386
*/
@@ -91,6 +114,31 @@ public static void checkCanDelete(MaintenanceTarget target) {
91114
}
92115
}
93116

117+
/**
118+
* Throws AccessDeniedException if the user does not have the given permissions.
119+
*
120+
* @param target the target to check permissions for
121+
* @param checkAll if true, all permissions must be granted; otherwise any permission is sufficient
122+
* @param permissions the permissions to check for
123+
*
124+
* @throws AccessDeniedException3 if the user does not have the required permissions
125+
*/
126+
public static void checkHasPermissions(MaintenanceTarget target, boolean checkAll, Permission... permissions) {
127+
if (hasPermissions(target, checkAll, permissions)) {
128+
return;
129+
}
130+
131+
if (checkAll) {
132+
Permission missing = Arrays.stream(permissions)
133+
.filter(p -> !hasPermissions(target, true, p))
134+
.findFirst()
135+
.orElse(permissions[0]);
136+
throwDenied(missing);
137+
} else {
138+
throwDenied(permissions[0]);
139+
}
140+
}
141+
94142
private static Computer getComputer(MaintenanceTarget target) {
95143
return Jenkins.get().getComputer(target.getName());
96144
}

0 commit comments

Comments
 (0)