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 ;
@@ -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 }
0 commit comments