using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
namespace BuiltinGroupReader
{
public class BuiltinGroupReader
{
#region Members
private IGroupSecurityService m_gss;
private ICommonStructureService m_css;
#endregion
#region Constructors
///
/// Constructs a built-in group reader
///
/// full URL to the server we wish to contact
public BuiltinGroupReader(string tfsServerName)
{
// connect to the server
TeamFoundationServer m_tfServer = TeamFoundationServerFactory.GetServer(tfsServerName);
// connect to two service interfaces
m_gss = (IGroupSecurityService)m_tfServer.GetService(typeof(IGroupSecurityService));
m_css = (ICommonStructureService)m_tfServer.GetService(typeof(ICommonStructureService));
}
#endregion
#region Public methods
///
/// Prints out details about admin groups, as well as a list of all
/// other project groups
///
public void DisplayProjectAdminGroups()
{
ProjectInfo[] projects = m_css.ListProjects();
foreach (ProjectInfo project in projects)
{
Console.WriteLine("Group info for project '{0}'", project.Name);
// Find all the Project Administrative users
Identity adminGroup = m_gss.ReadIdentity(SearchFactor.AdministrativeApplicationGroup, project.Uri, QueryMembership.Expanded);
// print interesting identity information
Console.WriteLine("Admin group details");
Console.WriteLine(" Display name: {0}", adminGroup.DisplayName);
Console.WriteLine(" Description: {0}", adminGroup.Description);
Console.WriteLine(" Sid: {0}", adminGroup.Sid);
Console.WriteLine(" Special Type: {0}", adminGroup.SpecialType);
Console.WriteLine();
// display all project groups
Console.WriteLine("List of groups in project '{0}'", project.Name);
Identity[] projectGroups = m_gss.ListApplicationGroups(project.Uri);
foreach (Identity group in projectGroups)
{
PrintIdentityName(group);
}
// insert spacer for visibility sake
Console.WriteLine("--------------------");
Console.WriteLine();
}
}
///
/// Prints out the membership of the three built-in groups
///
public void DisplayBuiltinGroupMembers()
{
// Find all the Administrative users
Identity adminGroup = m_gss.ReadIdentity(SearchFactor.AdministrativeApplicationGroup, null, QueryMembership.Expanded);
PrintIdentity(adminGroup);
// insert spacer for visibility sake
Console.WriteLine("--------------------");
Console.WriteLine();
// Find all the Service accounts
Identity serviceGroup = m_gss.ReadIdentity(SearchFactor.ServiceApplicationGroup, null, QueryMembership.Expanded);
PrintIdentity(serviceGroup);
// insert spacer for visibility sake
Console.WriteLine("--------------------");
Console.WriteLine();
// Find all the valid users
Identity everyoneGroup = m_gss.ReadIdentity(SearchFactor.EveryoneApplicationGroup, null, QueryMembership.Expanded);
PrintIdentity(everyoneGroup);
}
///
/// Finds user information for all members of the identity passed in and
/// prints out their name and type information
///
/// id whose information we wish to print
public void PrintIdentity(Identity id)
{
// print interesting identity information
Console.WriteLine("Detailed info for '{0}' identity", id.DisplayName);
Console.WriteLine(" Description: {0}", id.Description);
Console.WriteLine(" Sid: {0}", id.Sid);
Console.WriteLine(" Special Type: {0}", id.SpecialType);
Console.WriteLine();
// for groups, print out members
if ((id.Type == IdentityType.ApplicationGroup ||
id.Type == IdentityType.WindowsGroup) &&
(id.Members != null))
{
// get Identity objects for all members of the group
Identity[] members = m_gss.ReadIdentities(SearchFactor.Sid, id.Members, QueryMembership.None);
// now print them all out
Console.WriteLine("All members of the '{0}' group", id.DisplayName);
foreach (Identity member in members)
{
PrintIdentityName(member);
}
Console.WriteLine();
}
}
///
/// Prints data for an individual identity
///
/// id to display
public void PrintIdentityName(Identity id)
{
// if it's not a TFS identity, use Domain and AccountName
if (id.Type != IdentityType.ApplicationGroup)
{
Console.WriteLine(@" {0}\{1} ({2}) [{3}]", id.Domain, id.AccountName, id.DisplayName, id.Type);
}
// otherwise, we need to use the DisplayName
else
{
// default to the global group identifier
string domain = "SERVER";
// if the identity is a project group, find the project name
if (!String.IsNullOrEmpty(id.Domain))
{
ProjectInfo projInf = m_css.GetProject(id.Domain);
domain = projInf.Name;
}
Console.WriteLine(@" [{0}]\{1} [{2}]", domain, id.DisplayName, id.Type);
}
}
#endregion
#region 'main' method
///
/// Main method- entry point for this program
///
///
public static void Main(string[] args)
{
// default to a known server
string serverName = "http://vstsnc-011:8080";
// check if any other server was passed in.
if (args != null && args.Length > 0)
{
serverName = args[0];
}
// create the built-in group reader and then display all the
// users in the builtin groups
BuiltinGroupReader p = new BuiltinGroupReader(serverName);
p.DisplayBuiltinGroupMembers();
p.DisplayProjectAdminGroups();
}
#endregion
}
}