From 079b05cb1f3a3a4a432119bf4d2e4ead514e6393 Mon Sep 17 00:00:00 2001 From: Suresh Kumar P Date: Sun, 14 Aug 2016 12:32:50 +0530 Subject: [PATCH] Added support for View permissions at the View level rather than only at global level --- .../RoleBasedAuthorizationStrategy.java | 22 +- .../rolestrategy/RoleStrategyConfig.java | 4 + .../plugins/rolestrategy/RoleType.java | 9 +- .../RoleStrategyConfig/assign-roles.jelly | 7 + .../assign-view-roles.jelly | 145 ++++++++++++ .../RoleStrategyConfig/manage-roles.jelly | 8 + .../manage-view-roles.jelly | 210 ++++++++++++++++++ 7 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-view-roles.jelly create mode 100644 src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-view-roles.jelly diff --git a/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java index ee82f8af..ac9f5f0c 100644 --- a/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java +++ b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java @@ -80,6 +80,7 @@ public class RoleBasedAuthorizationStrategy extends AuthorizationStrategy { public final static String GLOBAL = "globalRoles"; public final static String PROJECT = "projectRoles"; public final static String SLAVE = "slaveRoles"; + public final static String VIEW = "viewRoles"; public final static String MACRO_ROLE = "roleMacros"; public final static String MACRO_USER = "userMacros"; @@ -138,6 +139,17 @@ public ACL getACL(AbstractItem project) { public ACL getACL(Computer computer) { return getACL(SLAVE, computer.getName(), RoleType.Slave, computer); } + + @Override + public ACL getACL(View view) { + return getACL(VIEW, getViewFullName(view), RoleType.View, view); + } + + String getViewFullName(View view) { + String n = view.getOwnerItemGroup().getFullName(); + if(n.length()==0) return view.getViewName(); + else return n+'/'+ view.getViewName(); + } /** * Used by the container realm. @@ -449,7 +461,7 @@ public AuthorizationStrategy newInstance(StaplerRequest req, JSONObject formData // If the form contains data, it means the method has been called by plugin // specifics forms, and we need to handle it. - if (formData.has(GLOBAL) && formData.has(PROJECT) && formData.has(SLAVE) && oldStrategy instanceof RoleBasedAuthorizationStrategy) { + if (formData.has(GLOBAL) && formData.has(PROJECT) && formData.has(SLAVE) && formData.has(VIEW) && oldStrategy instanceof RoleBasedAuthorizationStrategy) { strategy = new RoleBasedAuthorizationStrategy(); JSONObject globalRoles = formData.getJSONObject(GLOBAL); @@ -478,6 +490,7 @@ public AuthorizationStrategy newInstance(StaplerRequest req, JSONObject formData ReadRoles(formData, PROJECT, strategy, (RoleBasedAuthorizationStrategy)oldStrategy); ReadRoles(formData, SLAVE, strategy, (RoleBasedAuthorizationStrategy)oldStrategy); + ReadRoles(formData, VIEW, strategy, (RoleBasedAuthorizationStrategy)oldStrategy); } // When called from Hudson Manage panel, but was already on a role-based strategy else if(oldStrategy instanceof RoleBasedAuthorizationStrategy) { @@ -592,6 +605,10 @@ else if (type.equals(SLAVE)) { groups.remove(PermissionGroup.get(SCM.class)); groups.remove(PermissionGroup.get(Run.class)); } + else if (type.equals(VIEW)) { + groups = new ArrayList(); + groups.add(PermissionGroup.get(View.class)); + } else { groups = null; } @@ -611,6 +628,9 @@ else if(type.equals(PROJECT)) { else if (type.equals(SLAVE)) { return p!=Computer.CREATE && p.getEnabled(); } + else if (type.equals(VIEW)) { + return p!=View.CREATE && p.getEnabled(); + } else { return false; } diff --git a/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig.java b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig.java index 4658e9f1..aa7777ce 100644 --- a/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig.java +++ b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig.java @@ -177,4 +177,8 @@ public final RoleType getProjectRoleType() { public final RoleType getSlaveRoleType() { return RoleType.Slave; } + + public final RoleType getViewRoleType() { + return RoleType.View; + } } diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/rolestrategy/RoleType.java b/src/main/java/com/synopsys/arc/jenkins/plugins/rolestrategy/RoleType.java index 25fec613..2dfee2f3 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/rolestrategy/RoleType.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/rolestrategy/RoleType.java @@ -35,7 +35,8 @@ public enum RoleType { Global, Project, - Slave; + Slave, + View; /** * @deprecated Naming convention violation, use {@link #fromString(java.lang.String)}. @@ -68,6 +69,10 @@ public static RoleType fromString(String roleName) { return Slave; } + if (roleName.equals(RoleBasedAuthorizationStrategy.VIEW)) { + return View; + } + throw new java.lang.IllegalArgumentException("Unexpected roleName=" + roleName); } @@ -84,6 +89,8 @@ public String getStringType() { return RoleBasedAuthorizationStrategy.PROJECT; case Slave: return RoleBasedAuthorizationStrategy.SLAVE; + case View: + return RoleBasedAuthorizationStrategy.VIEW; default: throw new java.lang.IllegalArgumentException("Unsupported Role: " + this); } diff --git a/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-roles.jelly b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-roles.jelly index 91baa120..5f6132c9 100644 --- a/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-roles.jelly +++ b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-roles.jelly @@ -37,10 +37,12 @@ + + @@ -90,6 +92,11 @@ + + + + + diff --git a/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-view-roles.jelly b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-view-roles.jelly new file mode 100644 index 00000000..cfc7f856 --- /dev/null +++ b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/assign-view-roles.jelly @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + ${%User/group} + + ${role.key.name} + +
+ + ${%User/group} + + ${role.key.name} + +
+ +

+ + + + + + + + +
diff --git a/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-roles.jelly b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-roles.jelly index b7ad8ce6..5db9ca9d 100644 --- a/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-roles.jelly +++ b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-roles.jelly @@ -38,6 +38,7 @@ + @@ -97,6 +98,13 @@ --> + + + + + + diff --git a/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-view-roles.jelly b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-view-roles.jelly new file mode 100644 index 00000000..4b77c095 --- /dev/null +++ b/src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-view-roles.jelly @@ -0,0 +1,210 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + ${%Role} + + ${%Pattern} + + ${g.title} + +
+ ${p.name} +
+ + ${%Role} + + ${%Pattern} + + ${p.name} +
+ ${g.title} + +
+

+ + + + + + + + + + + +