|
| 1 | +package com.sap.prd.jenkins.plugins.agent_maintenance; |
| 2 | + |
| 3 | +import hudson.model.Computer; |
| 4 | +import hudson.security.AccessDeniedException3; |
| 5 | +import hudson.security.Permission; |
| 6 | +import jenkins.model.Jenkins; |
| 7 | + |
| 8 | +/** |
| 9 | + * Centralized permission manager for maintenance targets. |
| 10 | + * <p> |
| 11 | + * Permission rules: |
| 12 | + * <ul> |
| 13 | + * <li>AGENT view: Computer.EXTENDED_READ | Computer.CONFIGURE | Computer.DISCONNECT</li> |
| 14 | + * <li>AGENT modify/delete: Computer.CONFIGURE | Computer.DISCONNECT</li> |
| 15 | + * <li>CLOUD view: Jenkins.SYSTEM_READ | Jenkins.ADMINISTER</li> |
| 16 | + * <li>CLOUD modify/delete: Jenkins.ADMINISTER only</li> |
| 17 | + * </ul> |
| 18 | + */ |
| 19 | +public final class PermissionManager { |
| 20 | + |
| 21 | + private PermissionManager() {} |
| 22 | + |
| 23 | + /** |
| 24 | + * Returns true if the current user can VIEW maintenance windows for this target. |
| 25 | + */ |
| 26 | + public static boolean canView(MaintenanceTarget target) { |
| 27 | + return switch (target.getType()) { |
| 28 | + case AGENT -> { |
| 29 | + Computer c = getComputer(target); |
| 30 | + yield c != null |
| 31 | + && (c.hasPermission(Computer.EXTENDED_READ) |
| 32 | + || c.hasPermission(Computer.CONFIGURE) |
| 33 | + || c.hasPermission(Computer.DISCONNECT)); |
| 34 | + } |
| 35 | + case CLOUD -> Jenkins.get().hasPermission(Jenkins.SYSTEM_READ); |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns true if the current user can ADD or EDIT maintenance windows for this target. |
| 41 | + */ |
| 42 | + public static boolean canModify(MaintenanceTarget target) { |
| 43 | + return switch (target.getType()) { |
| 44 | + case AGENT -> { |
| 45 | + Computer c = getComputer(target); |
| 46 | + yield c != null |
| 47 | + && (c.hasPermission(Computer.CONFIGURE) |
| 48 | + || c.hasPermission(Computer.DISCONNECT)); |
| 49 | + } |
| 50 | + case CLOUD -> Jenkins.get().hasPermission(Jenkins.ADMINISTER); |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns true if the current user can DELETE maintenance windows for this target. |
| 56 | + */ |
| 57 | + public static boolean canDelete(MaintenanceTarget target) { |
| 58 | + return canModify(target); // same threshold for now |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Throws AccessDeniedException if the user cannot VIEW. |
| 63 | + */ |
| 64 | + public static void checkCanView(MaintenanceTarget target) { |
| 65 | + if (!canView(target)) { |
| 66 | + throwDenied(target.getType() == MaintenanceTarget.TargetType.CLOUD |
| 67 | + ? Jenkins.SYSTEM_READ |
| 68 | + : Computer.EXTENDED_READ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Throws AccessDeniedException if the user cannot MODIFY. |
| 74 | + */ |
| 75 | + public static void checkCanModify(MaintenanceTarget target) { |
| 76 | + if (!canModify(target)) { |
| 77 | + throwDenied(target.getType() == MaintenanceTarget.TargetType.CLOUD |
| 78 | + ? Jenkins.ADMINISTER |
| 79 | + : Computer.CONFIGURE); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Throws AccessDeniedException if the user cannot DELETE. |
| 85 | + */ |
| 86 | + public static void checkCanDelete(MaintenanceTarget target) { |
| 87 | + if (!canDelete(target)) { |
| 88 | + throwDenied(target.getType() == MaintenanceTarget.TargetType.CLOUD |
| 89 | + ? Jenkins.ADMINISTER |
| 90 | + : Computer.CONFIGURE); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static Computer getComputer(MaintenanceTarget target) { |
| 95 | + return Jenkins.get().getComputer(target.getName()); |
| 96 | + } |
| 97 | + |
| 98 | + private static void throwDenied(Permission required) { |
| 99 | + throw new AccessDeniedException3(Jenkins.getAuthentication2(), required); |
| 100 | + } |
| 101 | +} |
0 commit comments