Merge Security Groups

This commit is contained in:
Virtuworks 2013-08-22 12:28:56 -04:00
commit fde3950e9b
46 changed files with 5521 additions and 816 deletions

View file

@ -27,10 +27,10 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Globalization;
using System.Text;
using WebsitePanel.Providers.Common;
using WebsitePanel.Providers.ResultObjects;
@ -102,6 +102,20 @@ namespace WebsitePanel.Providers.HostedSolution
return sb.ToString();
}
private string GetGroupPath(string organizationId, string groupName)
{
StringBuilder sb = new StringBuilder();
// append provider
AppendProtocol(sb);
AppendDomainController(sb);
AppendCNPath(sb, groupName);
AppendOUPath(sb, organizationId);
AppendOUPath(sb, RootOU);
AppendDomainPath(sb, RootDomain);
return sb.ToString();
}
private string GetRootOU()
{
StringBuilder sb = new StringBuilder();
@ -213,6 +227,7 @@ namespace WebsitePanel.Providers.HostedSolution
org.OrganizationId = organizationId;
org.DistinguishedName = ActiveDirectoryUtils.RemoveADPrefix(orgPath);
org.SecurityGroup = ActiveDirectoryUtils.RemoveADPrefix(GetGroupPath(organizationId));
org.GroupName = organizationId;
}
catch (Exception ex)
{
@ -498,10 +513,19 @@ namespace WebsitePanel.Providers.HostedSolution
throw new ArgumentNullException("loginName");
string path = GetUserPath(organizationId, loginName);
DirectoryEntry entry = ActiveDirectoryUtils.GetADObject(path);
OrganizationUser retUser = GetUser(path);
HostedSolutionLog.LogEnd("GetUserGeneralSettingsInternal");
return retUser;
}
private OrganizationUser GetUser(string path)
{
OrganizationUser retUser = new OrganizationUser();
DirectoryEntry entry = ActiveDirectoryUtils.GetADObject(path);
retUser.FirstName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.FirstName);
retUser.LastName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.LastName);
retUser.DisplayName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.DisplayName);
@ -524,14 +548,13 @@ namespace WebsitePanel.Providers.HostedSolution
retUser.Notes = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.Notes);
retUser.ExternalEmail = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.ExternalEmail);
retUser.Disabled = (bool)entry.InvokeGet(ADAttributes.AccountDisabled);
retUser.Manager = GetManager(entry);
retUser.Manager = GetManager(entry, ADAttributes.Manager);
retUser.SamAccountName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.SAMAccountName);
retUser.DomainUserName = GetDomainName(ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.SAMAccountName));
retUser.DistinguishedName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.DistinguishedName);
retUser.Locked = (bool)entry.InvokeGet(ADAttributes.AccountLocked);
retUser.UserPrincipalName= (string)entry.InvokeGet(ADAttributes.UserPrincipalName);
retUser.UserPrincipalName = (string)entry.InvokeGet(ADAttributes.UserPrincipalName);
HostedSolutionLog.LogEnd("GetUserGeneralSettingsInternal");
return retUser;
}
@ -542,10 +565,10 @@ namespace WebsitePanel.Providers.HostedSolution
return ret;
}
private OrganizationUser GetManager(DirectoryEntry entry)
private OrganizationUser GetManager(DirectoryEntry entry, string adAttribute)
{
OrganizationUser retUser = null;
string path = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.Manager);
string path = ActiveDirectoryUtils.GetADObjectStringProperty(entry, adAttribute);
if (!string.IsNullOrEmpty(path))
{
path = ActiveDirectoryUtils.AddADPrefix(path, PrimaryDomainController);
@ -800,6 +823,226 @@ namespace WebsitePanel.Providers.HostedSolution
}
#endregion
#region Security Groups
public int CreateSecurityGroup(string organizationId, string groupName, string managedBy)
{
return CreateSecurityGroupInternal(organizationId, groupName, managedBy);
}
internal int CreateSecurityGroupInternal(string organizationId, string groupName, string managedBy)
{
HostedSolutionLog.LogStart("CreateSecurityGroupInternal");
HostedSolutionLog.DebugInfo("organizationId : {0}", organizationId);
HostedSolutionLog.DebugInfo("groupName : {0}", groupName);
if (string.IsNullOrEmpty(organizationId))
throw new ArgumentNullException("organizationId");
if (string.IsNullOrEmpty(groupName))
throw new ArgumentNullException("groupName");
bool groupCreated = false;
string groupPath = null;
try
{
string path = GetOrganizationPath(organizationId);
groupPath = GetGroupPath(organizationId, groupName);
if (!ActiveDirectoryUtils.AdObjectExists(groupPath))
{
ActiveDirectoryUtils.CreateGroup(path, groupName);
DirectoryEntry entry = ActiveDirectoryUtils.GetADObject(groupPath);
string manager = string.Empty;
if (!string.IsNullOrEmpty(managedBy))
{
string managerPath = GetUserPath(organizationId, managedBy);
manager = ActiveDirectoryUtils.AdObjectExists(managerPath) ? managerPath : string.Empty;
}
ActiveDirectoryUtils.SetADObjectProperty(entry, ADAttributes.ManagedBy, ActiveDirectoryUtils.RemoveADPrefix(manager));
entry.CommitChanges();
groupCreated = true;
HostedSolutionLog.DebugInfo("Security Group created: {0}", groupName);
}
else
{
HostedSolutionLog.DebugInfo("AD_OBJECT_ALREADY_EXISTS: {0}", groupPath);
HostedSolutionLog.LogEnd("CreateSecurityGroupInternal");
return Errors.AD_OBJECT_ALREADY_EXISTS;
}
}
catch (Exception e)
{
HostedSolutionLog.LogError(e);
try
{
if (groupCreated)
ActiveDirectoryUtils.DeleteADObject(groupPath);
}
catch (Exception ex)
{
HostedSolutionLog.LogError(ex);
}
return Errors.AD_OBJECT_ALREADY_EXISTS;
}
HostedSolutionLog.LogEnd("CreateSecurityGroupInternal");
return Errors.OK;
}
public OrganizationSecurityGroup GetSecurityGroupGeneralSettings(string groupName, string organizationId)
{
return GetSecurityGroupGeneralSettingsInternal(groupName, organizationId);
}
internal OrganizationSecurityGroup GetSecurityGroupGeneralSettingsInternal(string groupName, string organizationId)
{
HostedSolutionLog.LogStart("GetSecurityGroupGeneralSettingsInternal");
HostedSolutionLog.DebugInfo("groupName : {0}", groupName);
HostedSolutionLog.DebugInfo("organizationId : {0}", organizationId);
if (string.IsNullOrEmpty(organizationId))
throw new ArgumentNullException("organizationId");
if (string.IsNullOrEmpty(groupName))
throw new ArgumentNullException("groupName");
string path = GetGroupPath(organizationId, groupName);
DirectoryEntry entry = ActiveDirectoryUtils.GetADObject(path);
OrganizationSecurityGroup securityGroup = new OrganizationSecurityGroup();
securityGroup.ManagerAccount = GetManager(entry, ADAttributes.ManagedBy);
securityGroup.Notes = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.Notes);
securityGroup.AccountName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.SAMAccountName);
securityGroup.SAMAccountName = ActiveDirectoryUtils.GetADObjectStringProperty(entry, ADAttributes.SAMAccountName);
List<OrganizationUser> members = new List<OrganizationUser>();
foreach (string userPath in ActiveDirectoryUtils.GetUsersGroup(groupName))
{
members.Add(GetUser(userPath));
}
securityGroup.MembersAccounts = members.ToArray();
HostedSolutionLog.LogEnd("GetSecurityGroupGeneralSettingsInternal");
return securityGroup;
}
public void DeleteSecurityGroup(string groupName, string organizationId)
{
DeleteSecurityGroupInternal(groupName, organizationId);
}
internal void DeleteSecurityGroupInternal(string groupName, string organizationId)
{
HostedSolutionLog.LogStart("DeleteSecurityGroupInternal");
HostedSolutionLog.DebugInfo("groupName : {0}", groupName);
HostedSolutionLog.DebugInfo("organizationId : {0}", organizationId);
if (string.IsNullOrEmpty(organizationId))
throw new ArgumentNullException("organizationId");
if (string.IsNullOrEmpty(groupName))
throw new ArgumentNullException("groupName");
string path = GetGroupPath(organizationId, groupName);
if (ActiveDirectoryUtils.AdObjectExists(path))
ActiveDirectoryUtils.DeleteADObject(path, true);
HostedSolutionLog.LogEnd("DeleteSecurityGroupInternal");
}
public void SetSecurityGroupGeneralSettings(string organizationId, string groupName, string managedBy, string[] memberAccounts, string notes)
{
SetSecurityGroupGeneralSettingsInternal(organizationId, groupName, managedBy, memberAccounts, notes);
}
internal void SetSecurityGroupGeneralSettingsInternal(string organizationId, string groupName, string managedBy, string[] memberAccounts, string notes)
{
HostedSolutionLog.LogStart("SetSecurityGroupGeneralSettingsInternal");
HostedSolutionLog.DebugInfo("organizationId : {0}", organizationId);
HostedSolutionLog.DebugInfo("groupName : {0}", groupName);
if (string.IsNullOrEmpty(organizationId))
throw new ArgumentNullException("organizationId");
if (string.IsNullOrEmpty(groupName))
throw new ArgumentNullException("groupName");
string path = GetGroupPath(organizationId, groupName);
DirectoryEntry entry = ActiveDirectoryUtils.GetADObject(path);
string manager = string.Empty;
if (!string.IsNullOrEmpty(managedBy))
{
string managerPath = GetUserPath(organizationId, managedBy);
manager = ActiveDirectoryUtils.AdObjectExists(managerPath) ? managerPath : string.Empty;
}
ActiveDirectoryUtils.SetADObjectProperty(entry, ADAttributes.ManagedBy, ActiveDirectoryUtils.RemoveADPrefix(manager));
ActiveDirectoryUtils.SetADObjectProperty(entry, ADAttributes.Notes, notes);
foreach(string userPath in ActiveDirectoryUtils.GetUsersGroup(groupName)) {
ActiveDirectoryUtils.RemoveUserFromGroup(userPath, path);
}
foreach(string user in memberAccounts) {
string userPath = GetUserPath(organizationId, user);
ActiveDirectoryUtils.AddUserToGroup(userPath, path);
}
entry.CommitChanges();
}
public void AddUserToSecurityGroup(string organizationId, string loginName, string groupName)
{
AddUserToSecurityGroupInternal(organizationId, loginName, groupName);
}
internal void AddUserToSecurityGroupInternal(string organizationId, string loginName, string groupName)
{
HostedSolutionLog.LogStart("AddUserToSecurityGroupInternal");
HostedSolutionLog.DebugInfo("organizationId : {0}", organizationId);
HostedSolutionLog.DebugInfo("loginName : {0}", loginName);
HostedSolutionLog.DebugInfo("groupName : {0}", groupName);
if (string.IsNullOrEmpty(organizationId))
throw new ArgumentNullException("organizationId");
if (string.IsNullOrEmpty(loginName))
throw new ArgumentNullException("loginName");
if (string.IsNullOrEmpty(groupName))
throw new ArgumentNullException("groupName");
string userPath = GetUserPath(organizationId, loginName);
string groupPath = GetGroupPath(organizationId, groupName);
ActiveDirectoryUtils.AddUserToGroup(userPath, groupPath);
}
#endregion
public override bool IsInstalled()
{
return Environment.UserDomainName != Environment.MachineName;