#region Group Methods
/// \
/// Creates a new group in Active Directory
/// \
/// \The OU location you want to save your new Group\
/// \The name of the new group\
/// \The description of the new group\
/// \The scope of the new group\
/// \True is you want this group to be a security group, false if you want this as a distribution group\
/// \Retruns the GroupPrincipal object\
public GroupPrincipal CreateNewGroup(string sOU, string sGroupName, string sDescription, GroupScope oGroupScope, bool bSecurityGroup)
{
PrincipalContext oPrincipalContext = GetPrincipalContext(sOU);
GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext, sGroupName);
oGroupPrincipal.Description = sDescription;
oGroupPrincipal.GroupScope = oGroupScope;
oGroupPrincipal.IsSecurityGroup = bSecurityGroup;
oGroupPrincipal.Save();
return oGroupPrincipal;
}
/// \
/// Adds the user for a given group
/// \
/// \The user you want to add to a group\
/// \The group you want the user to be added in\
/// \Returns true if successful\
public bool AddUserToGroup(string sUserName, string sGroupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oUserPrincipal != null && oGroupPrincipal != null)
{
if (!IsUserGroupMember(sUserName, sGroupName))
{
oGroupPrincipal.Members.Add(oUserPrincipal);
oGroupPrincipal.Save();
}
}
return true;
}
catch
{
return false;
}
}
/// \
/// Removes user from a given group
/// \
/// \The user you want to remove from a group\
/// \The group you want the user to be removed from\
/// \Returns true if successful
public bool RemoveUserFromGroup(string sUserName, string sGroupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oUserPrincipal != null && oGroupPrincipal != null)
{
if (IsUserGroupMember(sUserName, sGroupName))
{
oGroupPrincipal.Members.Remove(oUserPrincipal);
oGroupPrincipal.Save();
}
}
return true;
}
catch
{
return false;
}
}
/// \
/// Checks if user is a member of a given group
/// \
/// \The user you want to validate\
/// \The group you want to check the membership of the user\
/// \Returns true if user is a group member\
public bool IsUserGroupMember(string sUserName, string sGroupName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oUserPrincipal != null && oGroupPrincipal != null)
{
return oGroupPrincipal.Members.Contains(oUserPrincipal);
}
else
{
return false;
}
}
/// \
/// Gets a list of the users group memberships
/// \
/// \The user you want to get the group memberships\
/// \Returns an arraylist of group memberships\
public ArrayList GetUserGroups(string sUserName)
{
ArrayList myItems = new ArrayList();
UserPrincipal oUserPrincipal = GetUser(sUserName);
PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
foreach (Principal oResult in oPrincipalSearchResult)
{
myItems.Add(oResult.Name);
}
return myItems;
}
/// \
/// Gets a list of the users authorization groups
/// \
/// \The user you want to get authorization groups\
/// \Returns an arraylist of group authorization memberships\
public ArrayList GetUserAuthorizationGroups(string sUserName)
{
ArrayList myItems = new ArrayList();
UserPrincipal oUserPrincipal = GetUser(sUserName);
PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetAuthorizationGroups();
foreach (Principal oResult in oPrincipalSearchResult)
{
myItems.Add(oResult.Name);
}
return myItems;
}
#endregion
#region Helper Methods
/// \
/// Gets the base principal context
/// \
/// \Retruns the PrincipalContext object\
public PrincipalContext GetPrincipalContext()
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
return oPrincipalContext;
}
/// \
/// Gets the principal context on specified OU
/// \
/// \The OU you want your Principal Context to run on\
/// \Retruns the PrincipalContext object\
public PrincipalContext GetPrincipalContext(string sOU)
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
return oPrincipalContext;
}
#endregion
} |