Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.plugins.collabnet.auth;
  
  import hudson.model.Item;
  import hudson.scm.SCM;
 
 import java.util.List;
 import java.util.Set;
 
An ACL that uses project roles to determine what Hudson permissions to give.
 
 public class CNProjectACL extends ACL {
     private String projectId = null;
 
     private static Logger log = Logger.getLogger("CNProjectACL");

    
Constructor.

Parameters:
projectId the id of the TeamForge project associated with this ACL
 
     public CNProjectACL(String projectId) {
         this. = projectId;
     }
 
     public boolean hasPermission(Authentication aPermission permission) {
 
         if (!(a instanceof CNAuthentication)) {
             .severe("Improper Authentication type used with " +
                        "CNAuthorizationStrategy!  CNAuthorization " +
                        "strategy cannot be used without " +
                        "CNAuthentication.  Please re-configure your " +
                        "Hudson instance.");
             return false;
         }
 
         if (CommonUtil.isEmpty()) {
             .severe("hasPerission: project id could not be found for project: " + this. + ".");
             return false;
         }
 
         CNAuthentication cnAuth = (CNAuthenticationa;
         String username = (StringcnAuth.getPrincipal();
         Set<PermissionuserPerms = cnAuth.getUserProjectPermSet(username);
         for(; permission!=nullpermission=permission.impliedBy) {
             if (userPerms.contains(permission)) {
                 return true;
             }
         }
         return false;
     }
 
     public static class CollabNetRoles {
         private static Collection<CollabNetRoleroles =
             Collections.emptyList();

        
Get the applicable hudson roles matching a set of user role names

Parameters:
userRoleSet names of roles to match
Returns:
a collection of hudson roles with names that exist in user role set
 
         public static Collection<CollabNetRolegetMatchingRoles(Set<StringuserRoleSet) {
             Collection<CollabNetRolematchRoles = new ArrayList<CollabNetRole>();
             for (CollabNetRole role : getAllRoles()) {
                 if (userRoleSet.contains(role.getName())) {
                     matchRoles.add(role);
                 }
             }
             return matchRoles;
         }

        

Returns:
all roles. Lazily initialized.
 
         public static Collection<CollabNetRolegetAllRoles() {
             if (..isEmpty()) {
                  = new ArrayList<CollabNetRole>();
                 Collection<PermissiontempPermission =
                     new ArrayList<Permission>();
                 tempPermission.add(.);
                 tempPermission.add(.);
                 .add(new CollabNetRole("Hudson Read""Allows users " +
                                             "read-access to Hudson jobs.",
                                             tempPermission));
                 tempPermission.clear();
                 tempPermission.add(.);
                tempPermission.add(.);
                tempPermission.add(.);
                tempPermission.add(.);
                tempPermission.add(.); // if you have permission to build, you can create tag
                .add(new CollabNetRole("Hudson Build/Cancel""Allow " +
                                            "users to start a new build, or " +
                                            "to cancel a build.",
                                            tempPermission));
                tempPermission.clear();
                tempPermission.add(.);
                .add(new CollabNetRole("Hudson Configure""Allow users" +
                                            " to configure a build.",
                                            tempPermission));
                tempPermission.clear();
                tempPermission.add(.);
                .add(new CollabNetRole("Hudson Delete""Allow users to "
                                            + "delete builds.",
                                            tempPermission));
                tempPermission.clear();
                // add build promotion as a permission, if the build promotion
                // plugin is present.
                if (Hudson.getInstance().getPlugin("promoted-builds") != null) {
                    // check if we have the PROMOTE permission
                    Field promote = null;
                    Field[] promotionFields = Promotion.class.getFields();
                    for (Field fieldpromotionFields) {
                        if (field.getName().equals("PROMOTE")) {
                            promote = field;
                            break;
                        }
                    }
                    if (promote != null) {
                        Permission promotePermission = null;
                        try {
                            promotePermission = (Permissionpromote.get(null);
                        } catch (IllegalAccessException iae) {}
                        if (promotePermission != null) {
                            // if we have the permission, add it
                            tempPermission.add(promotePermission);
                        }
                    }
                    .add(new CollabNetRole("Hudson Promote",
                                            "Allow users to " +
                                            "promote builds.",
                                            tempPermission));
                    tempPermission.clear();
                }
            }
            return .;
        }

        

Returns:
an ordered List of Role names.
        public static List<StringgetNames() {
            List<Stringnames = new ArrayList<String>();
            for (CollabNetRole role: CollabNetRoles.getAllRoles()) {
                names.add(role.getName());
            }
            return names;
        }

        
Given a permission, return the CollabNet role which would grant that permission (if any). Returns the first permission granting role found (but we expect only one).

Parameters:
permission
Returns:
the CollabNet role which would grant that permission, or null if none would.
        public static CollabNetRole getGrantingRole(Permission permission) {
            Collection<PermissionimplyingPermissions = 
                CollabNetRoles.expandPermissions(permission);
            for (CollabNetRole role: CollabNetRoles.getAllRoles()) {
                for (Permission pimplyingPermissions) {
                    if (role.hasPermission(p)) {
                        return role;
                    }
                }
            }
            return null;
        }

        
Given a permission, expand it into a collection, containing every permission implied by this permission (including the starting permission).

Parameters:
permission
Returns:
the collection of implied permissions.
        private static Collection<PermissionexpandPermissions(Permission 
                                                                permission) {
            Collection<Permissionpermissions = new ArrayList<Permission>();
            for(Permission p = permissionp != null
                p = permission.impliedBy) {
                permissions.add(p);
            }
            return permissions;
        }
    }
New to GrepCode? Check out our FAQ X