Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.plugins.collabnet.auth;
  
  
  
 import java.util.List;
 
 
CNConnection encapsulates the CN webservice methods.
 
 
 public class CNConnection {
     private CNAuthentication auth;
 
     private static Logger log = Logger.getLogger("CNConnection");

    

Parameters:
authentication
 
     private CNConnection(CNAuthentication a) {
         this. = a;
     }

    
Gets an instance with the current authentication, or null if the auth is the wrong type.
 
     public static CNConnection getInstance() {
         return CNConnection.getInstance(Hudson.getAuthentication());
     }

    
Wraps the private constructor. Will return null if the Authentication is the wrong type (i.e. not CNAuthentication).
 
     public static CNConnection getInstance(Authentication a) {
         if (a instanceof CNAuthentication) {
             return new CNConnection((CNAuthenticationa);
         } else {
             return null;
         }
     }

    

Returns:
the CollabNetApp.
 
     public CollabNetApp getCollabNetApp() {
         Object creds = this.getAuth().getCredentials();
         if (creds instanceof CollabNetApp) {
             return (CollabNetAppcreds;
         } else {
             throw new IllegalStateException ("Credentials are incorrect " +
                                              "type for CollabNetAutorization: "
                                              + creds.getClass().getName());
         }
     }

    

Parameters:
url for CollabNet server
Returns:
the CollabNet version for the given url.
 
     public static CNVersion getVersion(String url) {
         String version = null;
         CNVersion cnversion = null;
         try {
             version = CollabNetApp.getApiVersion(url);
         } catch (RemoteException re) {
             .info("getVersion: failed with RemoteException: " + 
                      re.getMessage());
             return cnversion;
         }
         try {
             cnversion = new CNVersion(version);
         } catch (IllegalArgumentException iae) {
             .severe("getVersion: unexpected error when attempting to " +
                        "parse CollabNet version: " + iae.getMessage());
             return cnversion;
         }
         return cnversion;
     }

    

Returns:
the username.
 
     public String getUsername() {
         Object principal = this.getAuth().getPrincipal();
        if (principal instanceof String) {
            return (Stringprincipal;
        } else {
            throw new IllegalStateException("Pricipal is incorrect type " +
                                            "for CollabNetAutorization: " + 
                                            principal.getClass().getName());
        }
    }

    

Returns:
the Authentication.
    public CNAuthentication getAuth() {
        return this.;
    }

    
Determines if the authenticated user belongs to any of the groups. This is currently from data that's calculated once (on login). If this ever turns out to be insufficient, we could change this method to get the data on the fly.

Parameters:
groups
Returns:
true if the user is a member of any of the groups.
    public boolean isMemberOfAny(Collection<Stringgroups) {
        for (String groupgroups) {
            if (this.getAuth().isMember(group)) {
                return true;
            }
        }
        return false;
    }

    
Determines if the authenticated user is a super user. This is currently from data that's calculated once (on login). If this ever turns out to be insufficient, we could change this method to get the data on the fly.
    public boolean isSuperUser() {
        GrantedAuthority[] authorities = this.getAuth().getAuthorities();
        for (GrantedAuthority authorityauthorities) {
            if (authority.getAuthority().equals(.)) {
                return true;
            }
        }
        return false;
    }

    
Determines if the authenticated user is a project admin.

Parameters:
projectId
Returns:
true if the user is a project admin.
    public boolean isProjectAdmin(String projectId) {
        boolean admin = false;
        try {
            admin = this.getCollabNetApp().
                isUserProjectAdmin(this.getUsername(), projectId);
        } catch (RemoteException re) {
            .info("isProjectAdmin: failed with RemoteException: " + 
                          re.getMessage());
        }
        return admin;
    }

    

Parameters:
project name
Returns:
projectId or null, if none found.
    public String getProjectId(String project) {
        return CNHudsonUtil.getProjectId(this.getCollabNetApp(), project);
    }

    

Returns:
all project names, sanitized for JS.
    public Collection<StringgetProjects() {
        return ComboBoxUpdater.ProjectsUpdater
            .getProjectList(this.getCollabNetApp());
    }

    

Parameters:
projectId
username
Returns:
a Collection of roles the user has in this project.
    public Collection<StringgetUserRoles(String projectIdString username) {
        Collection<Stringroles = new ArrayList<String>();
        RbacApp ra = new RbacApp(this.getCollabNetApp());
        try {
            roles = ra.getUserRoles(projectIdusername);
        } catch (RemoteException re) {
            .severe("getUserRoles: failed with Remote Exception while " +
                       "getting roles for user " + username + ": " + 
                       re.getMessage());
        }
        return roles;
    }

    

Parameters:
projectId
Returns:
a Collection of all users that are members of the project.
    public Collection<StringgetUsers(String projectId) {
        return ComboBoxUpdater.UsersUpdater
            .getUserList(this.getCollabNetApp(), projectId);
    }

    

Parameters:
username
Returns:
true if user exists.
    public boolean isUsernameValid(String username) {
        return CNHudsonUtil.isUserValid(this.getCollabNetApp(), username);
    }

    

Parameters:
group
Returns:
true if group exists.
    public boolean isGroupnameValid(String group) {
        boolean valid = false;
        try {
            valid = this.getCollabNetApp().getGroups().containsKey(group);
        } catch (RemoteException re) {
            .severe("isGroupnameValid: failed with RemoteException " + 
                       re.getMessage());
        }
        return valid;
    }

    

Parameters:
projectId
Returns:
a Collection of all admins in the project.
    public Collection<StringgetAdmins(String projectId) {
        Collection<Stringadmins = Collections.emptyList();
        try {
            admins = this.getCollabNetApp().getAdmins(projectId);
        } catch (RemoteException re) {
            .severe("getAdmins: failed with RemoteException " + 
                       re.getMessage());
        }
        return admins;
    }

    

Parameters:
projectId
roleNames
descriptions of the roles.
    public boolean addRoles(String projectIdList<StringroleNames
                            List<Stringdescriptions) {
        boolean added = false;
        RbacApp ra = new RbacApp(this.getCollabNetApp());
        try {
            added = ra.addRoles(projectIdroleNames.toArray(new String[0]), 
                                descriptions.toArray(new String[0]));
        } catch (RemoteException re) {
            .severe("addRoles: failed with RemoteException: " + 
                       re.getMessage());
        }
        return added;
    }

    
Grant the specified roles to the users in the given project.

Parameters:
projectId
roles
usernames
    public void grantRoles(String projectIdCollection<Stringroles
                           Collection<Stringusernames) {
        RbacApp ra = new RbacApp(this.getCollabNetApp());
        for (String usernameusernames) {
            for (String roleroles) {
                try {
                    ra.grantRole(projectIdroleusername);
                } catch (RemoteException re) {
                    .severe("grantRoles: failed with RemoteException: " +
                               re.getMessage());
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X