授予自定义TFS应用程序组的权限

时间:2011-11-21 13:33:19

标签: c#-4.0 tfs tfs2010

我想在每个团队项目中创建一个新的应用程序组并授予其权限。到目前为止,我所做的是迭代所有项目并创建组,如果不存在的话:

static void Main( string[] args )
{
    m_TfsServer = new TfsTeamProjectCollection( 
        new Uri( "http://server:port/vdir" ), 
        System.Net.CredentialCache.DefaultNetworkCredentials, 
        new UICredentialsProvider( ) );
    m_TfsServer.EnsureAuthenticated( );

    m_TfsSecurityService = m_TfsServer.GetService<IGroupSecurityService>( );

    var structService = m_TfsServer.GetService<ICommonStructureService>( );
    foreach ( var p in structService.ListAllProjects( ) )
    {
        string groupSid;
        if ( !GroupExist( p.Uri, GroupName ) )
        {
            groupSid = m_TfsSecurityService.CreateApplicationGroup( 
                p.Uri, 
                GroupName, 
                GroupDescription );
        }
        else
        {
            groupSid = GetApplicationGroupSid( p.Uri, GroupName );
        }
        Identity userIdentity = m_TfsSecurityService.ReadIdentityFromSource( 
            SearchFactor.AccountName, 
            UserName );
        if ( !m_TfsSecurityService.IsMember( groupSid, userIdentity.Sid ) )
        {
            m_TfsSecurityService.AddMemberToApplicationGroup( 
                groupSid, 
                userIdentity.Sid );
        }
    }
}

private static bool GroupExist( string projectUri, string groupName )
{
    bool result = false;
    Identity[] groups = 
        m_TfsSecurityService.ListApplicationGroups( projectUri );
    foreach ( Identity group in groups )
    {
        result |= group.SecurityGroup && group.DisplayName.Equals( groupName );
    }
    return result;
}

private static string GetApplicationGroupSid( 
    string projectUri, 
    string groupName)
{
    return m_TfsSecurityService.ListApplicationGroups( projectUri )
        .Where( g => g.DisplayName.Equals( groupName ) )
        .Select( g => g.Sid )
        .First( );
}

唯一剩下的就是将“查看项目级信息”权限授予该组。

[编辑]

我发现了一些使用VersionControlService授予权限的东西:

var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection( ServerUri );
tfs.EnsureAuthenticated( );         
var vcs = tfs.GetService<VersionControlServer>( );
//vcs.SetPermissions( new SecurityChange[] { } ); ???

但我没有找到任何文档如何授予组权限,所以我甚至不确定这个解决方案是否是正确的方法。

[/编辑]

是否有人使用TFS权限管理或已通过TFS API授予权限的任何人?

1 个答案:

答案 0 :(得分:0)