Check out my new blog at https://shibumiware.blogspot.com

Monday, June 16, 2008

Microsoft Project FX (mpFx) - Project Server Groups

Below is an mpFx class that provides data and functionality for managing Project Server groups, plus a helper method for retrieving the Active Directory groups for the default AD root.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Diagnostics;
   4: using System.DirectoryServices;
   5: using CodePlex.MicrosoftProject.mpFx.SecurityWebService;
   6:  
   7: namespace CodePlex.MicrosoftProject.mpFx
   8: {
   9:     public class Groups
  10:     {
  11:         private Dictionary<Guid, string> _Groups;
  12:  
  13:         protected internal Groups(Security parent)
  14:         {
  15:             Parent = parent;
  16:         }
  17:  
  18:         protected internal Security Parent { get; protected set; }
  19:  
  20:         /// <summary>
  21:         /// Retrieve a dictionary of Group GUIDS and Names from Project Server
  22:         /// </summary>
  23:         public Dictionary<Guid, string> GetGroupsList(bool refresh)
  24:         {
  25:             if (_Groups == null || refresh)
  26:             {
  27:                 _Groups = new Dictionary<Guid, string>();
  28:  
  29:                 using (SecurityGroupsDataSet securityGroupsDataSet = Parent.Parent.WebServices.Security.ReadGroupList())
  30:                 {
  31:                     foreach (SecurityGroupsDataSet.SecurityGroupsRow group in securityGroupsDataSet.SecurityGroups.Rows)
  32:                     {
  33:                         _Groups.Add(group.WSEC_GRP_UID, group.WSEC_GRP_NAME);
  34:                     }
  35:                 }
  36:             }
  37:  
  38:             return _Groups;
  39:         }
  40:  
  41:         #region Group Methods
  42:  
  43:         /// <summary>
  44:         /// Create a new group
  45:         /// </summary>
  46:         /// <param name="groupGuid">The group's GUID</param>
  47:         /// <param name="name">The group's name</param>
  48:         /// <param name="description">A description of the group</param>
  49:         /// <param name="adGroupName">The group's Active Directory if relevant</param>
  50:         /// <param name="adGroupGuid">The group's Active Directory objectGUID if relevant</param>
  51:         public void CreateGroup(Guid groupGuid,
  52:                                 string name,
  53:                                 string description,
  54:                                 string adGroupName,
  55:                                 Guid adGroupGuid)
  56:         {
  57:             using (SecurityGroupsDataSet securityGroupsDataSet = new SecurityGroupsDataSet())
  58:             {
  59:                 SecurityGroupsDataSet.SecurityGroupsRow group = securityGroupsDataSet.SecurityGroups.NewSecurityGroupsRow();
  60:  
  61:                 group.WSEC_GRP_UID = groupGuid;
  62:                 group.WSEC_GRP_NAME = name;
  63:                 group.WSEC_GRP_DESC = description;
  64:                 group.WSEC_GRP_AD_GROUP = adGroupName;
  65:  
  66:                 if (adGroupGuid != Guid.Empty)
  67:                 {
  68:                     group.WSEC_GRP_AD_GUID = adGroupGuid;
  69:                 }
  70:  
  71:                 securityGroupsDataSet.SecurityGroups.AddSecurityGroupsRow(group);
  72:  
  73:                 Parent.Parent.WebServices.Security.CreateGroups(securityGroupsDataSet);
  74:             }
  75:         }
  76:  
  77:         /// <summary>
  78:         /// Create a new group and add resources to it
  79:         /// </summary>
  80:         /// <param name="groupGuid">The group's GUID</param>
  81:         /// <param name="name">The group's name</param>
  82:         /// <param name="description">A description of the group</param>
  83:         /// <param name="adGroupName">The group's Active Directory if relevant</param>
  84:         /// <param name="adGroupGuid">The group's Active Directory objectGUID if relevant</param>
  85:         /// <param name="resourceGuids">An array of resource GUID for which memebership we be established</param>
  86:         public void CreateGroup(Guid groupGuid,
  87:                                 string name,
  88:                                 string description,
  89:                                 string adGroupName,
  90:                                 Guid adGroupGuid,
  91:                                 Guid[] resourceGuids)
  92:         {
  93:             CreateGroup(groupGuid, name, description, adGroupName, adGroupGuid);
  94:             AddResourcesToGroup(groupGuid, resourceGuids);
  95:         }
  96:  
  97:         /// <summary>
  98:         /// Add users to a group
  99:         /// </summary>
 100:         /// <param name="groupGuid">The GUID of the group</param>
 101:         /// <param name="resourceGuids">An array of resource GUID for which memebership we be established</param>
 102:         public void AddResourcesToGroup(Guid groupGuid,
 103:                                         Guid[] resourceGuids)
 104:         {
 105:             if (groupGuid == Guid.Empty || !_Groups.ContainsKey(groupGuid))
 106:             {
 107:                 throw new ArgumentException("Invalid group");
 108:             }
 109:  
 110:             using (SecurityGroupsDataSet securityGroupsDataSet = Parent.Parent.WebServices.Security.ReadGroup(groupGuid))
 111:             {
 112:                 foreach (Guid resourceGuid in resourceGuids)
 113:                 {
 114:                     if (securityGroupsDataSet.GroupMembers.FindByRES_UIDWSEC_GRP_UID(resourceGuid, groupGuid) == null)
 115:                     {
 116:                         SecurityGroupsDataSet.GroupMembersRow groupMembership = securityGroupsDataSet.GroupMembers.NewGroupMembersRow();
 117:  
 118:                         groupMembership.RES_UID = resourceGuid;
 119:                         groupMembership.WSEC_GRP_UID = groupGuid;
 120:  
 121:                         securityGroupsDataSet.GroupMembers.AddGroupMembersRow(groupMembership);
 122:                     }
 123:                 }
 124:  
 125:                 Parent.Parent.WebServices.Security.SetGroups(securityGroupsDataSet);
 126:             }
 127:         }
 128:  
 129:         /// <summary>
 130:         /// Retrieve dictionary of Active Directory groups for the current default search root
 131:         /// </summary>
 132:         /// <returns></returns>
 133:         public Dictionary<string, Guid> GetADGroups()
 134:         {
 135:             DirectorySearcher directorySearcher = new DirectorySearcher {Filter = "(&(objectCategory=group))", SearchScope = SearchScope.Subtree};
 136:  
 137:             directorySearcher.PropertiesToLoad.Add("name");
 138:             directorySearcher.PropertiesToLoad.Add("objectGUID");
 139:  
 140:             SearchResultCollection searchResultCollection = directorySearcher.FindAll();
 141:  
 142:             Dictionary<string, Guid> results = new Dictionary<string, Guid>();
 143:  
 144:             foreach (SearchResult result in searchResultCollection)
 145:             {
 146:                 Debug.Assert(result.Properties["name"].Count == 1 && result.Properties["objectGUID"].Count == 1);
 147:  
 148:                 results.Add(result.Properties["name"][0].ToString(), new Guid((byte[]) result.Properties["objectGUID"][0]));
 149:             }
 150:  
 151:             return results;
 152:         }
 153:  
 154:         #endregion
 155:     }
 156: }

No comments :

Disclaimer

Content on this site is provided "AS IS" with no warranties and confers no rights. Additionally, all content on this site is my own personal opinion and does not represent my employer's view in any way.