Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.plugins.collabnet.auth;
  
  import hudson.Extension;
  import hudson.model.Item;
  import hudson.model.Job;
 
 import java.util.List;
 
 
Job property to associate a Hudson job with a CollabNet Project for Authorization purposes (used with CollabNet Authorization).
 
 public class CNAuthProjectProperty extends JobProperty<Job<?, ?>> {
     public static Permission CONFIGURE_PROPERTY = .;
     private String project;
     private boolean createRoles = false;
     private boolean grantDefaultRoles = false;
     private static Logger log = Logger.getLogger("CNAuthProjectProperty");
     private static Collection<StringdefaultAdminRoles = 
         Collections.emptyList();
     private static Collection<StringdefaultUserRoles = 
         Collections.emptyList();
 
     public CNAuthProjectProperty(String projectBoolean createRoles
                                  Boolean grantDefaults) {
         this. = project;
         this. = createRoles.booleanValue();
         this. = grantDefaults.booleanValue();
         if (this. || this.) {
             this.loadRoles();
         }
     }

    

Returns:
the name of the CollabNet project.
 
     public String getProject() {
         return this.;
     }

    

Returns:
true if creating the roles on the CollabNet server should be attempted.
 
     public boolean createRoles() {
         return this.;
     }

    

Returns:
true if the default roles should be added.
 
     public boolean grantDefaultRoles() {
         return this.;
     }

    

Returns:
the default user roles. Lazily initialized.
 
     public Collection<StringgetDefaultUserRoles() {
         if (..isEmpty()) {
             . = new ArrayList<String>();
             ..add("Hudson Read");
         }
         return .;
     }

    

Returns:
the default admin roles. Lazily initialized.
 
     public Collection<StringgetDefaultAdminRoles() {
             . = 
                 CNProjectACL.CollabNetRoles.getNames();
         }
         return .;
     }

    
Load the roles into CSFE, if they are not already present. Requires the logged in user to be a project admin in the CollabNet project.
 
     private void loadRoles() {
         if (this.getProject() != null && !this.getProject().equals("")) {
            CNConnection conn = CNConnection.getInstance();
            if (conn == null) {
                .warning("Cannot loadRoles, incorrect authentication type.");
                return;
            }
            String projectId = conn.getProjectId(this.getProject());
            if (this.createRoles()) {
                List<StringroleNames = CNProjectACL.CollabNetRoles.
                    getNames();
                List<Stringdescriptions = CNProjectACL.CollabNetRoles.
                    getDescriptions();
                conn.addRoles(projectIdroleNamesdescriptions);
            }
            
            if (this.grantDefaultRoles()) {
                // load up some default roles
                // this should be an option later
                conn.grantRoles(projectIdthis.getDefaultUserRoles(), 
                                conn.getUsers(projectId));
                conn.grantRoles(projectIdthis.getDefaultAdminRoles(), 
                                conn.getAdmins(projectId));
            }
        }   
    }

    
Descriptor class.
    @Extension
    public static class DescriptorImpl extends JobPropertyDescriptor {
        public DescriptorImpl() {
            super(CNAuthProjectProperty.class);
        }

        

Parameters:
req config page parameters.
Returns:
new CNAuthProjectProperty object, instantiated from the configuration form vars.
Throws:
hudson.model.Descriptor.FormException
        @Override
        public JobProperty<?> newInstance(StaplerRequest req,
                                          JSONObject formData
            throws FormException {
            Boolean createRoles = .;
            Boolean grantDefaults = .;
            if (formData.get("createRoles") != null) {
                createRoles = .;
            }
            if (formData.get("grantDefaults") != null) {
                grantDefaults = .;
            }
            return new CNAuthProjectProperty((String)formData.get("project"),
                                             createRoles,
                                             grantDefaults);
        }

        

Returns:
string to display.
        @Override
        public String getDisplayName() {
            return "Associated CollabNet Project";
        }
        
        

Parameters:
jobType
Returns:
true when the CNAuthorizationStrategy is in effect.
        @Override
	public boolean isApplicable(Class<? extends JobjobType) {
            // only applicable when using CNAuthorizationStrategy
            return Hudson.getInstance().getAuthorizationStrategy() 
                instanceof CNAuthorizationStrategy;
        }

        
Form validation for the project field.

Parameters:
project
        public FormValidation doProjectCheck(@QueryParameter String project) {
            if (project == null || project.equals("")) {
                return FormValidation.warning("If left empty, only Hudson admins have " +
                        "more than READ permissions in the project.");
            }
            CNConnection conn = CNConnection.getInstance();
            if (conn == null) {
                return FormValidation.warning("Cannot check project name, improper" +
                        " authentication type.");
            }
            String projectId = conn.getProjectId(project);
            boolean superUser = conn.isSuperUser();
            boolean hudsonAdmin = Hudson.getInstance().getACL()
                .hasPermission(.);
            if (projectId == null) {
                if (superUser) {
                    return FormValidation.error("This project does not exist.");
                } else {
                    return FormValidation.error("The current user does not have access " +
                          "to this project.  If this project is " +
                          "chosen, the current user will be locked " +
                          "out of this Hudson job.");
                }
            }
            if (superUser) {
                // all other errors should not be valid for a
                // superuser, since superusers are Hudson Admins
                // (so all-powerful in the Hudson realm) and also
                // all-powerful in the CollabNet server.
                return FormValidation.ok();
            }
            if (!conn.isProjectAdmin(projectId)) {
                return FormValidation.warning("The current user is not a project admin in " +
                     "the project, so he/she cannot create or " +
                     "grant roles.");
            }
            if (hudsonAdmin) {
                // no more errors apply to the Hudson Admin, since
                // admins will never be locked out of this page.
                return FormValidation.ok();
            }
            // check that the user will have configure permissions
            // on this page
            CNProjectACL acl = new CNProjectACL(project);
            if (!acl.hasPermission(
                                   .)) {
                CollabNetRole roleNeeded =
                    CNProjectACL.CollabNetRoles
                    .getGrantingRole(
                                     .);
                return FormValidation.warning("The current user does not have the '" +
                     roleNeeded.getName() + "' role in the " +
                     "project, which is required to configure " +
                     "this Hudson job.  If this project is chosen," +
                     " the current user will not have the power " +
                     "to change the project later, unless he/she " +
                     "is given this role.");
            }
            return FormValidation.ok();
        }

        
Get a list of projects to choose from.

Returns:
an array of project names.
        public String[] getProjects() {
            Collection<Stringprojects = Collections.emptyList();
            CNConnection conn = CNConnection.getInstance();
            if (conn == null) {
                return new String[0];
            }
            projects = conn.getProjects();
            return projects.toArray(new String[0]);
        }

        

Returns:
the CollabNet server url.
        public String getCollabNetUrl() {
            CNConnection conn = CNConnection.getInstance();
            return conn.getCollabNetApp().getServerUrl();
        }
    }
New to GrepCode? Check out our FAQ X