Tuesday, December 22, 2009

Get User Permissions on TFS team project

This code is based on the listing in http://social.msdn.microsoft.com/Forums/en/tfsadmin/thread/9f977d37-23d8-4b07-a5dd-6cdc6d9dc6a6

Requires 2 parameters: windows user id, and team project name
Returns a List of groups in the team project that the user has permissions for


public static List <string> GetAccountPermissions(string account, string projectName)
{
    List <string> groupList = new List <string>();
    String serverUri = @"http://serverName:8080/";
    // connect to the server
    TeamFoundationServer server = new TeamFoundationServer(serverUri);
    // get ICommonStructureService (later to be used to list all team projects)
    ICommonStructureService iss = (ICommonStructureService)server.GetService(typeof(ICommonStructureService));
    // get IGroupSecurityService (later to be used to retrieve identity information)
    IGroupSecurityService2 gss = (IGroupSecurityService2)server.GetService(typeof(IGroupSecurityService2));
    ProjectInfo[] allTeamProjects = iss.ListProjects();
    // iterate thru the team project list
    foreach (ProjectInfo pi in allTeamProjects)
    {
    // ProjectInfo gives you the team project's name, status, and URI.
        String teamProjectName = pi.Name;
        if (pi.Name == projectName)
        {
            Identity[] allProjectGroups = gss.ListApplicationGroups(pi.Uri);
            // iterate thru the project group list and get a list of direct members for each project group
            foreach (Identity projectGroup in allProjectGroups)
            {
                Identity pg = gss.ReadIdentity(SearchFactor.Sid, projectGroup.Sid, QueryMembership.Direct);
                foreach (String groupMemberSid in pg.Members)
                {
                    Identity m = gss.ReadIdentity(SearchFactor.Sid, groupMemberSid, QueryMembership.None);
                    if (m.AccountName == account)
                    {
                        groupList.Add(pg.DisplayName);
                    }
                }
            }
        }
    }
    return groupList;
}

No comments:

Post a Comment