1414import java .io .IOException ;
1515import java .time .LocalDateTime ;
1616import java .util .ArrayList ;
17- import java .util .Arrays ;
1817import java .util .Collections ;
1918import java .util .HashMap ;
2019import java .util .List ;
@@ -64,33 +63,12 @@ public boolean isVisible() {
6463 if (!MaintenanceHelper .getInstance ().isValidTarget (target .toKey ())) {
6564 return false ;
6665 }
67-
68- if (isAgent ()) {
69- Computer computer = Jenkins .get ().getComputer (target .getName ());
70- return computer != null
71- && (computer .hasPermission (Computer .DISCONNECT )
72- || computer .hasPermission (Computer .CONFIGURE )
73- || computer .hasPermission (Computer .EXTENDED_READ ))
74- && computer .getNode () != null ;
75- }
76- return Jenkins .get ().hasPermission (Jenkins .ADMINISTER );
66+ return PermissionManager .canView (target );
7767 } catch (IOException e ) {
7868 return false ;
7969 }
8070 }
8171
82- protected void checkPermission (Permission ... permissions ) {
83- if (isAgent ()) {
84- Computer c = Jenkins .get ().getComputer (target .getName ());
85- if (c == null ) {
86- throw new IllegalStateException ("Agent '" + target .getName () + "' no longer exists" );
87- }
88- c .checkAnyPermission (permissions );
89- } else { // For cloud
90- Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
91- }
92- }
93-
9472 @ Override
9573 public String getIconFileName () {
9674 if (isVisible ()) {
@@ -103,7 +81,7 @@ public String getIconFileName() {
10381 @ Override
10482 public String getDisplayName () {
10583 if (isVisible ()) {
106- if (hasPermissions ( )) {
84+ if (PermissionManager . canModify ( target )) {
10785 return Messages .MaintenanceAction_maintenanceWindows ();
10886 } else {
10987 return Messages .MaintenanceAction_view ();
@@ -182,58 +160,30 @@ public Computer getAgentComputer() {
182160 }
183161
184162 /**
185- * Checks if the user has permissions to access MaintenanceWindows .
163+ * Checks if the user can view maintenance windows for this target (called by jelly) .
186164 *
187- * @return true if they do .
165+ * @return true if they can view .
188166 */
189167 public boolean hasPermissions () {
190- if (isAgent ()) {
191- Computer c = Jenkins .get ().getComputer (target .getName ());
192- return c != null
193- && (c .hasPermission (Computer .DISCONNECT )
194- || c .hasPermission (Computer .CONFIGURE )
195- || c .hasPermission (Computer .EXTENDED_READ ));
196- } else {
197- return Jenkins .get ().hasPermission (Jenkins .ADMINISTER );
198- }
168+ return PermissionManager .canView (target );
199169 }
200170
201171 /**
202- * Checks the given permissions .
172+ * Checks if the user can add or edit maintenance windows for this target (called by jelly) .
203173 *
204- * @param permissions A group of permissions to be checked.
205- * @return true if all permissions are granted.
174+ * @return true if they can modify.
206175 */
207- public boolean hasPermissions (Permission ... permissions ) {
208- if (isAgent ()) {
209- Computer c = Jenkins .get ().getComputer (target .getName ());
210- return c != null && Arrays .stream (permissions ).allMatch (c ::hasPermission );
211- } else {
212- return Jenkins .get ().hasPermission (Jenkins .ADMINISTER );
213- }
176+ public boolean hasModifyPermission () {
177+ return PermissionManager .canModify (target );
214178 }
215179
216180 /**
217- * Checks if the user has permissions to delete MaintenanceWindows .
181+ * Checks if the user can delete maintenance windows for this target .
218182 *
219- * @return true if they do .
183+ * @return true if they can delete .
220184 */
221185 public boolean hasDeletePermission () {
222- if (isAgent ()) {
223- Computer c = Jenkins .get ().getComputer (target .getName ());
224- return c != null && (c .hasPermission (Computer .DISCONNECT ) || c .hasPermission (Computer .CONFIGURE ));
225- } else {
226- return Jenkins .get ().hasPermission (Jenkins .ADMINISTER );
227- }
228- }
229-
230- /**
231- * Checks if the user has permissions to delete Cloud windows.
232- *
233- * @return true if they do.
234- */
235- public boolean hasCloudDeletePermission () {
236- return Jenkins .get ().hasPermission (Jenkins .ADMINISTER );
186+ return PermissionManager .canDelete (target );
237187 }
238188
239189 /**
@@ -321,7 +271,7 @@ public Set<RecurringMaintenanceWindow> getRecurringMaintenanceWindows() {
321271 */
322272 @ POST
323273 public HttpResponse doAdd (StaplerRequest2 req ) throws IOException , ServletException {
324- checkPermission ( CONFIGURE_AND_DISCONNECT );
274+ PermissionManager . checkCanModify ( target );
325275
326276 JSONObject src = req .getSubmittedForm ();
327277 MaintenanceWindow mw = req .bindJSON (MaintenanceWindow .class , src );
@@ -339,7 +289,7 @@ public HttpResponse doAdd(StaplerRequest2 req) throws IOException, ServletExcept
339289 */
340290 @ POST
341291 public void doAddRecurring (StaplerRequest2 req , StaplerResponse2 rsp ) throws IOException , ServletException {
342- checkPermission ( CONFIGURE_AND_DISCONNECT );
292+ PermissionManager . checkCanModify ( target );
343293
344294 JSONObject src = req .getSubmittedForm ();
345295 RecurringMaintenanceWindow rmw = req .bindJSON (RecurringMaintenanceWindow .class , src );
@@ -354,7 +304,7 @@ public void doAddRecurring(StaplerRequest2 req, StaplerResponse2 rsp) throws IOE
354304 */
355305 @ JavaScriptMethod
356306 public String [] deleteMultiple (String [] ids ) {
357- checkPermission ( CONFIGURE_AND_DISCONNECT );
307+ PermissionManager . checkCanDelete ( target );
358308 List <String > deletedList = new ArrayList <>();
359309 for (String id : ids ) {
360310 try {
@@ -375,7 +325,7 @@ public String[] deleteMultiple(String[] ids) {
375325 @ JavaScriptMethod
376326 public Map <String , Boolean > getMaintenanceStatus () {
377327 Map <String , Boolean > statusList = new HashMap <>();
378- if (hasPermissions ( )) {
328+ if (PermissionManager . canView ( target )) {
379329 try {
380330 for (MaintenanceWindow mw : MaintenanceHelper .getInstance ().getMaintenanceWindows (target .toKey ())) {
381331 if (!mw .isMaintenanceOver ()) {
@@ -396,7 +346,7 @@ public Map<String, Boolean> getMaintenanceStatus() {
396346 */
397347 @ JavaScriptMethod
398348 public String [] deleteMultipleRecurring (String [] ids ) {
399- checkPermission ( CONFIGURE_AND_DISCONNECT );
349+ PermissionManager . checkCanDelete ( target );
400350 List <String > deletedList = new ArrayList <>();
401351 for (String id : ids ) {
402352 try {
@@ -417,7 +367,7 @@ public String[] deleteMultipleRecurring(String[] ids) {
417367 @ JavaScriptMethod
418368 public boolean deleteMaintenance (String id ) {
419369 try {
420- checkPermission ( CONFIGURE_AND_DISCONNECT );
370+ PermissionManager . checkCanDelete ( target );
421371 if (Util .fixEmptyAndTrim (id ) == null ) {
422372 return false ;
423373 }
@@ -442,7 +392,7 @@ public boolean deleteMaintenance(String id) {
442392 @ JavaScriptMethod
443393 public boolean deleteRecurringMaintenance (String id ) {
444394 try {
445- checkPermission ( CONFIGURE_AND_DISCONNECT );
395+ PermissionManager . checkCanDelete ( target );
446396 if (Util .fixEmptyAndTrim (id ) == null ) {
447397 return false ;
448398 }
@@ -469,7 +419,7 @@ public boolean deleteRecurringMaintenance(String id) {
469419 */
470420 @ POST
471421 public synchronized HttpResponse doConfigSubmit (StaplerRequest2 req ) throws IOException , ServletException {
472- checkPermission ( Computer . CONFIGURE );
422+ PermissionManager . checkCanModify ( target );
473423
474424 JSONObject src = req .getSubmittedForm ();
475425
@@ -500,7 +450,7 @@ public synchronized HttpResponse doConfigSubmit(StaplerRequest2 req) throws IOEx
500450 public void doEnable (StaplerResponse2 rsp ) throws IOException {
501451 Computer c = getAgentComputer ();
502452 if (c != null ) {
503- c . checkPermission ( Computer . CONFIGURE );
453+ PermissionManager . checkCanModify ( target );
504454 MaintenanceHelper .getInstance ().injectRetentionStrategy (c );
505455 }
506456 rsp .sendRedirect ("." );
@@ -516,7 +466,7 @@ public void doEnable(StaplerResponse2 rsp) throws IOException {
516466 public void doDisable (StaplerResponse2 rsp ) throws IOException {
517467 Computer c = getAgentComputer ();
518468 if (c != null ) {
519- c . checkPermission ( Computer . CONFIGURE );
469+ PermissionManager . checkCanModify ( target );
520470 MaintenanceHelper .getInstance ().removeRetentionStrategy (c );
521471 }
522472
@@ -536,15 +486,14 @@ public void doIndex(StaplerRequest2 req, StaplerResponse2 rsp)
536486 rsp .sendError (HttpServletResponse .SC_NOT_FOUND ); // 404
537487 return ;
538488 }
539- c .checkAnyPermission (Computer .EXTENDED_READ , Computer .CONFIGURE , Computer .DISCONNECT );
540489 } else {
541490 Cloud cloud = getCloud ();
542491 if (cloud == null ) {
543492 rsp .sendError (HttpServletResponse .SC_NOT_FOUND );
544493 return ;
545494 }
546- Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
547495 }
496+ PermissionManager .checkCanView (target );
548497
549498 req .getView (this , "index.jelly" ).forward (req , rsp );
550499 }
0 commit comments