Tidy: Sort/remove usings, simplify names

This commit is contained in:
Gary Sharp
2017-03-25 15:29:51 +11:00
parent 526f8547f7
commit ed66f4f285
168 changed files with 708 additions and 1175 deletions
@@ -52,7 +52,7 @@ namespace Disco.Services.Interop.ActiveDirectory
var trigger = TriggerBuilder.Create()
.StartAt(DateTimeOffset.Now.AddMinutes(3));
this.ScheduleTask(trigger);
ScheduleTask(trigger);
}
}
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Services.Interop.ActiveDirectory
{
@@ -38,8 +34,8 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
if (this.Entry != null)
return this.Entry.Path;
if (Entry != null)
return Entry.Path;
else
return base.ToString();
}
@@ -1,10 +1,8 @@
using Disco.Data.Repository;
using Disco.Services.Tasks;
using System;
using System.Collections.Generic;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Services.Interop.ActiveDirectory
@@ -43,19 +43,19 @@ namespace Disco.Services.Interop.ActiveDirectory
public ADDomain(ActiveDirectoryContext Context, Domain Domain)
{
this.context = Context;
context = Context;
this.Domain = Domain;
this.SearchContainers = null;
this.domainControllers = null;
this.domainMaintenanceNext = DateTime.Now.AddMinutes(DomainMaintanceIntervalMinutes);
SearchContainers = null;
domainControllers = null;
domainMaintenanceNext = DateTime.Now.AddMinutes(DomainMaintanceIntervalMinutes);
this.Initialize();
Initialize();
}
private void Initialize()
{
this.Name = Domain.Name;
Name = Domain.Name;
var dc = Domain.FindDomainController();
@@ -63,26 +63,26 @@ namespace Disco.Services.Interop.ActiveDirectory
using (var adRootDSE = new DirectoryEntry(ldapPath + "RootDSE"))
{
this.DistinguishedName = adRootDSE.Properties["defaultNamingContext"][0].ToString();
this.ConfigurationNamingContext = adRootDSE.Properties["configurationNamingContext"][0].ToString();
DistinguishedName = adRootDSE.Properties["defaultNamingContext"][0].ToString();
ConfigurationNamingContext = adRootDSE.Properties["configurationNamingContext"][0].ToString();
}
using (var adDomainRoot = new DirectoryEntry(ldapPath + this.DistinguishedName))
using (var adDomainRoot = new DirectoryEntry(ldapPath + DistinguishedName))
{
this.SecurityIdentifier = new SecurityIdentifier((byte[])(adDomainRoot.Properties["objectSid"][0]), 0);
SecurityIdentifier = new SecurityIdentifier((byte[])(adDomainRoot.Properties["objectSid"][0]), 0);
}
using (var configSearchRoot = new DirectoryEntry(ldapPath + "CN=Partitions," + this.ConfigurationNamingContext))
using (var configSearchRoot = new DirectoryEntry(ldapPath + "CN=Partitions," + ConfigurationNamingContext))
{
var configSearchFilter = string.Format("(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))", this.Name);
var configSearchFilter = string.Format("(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))", Name);
using (var configSearcher = new DirectorySearcher(configSearchRoot, configSearchFilter, new string[] { "NetBIOSName" }, System.DirectoryServices.SearchScope.OneLevel))
{
SearchResult configResult = configSearcher.FindOne();
if (configResult != null)
this.NetBiosName = configResult.Properties["NetBIOSName"][0].ToString();
NetBiosName = configResult.Properties["NetBIOSName"][0].ToString();
else
this.NetBiosName = null;
NetBiosName = null;
}
}
}
@@ -91,17 +91,17 @@ namespace Disco.Services.Interop.ActiveDirectory
public IEnumerable<ADDomainController> GetAllReachableDomainControllers()
{
return this.Domain.FindAllDomainControllers().WhereReachable().Select(dc => new ADDomainController(this.context, dc, this, dc.SiteName == this.context.Site.Name, false));
return Domain.FindAllDomainControllers().WhereReachable().Select(dc => new ADDomainController(context, dc, this, dc.SiteName == context.Site.Name, false));
}
public IEnumerable<ADDomainController> GetReachableSiteDomainControllers()
{
return this.DomainControllers.Where(dc => dc.IsSiteServer && dc.DomainController.IsReachable());
return DomainControllers.Where(dc => dc.IsSiteServer && dc.DomainController.IsReachable());
}
public ADDomainController GetAvailableDomainController(bool RequireWritable = false)
{
if (this.domainMaintenanceNext < DateTime.Now)
if (domainMaintenanceNext < DateTime.Now)
MaintainDomainControllers();
IEnumerable<ADDomainController> availableServers;
@@ -144,7 +144,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
private IEnumerable<ADDomainController> AvailableDomainControllers(bool RequireSiteServer, bool RequireWritable)
{
IEnumerable<ADDomainController> query = this.DomainControllers.Where(dc => dc.IsAvailable);
IEnumerable<ADDomainController> query = DomainControllers.Where(dc => dc.IsAvailable);
if (RequireSiteServer)
query = query.Where(dc => dc.IsSiteServer);
if (RequireWritable)
@@ -160,11 +160,11 @@ namespace Disco.Services.Interop.ActiveDirectory
else
locatorOptions = LocatorOptions.ForceRediscovery;
var dc = this.Domain.FindDomainController(locatorOptions);
var dc = Domain.FindDomainController(locatorOptions);
var dcName = dc.Name;
var existingDC = this.DomainControllers.FirstOrDefault(edc => edc.Name == dcName);
var existingDC = DomainControllers.FirstOrDefault(edc => edc.Name == dcName);
if (existingDC != null)
{
@@ -183,10 +183,10 @@ namespace Disco.Services.Interop.ActiveDirectory
{
// New DC discovered
var adDC = new ADDomainController(this.context, dc, this, dc.SiteName == this.context.Site.Name, RequireWritable);
var adDC = new ADDomainController(context, dc, this, dc.SiteName == context.Site.Name, RequireWritable);
// Add DC to Available Servers
this.domainControllers.Push(adDC);
domainControllers.Push(adDC);
return adDC;
}
@@ -196,7 +196,7 @@ namespace Disco.Services.Interop.ActiveDirectory
{
lock (domainMaintainLock)
{
var servers = this.domainControllers.ToList();
var servers = domainControllers.ToList();
var nonSiteServersPresent = servers.Any(s => !s.IsSiteServer);
@@ -216,13 +216,13 @@ namespace Disco.Services.Interop.ActiveDirectory
UpdateDomainControllers(servers.Where(s => s.IsSiteServer || s.IsAvailable));
}
}
this.domainMaintenanceNext = DateTime.Now.AddMinutes(DomainMaintanceIntervalMinutes);
domainMaintenanceNext = DateTime.Now.AddMinutes(DomainMaintanceIntervalMinutes);
}
}
internal void UpdateDomainControllers(IEnumerable<ADDomainController> DomainControllers)
{
this.domainControllers = new ConcurrentStack<ADDomainController>(DomainControllers);
domainControllers = new ConcurrentStack<ADDomainController>(DomainControllers);
}
#endregion
@@ -233,7 +233,7 @@ namespace Disco.Services.Interop.ActiveDirectory
throw new ArgumentNullException("DistinguishedName");
if (!DistinguishedName.EndsWith(this.DistinguishedName, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format("The Distinguished Name ({0}) isn't a member of this domain [{1}]", DistinguishedName, this.Name), "DistinguishedName");
throw new ArgumentException(string.Format("The Distinguished Name ({0}) isn't a member of this domain [{1}]", DistinguishedName, Name), "DistinguishedName");
var dc = GetAvailableDomainController();
@@ -244,12 +244,12 @@ namespace Disco.Services.Interop.ActiveDirectory
public IEnumerable<ADSearchResult> SearchEntireDomain(string LdapFilter, string[] LoadProperties, int? ResultLimit = null)
{
return SearchInternal(this.DistinguishedName, LdapFilter, LoadProperties, ResultLimit);
return SearchInternal(DistinguishedName, LdapFilter, LoadProperties, ResultLimit);
}
public IEnumerable<ADSearchResult> SearchScope(string LdapFilter, string[] LoadProperties, int? ResultLimit = null)
{
var searchScope = this.SearchContainers;
var searchScope = SearchContainers;
// No scope set, search entire domain
if (searchScope == null)
@@ -322,11 +322,11 @@ namespace Disco.Services.Interop.ActiveDirectory
internal void UpdateSearchContainers(List<string> Containers)
{
this.SearchContainers = Containers ?? new List<string>();
SearchContainers = Containers ?? new List<string>();
}
internal void UpdateSearchEntireDomain()
{
this.SearchContainers = null;
SearchContainers = null;
}
#region Helpers
@@ -335,7 +335,7 @@ namespace Disco.Services.Interop.ActiveDirectory
{
get
{
return string.Format("CN=Computers,{0}", this.DistinguishedName);
return string.Format("CN=Computers,{0}", DistinguishedName);
}
}
@@ -346,7 +346,7 @@ namespace Disco.Services.Interop.ActiveDirectory
StringBuilder name = new StringBuilder();
name.Append('[').Append(this.NetBiosName).Append(']');
name.Append('[').Append(NetBiosName).Append(']');
var subDN = DistinguishedName.Substring(0, DistinguishedName.Length - this.DistinguishedName.Length);
var subDNComponents = subDN.Split(',');
@@ -365,7 +365,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
return string.Format("{0} [{1}]", this.Name, this.NetBiosName);
return string.Format("{0} [{1}]", Name, NetBiosName);
}
public override bool Equals(object obj)
@@ -373,11 +373,11 @@ namespace Disco.Services.Interop.ActiveDirectory
if (obj == null || !(obj is ADDomain))
return false;
else
return this.DistinguishedName == ((ADDomain)obj).DistinguishedName;
return DistinguishedName == ((ADDomain)obj).DistinguishedName;
}
public override int GetHashCode()
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(this.DistinguishedName);
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(DistinguishedName);
}
}
}
@@ -45,18 +45,18 @@ namespace Disco.Services.Interop.ActiveDirectory
public ADDomainController(ActiveDirectoryContext Context, DomainController DomainController, ADDomain Domain, bool IsSiteServer, bool IsWritable)
{
this.context = Context;
context = Context;
this.Domain = Domain;
this.DomainController = DomainController;
this.Name = DomainController.Name;
this.SiteName = DomainController.SiteName;
Name = DomainController.Name;
SiteName = DomainController.SiteName;
this.IsSiteServer = IsSiteServer;
this.IsWritable = IsWritable;
this.AvailableWhen = null;
AvailableWhen = null;
}
public ADDirectoryEntry RetrieveDirectoryEntry(string DistinguishedName, string[] LoadProperties = null)
@@ -64,15 +64,15 @@ namespace Disco.Services.Interop.ActiveDirectory
if (string.IsNullOrWhiteSpace(DistinguishedName))
throw new ArgumentNullException("DistinguishedName");
if (!DistinguishedName.EndsWith(this.Domain.DistinguishedName, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format("The Distinguished Name ({0}) isn't a member of this domain [{1}]", DistinguishedName, this.Domain.Name), "DistinguishedName");
if (!DistinguishedName.EndsWith(Domain.DistinguishedName, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format("The Distinguished Name ({0}) isn't a member of this domain [{1}]", DistinguishedName, Domain.Name), "DistinguishedName");
var entry = new DirectoryEntry(string.Format(LdapPathTemplate, this.Name, ADHelpers.EscapeDistinguishedName(DistinguishedName)));
var entry = new DirectoryEntry(string.Format(LdapPathTemplate, Name, ADHelpers.EscapeDistinguishedName(DistinguishedName)));
if (LoadProperties != null)
entry.RefreshCache(LoadProperties);
return new ADDirectoryEntry(this.Domain, this, entry);
return new ADDirectoryEntry(Domain, this, entry);
}
#region Searching
@@ -83,7 +83,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public IEnumerable<ADSearchResult> SearchScope(string LdapFilter, string[] LoadProperties, int? ResultLimit = null)
{
var searchScope = this.Domain.SearchContainers;
var searchScope = Domain.SearchContainers;
// No scope set, search entire domain
if (searchScope == null)
@@ -109,7 +109,7 @@ namespace Disco.Services.Interop.ActiveDirectory
if (ResultLimit.HasValue && ResultLimit.Value < 1)
throw new ArgumentOutOfRangeException("ResultLimit", "The ResultLimit must be 1 or greater");
using (ADDirectoryEntry rootEntry = this.RetrieveDirectoryEntry(SearchRoot))
using (ADDirectoryEntry rootEntry = RetrieveDirectoryEntry(SearchRoot))
{
using (DirectorySearcher searcher = new DirectorySearcher(rootEntry.Entry, LdapFilter, LoadProperties, System.DirectoryServices.SearchScope.Subtree))
{
@@ -174,7 +174,7 @@ namespace Disco.Services.Interop.ActiveDirectory
{
ldapFilter = string.Format(ADMachineAccount.LdapNetbootGuidSingleFilterTemplate, MacAddressNetbootGUID.Value.ToLdapQueryFormat());
}
adResult = this.SearchEntireDomain(ldapFilter, loadProperites, ActiveDirectory.SingleSearchResult).FirstOrDefault();
adResult = SearchEntireDomain(ldapFilter, loadProperites, ActiveDirectory.SingleSearchResult).FirstOrDefault();
}
if (adResult != null)
@@ -212,7 +212,7 @@ namespace Disco.Services.Interop.ActiveDirectory
? ADGroup.LoadProperties.Concat(AdditionalProperties).ToArray()
: ADGroup.LoadProperties;
using (var groupEntry = this.RetrieveDirectoryEntry(DistinguishedName, loadProperites))
using (var groupEntry = RetrieveDirectoryEntry(DistinguishedName, loadProperites))
{
if (groupEntry == null)
return null;
@@ -224,8 +224,8 @@ namespace Disco.Services.Interop.ActiveDirectory
{
if (SecurityIdentifier == null)
throw new ArgumentNullException("SecurityIdentifier");
if (!SecurityIdentifier.IsEqualDomainSid(this.Domain.SecurityIdentifier))
throw new ArgumentException(string.Format("The specified Security Identifier [{0}] does not belong to this domain [{1}]", SecurityIdentifier.ToString(), this.Domain.Name), "SecurityIdentifier");
if (!SecurityIdentifier.IsEqualDomainSid(Domain.SecurityIdentifier))
throw new ArgumentException(string.Format("The specified Security Identifier [{0}] does not belong to this domain [{1}]", SecurityIdentifier.ToString(), Domain.Name), "SecurityIdentifier");
var sidBinaryString = SecurityIdentifier.ToBinaryString();
@@ -234,7 +234,7 @@ namespace Disco.Services.Interop.ActiveDirectory
? ADGroup.LoadProperties.Concat(AdditionalProperties).ToArray()
: ADGroup.LoadProperties;
var result = this.SearchEntireDomain(ldapFilter, loadProperites, ActiveDirectory.SingleSearchResult).FirstOrDefault();
var result = SearchEntireDomain(ldapFilter, loadProperites, ActiveDirectory.SingleSearchResult).FirstOrDefault();
if (result == null)
return null;
else
@@ -280,7 +280,7 @@ namespace Disco.Services.Interop.ActiveDirectory
{
Dictionary<string, List<ADOrganisationalUnit>> resultTree = new Dictionary<string, List<ADOrganisationalUnit>>();
var unsortedOrganisationalUnits = this.SearchEntireDomain(OrganisationalUnitsLdapFilter, OrganisationalUnitsLoadProperties)
var unsortedOrganisationalUnits = SearchEntireDomain(OrganisationalUnitsLdapFilter, OrganisationalUnitsLoadProperties)
.Select(r => r.AsADOrganisationalUnit()).ToList();
var indexedOrganisationalUnits = unsortedOrganisationalUnits.ToDictionary(k => k.DistinguishedName);
@@ -307,12 +307,12 @@ namespace Disco.Services.Interop.ActiveDirectory
{
var slashIndex = Id.IndexOf('\\');
if (!this.Domain.NetBiosName.Equals(Id.Substring(0, slashIndex), StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format("The Id [{0}] is invalid for this domain [{1}]", Id, this.Domain.Name), "Id");
if (!Domain.NetBiosName.Equals(Id.Substring(0, slashIndex), StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format("The Id [{0}] is invalid for this domain [{1}]", Id, Domain.Name), "Id");
var ldapFilter = string.Format(LdapFilterTemplate, Id.Substring(slashIndex + 1));
return this.SearchEntireDomain(ldapFilter, LoadProperties, ActiveDirectory.SingleSearchResult).FirstOrDefault();
return SearchEntireDomain(ldapFilter, LoadProperties, ActiveDirectory.SingleSearchResult).FirstOrDefault();
}
#endregion
@@ -321,7 +321,7 @@ namespace Disco.Services.Interop.ActiveDirectory
{
using (Ping p = new Ping())
{
var pr = p.Send(this.Name, 1000);
var pr = p.Send(Name, 1000);
return (pr.Status == IPStatus.Success);
}
}
@@ -331,8 +331,8 @@ namespace Disco.Services.Interop.ActiveDirectory
if (MachineAccount != null && MachineAccount.IsCriticalSystemObject)
throw new InvalidOperationException(string.Format("This account {0} is a Critical System Active Directory Object and Disco refuses to modify it", MachineAccount.DistinguishedName));
if (!this.IsWritable)
throw new InvalidOperationException(string.Format("The domain controller [{0}] is not writable. This action (Offline Domain Join Provision) requires a writable domain controller.", this.Name));
if (!IsWritable)
throw new InvalidOperationException(string.Format("The domain controller [{0}] is not writable. This action (Offline Domain Join Provision) requires a writable domain controller.", Name));
StringBuilder diagnosticInfo = new StringBuilder();
string DJoinResult = null;
@@ -349,7 +349,7 @@ namespace Disco.Services.Interop.ActiveDirectory
{
try
{
using (var deOU = this.RetrieveDirectoryEntry(OrganisationalUnit, new string[] { "distinguishedName" }))
using (var deOU = RetrieveDirectoryEntry(OrganisationalUnit, new string[] { "distinguishedName" }))
{
if (deOU == null)
throw new Exception(string.Format("OU's Directory Entry couldn't be found at [{0}]", OrganisationalUnit));
@@ -367,8 +367,8 @@ namespace Disco.Services.Interop.ActiveDirectory
string tempFileName = System.IO.Path.GetTempFileName();
string argumentOU = (!string.IsNullOrWhiteSpace(OrganisationalUnit)) ? string.Format(" /MACHINEOU \"{0}\"", OrganisationalUnit) : string.Empty;
string arguments = string.Format("/PROVISION /DOMAIN \"{0}\" /DCNAME \"{1}\" /MACHINE \"{2}\"{3} /REUSE /SAVEFILE \"{4}\"",
this.Domain.Name,
this.Name,
Domain.Name,
Name,
ComputerSamAccountName,
argumentOU,
tempFileName
@@ -409,7 +409,7 @@ namespace Disco.Services.Interop.ActiveDirectory
DiagnosticInformation = diagnosticInfo.ToString();
// Reload Machine Account
MachineAccount = this.RetrieveADMachineAccount(string.Format(@"{0}\{1}", this.Domain.NetBiosName, ComputerSamAccountName), (MachineAccount == null ? null : MachineAccount.LoadedProperties.Keys.ToArray()));
MachineAccount = RetrieveADMachineAccount(string.Format(@"{0}\{1}", Domain.NetBiosName, ComputerSamAccountName), (MachineAccount == null ? null : MachineAccount.LoadedProperties.Keys.ToArray()));
return DJoinResult;
}
@@ -417,7 +417,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
return this.Name;
return Name;
}
public override bool Equals(object obj)
@@ -425,11 +425,11 @@ namespace Disco.Services.Interop.ActiveDirectory
if (obj == null || !(obj is ADDomainController))
return false;
else
return this.Name == ((ADDomainController)obj).Name;
return Name == ((ADDomainController)obj).Name;
}
public override int GetHashCode()
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(this.Name);
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(Name);
}
}
}
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Security.Principal;
@@ -22,7 +21,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public string SamAccountName { get; private set; }
public string Name { get; private set; }
public string DisplayName { get { return this.Name; } }
public string DisplayName { get { return Name; } }
public List<string> MemberOf { get; private set; }
@@ -105,18 +104,18 @@ namespace Disco.Services.Interop.ActiveDirectory
switch (PropertyName.ToLower())
{
case "name":
return new string[] { this.Name }.OfType<T>();
return new string[] { Name }.OfType<T>();
case "samaccountname":
return new string[] { this.SamAccountName }.OfType<T>();
return new string[] { SamAccountName }.OfType<T>();
case "distinguishedname":
return new string[] { this.DistinguishedName }.OfType<T>();
return new string[] { DistinguishedName }.OfType<T>();
case "objectsid":
return new SecurityIdentifier[] { this.SecurityIdentifier }.OfType<T>();
return new SecurityIdentifier[] { SecurityIdentifier }.OfType<T>();
case "memberof":
return this.MemberOf.OfType<T>();
return MemberOf.OfType<T>();
default:
object[] adProperty;
if (this.LoadedProperties.TryGetValue(PropertyName, out adProperty))
if (LoadedProperties.TryGetValue(PropertyName, out adProperty))
return adProperty.OfType<T>();
else
return Enumerable.Empty<T>();
@@ -125,7 +124,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
return this.Id;
return Id;
}
public override bool Equals(object obj)
@@ -133,11 +132,11 @@ namespace Disco.Services.Interop.ActiveDirectory
if (obj == null || !(obj is ADGroup))
return false;
else
return this.DistinguishedName == ((ADGroup)obj).DistinguishedName;
return DistinguishedName == ((ADGroup)obj).DistinguishedName;
}
public override int GetHashCode()
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(this.DistinguishedName);
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(DistinguishedName);
}
}
}
@@ -36,7 +36,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public ADGroup GetGroup()
{
return ActiveDirectory.RetrieveADGroup(this.Configuration.GroupId, "member");
return ActiveDirectory.RetrieveADGroup(Configuration.GroupId, "member");
}
protected void AddMember(string Id)
@@ -1,5 +1,4 @@
using Disco.Data.Repository;
using Disco.Services.Logging;
using Disco.Services.Tasks;
using Quartz;
using System;
@@ -20,23 +19,23 @@ namespace Disco.Services.Interop.ActiveDirectory
TriggerBuilder triggerBuilder = TriggerBuilder.Create().
WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(23, 0));
this.ScheduleTask(triggerBuilder);
ScheduleTask(triggerBuilder);
}
protected override void ExecuteTask()
{
int changeCount;
List<ADManagedGroup> managedGroups = this.ExecutionContext.JobDetail.JobDataMap["ManagedGroups"] as List<ADManagedGroup>;
List<ADManagedGroup> managedGroups = ExecutionContext.JobDetail.JobDataMap["ManagedGroups"] as List<ADManagedGroup>;
if (managedGroups == null)
managedGroups = ActiveDirectory.Context.ManagedGroups.Values;
this.Status.UpdateStatus(0, "Synchronising Active Directory Managed Groups", "Starting");
Status.UpdateStatus(0, "Synchronising Active Directory Managed Groups", "Starting");
changeCount = ActiveDirectory.Context.ManagedGroups.SyncManagedGroups(managedGroups, this.Status);
changeCount = ActiveDirectory.Context.ManagedGroups.SyncManagedGroups(managedGroups, Status);
Status.LogInformation($"Synchronised Active Directory Managed Groups, {changeCount:N0} changes made");
this.Status.SetFinishedMessage(string.Format("Made {0} Changes to Active Directory Groups", changeCount));
Status.SetFinishedMessage(string.Format("Made {0} Changes to Active Directory Groups", changeCount));
}
public static ScheduledTaskStatus ScheduleSync(ADManagedGroup ManagedGroup)
@@ -27,10 +27,10 @@ namespace Disco.Services.Interop.ActiveDirectory
{
int changeCount;
this.Status.UpdateStatus(1, "Starting", "Connecting to the Database and initializing the environment");
Status.UpdateStatus(1, "Starting", "Connecting to the Database and initializing the environment");
using (DiscoDataContext database = new DiscoDataContext())
{
UpdateLastNetworkLogonDates(database, this.Status);
UpdateLastNetworkLogonDates(database, Status);
Status.UpdateStatus(95, "Updating Database", "Writing last network logon dates to the Database");
changeCount = database.SaveChanges();
Status.Finished(string.Format("{0} Device last network logon dates updated", changeCount), "/Config/SystemConfig");
@@ -28,7 +28,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
return this.Name;
return Name;
}
public override bool Equals(object obj)
@@ -36,11 +36,11 @@ namespace Disco.Services.Interop.ActiveDirectory
if (obj == null || !(obj is ADOrganisationalUnit))
return false;
else
return this.DistinguishedName == ((ADOrganisationalUnit)obj).DistinguishedName;
return DistinguishedName == ((ADOrganisationalUnit)obj).DistinguishedName;
}
public override int GetHashCode()
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(this.DistinguishedName);
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(DistinguishedName);
}
}
}
@@ -18,14 +18,14 @@ namespace Disco.Services.Interop.ActiveDirectory
public ADSearchResult(ADDomain Domain, ADDomainController DomainController, string SearchPath, string LdapFilter, SearchResult Result)
{
this._result = Result;
_result = Result;
this.Domain = Domain;
this.DomainController = DomainController;
this.SearchPath = SearchPath;
this.LdapFilter = LdapFilter;
this.LdapPath = _result.Path;
this.DistinguishedName = Value<string>("dn");
LdapPath = _result.Path;
DistinguishedName = Value<string>("dn");
}
public bool Contains(string PropertyName)
@@ -47,7 +47,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
return this.LdapPath;
return LdapPath;
}
}
}
@@ -16,12 +16,12 @@ namespace Disco.Services.Interop.ActiveDirectory
public ADSite(ActiveDirectoryContext Context, ActiveDirectorySite Site)
{
this.context = Context;
context = Context;
this.Site = Site;
this.Name = Site.Name;
this.DomainControllers = null;
Name = Site.Name;
DomainControllers = null;
}
internal void UpdateDomainControllers(IEnumerable<ADDomainController> DomainControllers)
@@ -31,7 +31,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public override string ToString()
{
return this.Name;
return Name;
}
public override bool Equals(object obj)
@@ -39,11 +39,11 @@ namespace Disco.Services.Interop.ActiveDirectory
if (obj == null || !(obj is ADSite))
return false;
else
return this.Name == ((ADSite)obj).Name;
return Name == ((ADSite)obj).Name;
}
public override int GetHashCode()
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(this.Name);
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(Name);
}
}
}
@@ -2,11 +2,8 @@
using Disco.Models.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Services.Interop.ActiveDirectory
{
@@ -1,11 +1,9 @@
using Disco.Data.Repository;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Services.Interop.ActiveDirectory
@@ -53,22 +51,22 @@ namespace Disco.Services.Interop.ActiveDirectory
private void Initialize(DiscoDataContext Database)
{
// Search Entire Forest (default: true)
this._SearchAllForestServers = Database.DiscoConfiguration.ActiveDirectory.SearchAllForestServers ?? true;
_SearchAllForestServers = Database.DiscoConfiguration.ActiveDirectory.SearchAllForestServers ?? true;
// Set Search LDAP Filters
InitializeWildcardSearchSufixOnly(Database.DiscoConfiguration.ActiveDirectory.SearchWildcardSuffixOnly);
// Determine Site
var computerSite = ActiveDirectorySite.GetComputerSite();
this.Site = new ADSite(this, computerSite);
Site = new ADSite(this, computerSite);
// Determine Domains
var computerDomain = Domain.GetComputerDomain();
this.Domains = computerDomain.Forest.Domains
Domains = computerDomain.Forest.Domains
.Cast<Domain>()
.Select(d => new ADDomain(this, d))
.ToList();
this.PrimaryDomain = this.Domains.Where(d => d.Name == computerDomain.Name).First();
PrimaryDomain = Domains.Where(d => d.Name == computerDomain.Name).First();
// Determine Search Scope Containers
ReinitializeSearchContainers(Database.DiscoConfiguration.ActiveDirectory.SearchContainers);
@@ -80,7 +78,7 @@ namespace Disco.Services.Interop.ActiveDirectory
.Select(dc => new ADDomainController(this, dc, GetDomainByName(dc.Domain.Name), IsSiteServer: true, IsWritable: false));
Site.UpdateDomainControllers(siteDomainControllers);
this.Domains.ForEach(domain => domain.UpdateDomainControllers(siteDomainControllers.Where(dc => dc.Domain == domain)));
Domains.ForEach(domain => domain.UpdateDomainControllers(siteDomainControllers.Where(dc => dc.Domain == domain)));
}
#endregion
@@ -90,7 +88,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public bool TryGetDomainFromDistinguishedName(string DistinguishedName, out ADDomain Domain)
{
// Find closest match
Domain = this.Domains.Where(d => DistinguishedName.EndsWith(d.DistinguishedName, StringComparison.OrdinalIgnoreCase))
Domain = Domains.Where(d => DistinguishedName.EndsWith(d.DistinguishedName, StringComparison.OrdinalIgnoreCase))
.OrderByDescending(d => d.DistinguishedName.Length).FirstOrDefault();
return (Domain != null);
@@ -105,7 +103,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public bool TryGetDomainByNetBiosName(string NetBiosName, out ADDomain Domain)
{
Domain = this.Domains.FirstOrDefault(d => d.NetBiosName.Equals(NetBiosName, StringComparison.OrdinalIgnoreCase));
Domain = Domains.FirstOrDefault(d => d.NetBiosName.Equals(NetBiosName, StringComparison.OrdinalIgnoreCase));
return (Domain != null);
}
public ADDomain GetDomainByNetBiosName(string NetBiosName)
@@ -118,7 +116,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public bool TryGetDomainByName(string Name, out ADDomain Domain)
{
Domain = this.Domains.FirstOrDefault(d => d.Name.Equals(Name, StringComparison.OrdinalIgnoreCase));
Domain = Domains.FirstOrDefault(d => d.Name.Equals(Name, StringComparison.OrdinalIgnoreCase));
return (Domain != null);
}
public ADDomain GetDomainByName(string Name)
@@ -131,7 +129,7 @@ namespace Disco.Services.Interop.ActiveDirectory
public bool TryGetDomainFromSecurityIdentifier(SecurityIdentifier SecurityIdentifier, out ADDomain Domain)
{
Domain = this.Domains.FirstOrDefault(d => d.SecurityIdentifier.IsEqualDomainSid(SecurityIdentifier));
Domain = Domains.FirstOrDefault(d => d.SecurityIdentifier.IsEqualDomainSid(SecurityIdentifier));
return (Domain != null);
}
public ADDomain GetDomainFromSecurityIdentifier(SecurityIdentifier SecurityIdentifier)
@@ -184,14 +182,14 @@ namespace Disco.Services.Interop.ActiveDirectory
public IEnumerable<ADSearchResult> SearchEntireForest(string LdapFilter, string[] LoadProperties, int? ResultLimit = null)
{
var queries = this.Domains.Select(d => Tuple.Create(d, d.DistinguishedName));
var queries = Domains.Select(d => Tuple.Create(d, d.DistinguishedName));
return SearchInternal(queries, LdapFilter, LoadProperties, ResultLimit);
}
public IEnumerable<ADSearchResult> SearchScope(string LdapFilter, string[] LoadProperties, int? ResultLimit = null)
{
var queries = this.Domains.SelectMany(
var queries = Domains.SelectMany(
d => d.SearchContainers ?? new List<string>() { d.DistinguishedName },
(d, scope) => Tuple.Create(d, scope));
@@ -256,7 +254,7 @@ namespace Disco.Services.Interop.ActiveDirectory
if (SearchAllForestServers == false)
{
Database.DiscoConfiguration.ActiveDirectory.SearchAllForestServers = false;
this._SearchAllForestServers = false;
_SearchAllForestServers = false;
return true;
}
else
@@ -265,13 +263,13 @@ namespace Disco.Services.Interop.ActiveDirectory
if (forestServers.Count <= ActiveDirectory.MaxForestServerSearch)
{
Database.DiscoConfiguration.ActiveDirectory.SearchAllForestServers = true;
this._SearchAllForestServers = true;
_SearchAllForestServers = true;
return true;
}
else
{
Database.DiscoConfiguration.ActiveDirectory.SearchAllForestServers = false;
this._SearchAllForestServers = false;
_SearchAllForestServers = false;
return false;
}
}
@@ -315,18 +313,18 @@ namespace Disco.Services.Interop.ActiveDirectory
if (Containers == null)
{
// No search restrictions (search entire domain)
foreach (var domain in this.Domains)
foreach (var domain in Domains)
domain.UpdateSearchEntireDomain();
}
else
{
// Restrict search containers
var searchContainerDomains = Containers.Join(this.Domains, ok => ok.Key, ik => ik.Name, (o, i) => Tuple.Create(o, i), StringComparer.OrdinalIgnoreCase);
var searchContainerDomains = Containers.Join(Domains, ok => ok.Key, ik => ik.Name, (o, i) => Tuple.Create(o, i), StringComparer.OrdinalIgnoreCase);
foreach (var domainContainers in searchContainerDomains)
domainContainers.Item2.UpdateSearchContainers(domainContainers.Item1.Value);
// Ignore domains without configured containers
var unconfiguredContainers = this.Domains.Except(searchContainerDomains.Select(sc => sc.Item2));
var unconfiguredContainers = Domains.Except(searchContainerDomains.Select(sc => sc.Item2));
foreach (var domain in unconfiguredContainers)
domain.UpdateSearchContainers(new List<string>());
}
@@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Services.Interop.ActiveDirectory
@@ -21,8 +20,8 @@ namespace Disco.Services.Interop.ActiveDirectory
public ActiveDirectoryGroupCache()
{
this.securityIdentifierCache = new ConcurrentDictionary<SecurityIdentifier, Tuple<ADGroup, DateTime>>();
this.distinguishedNameCache = new ConcurrentDictionary<string, Tuple<ADGroup, DateTime>>(StringComparer.OrdinalIgnoreCase);
securityIdentifierCache = new ConcurrentDictionary<SecurityIdentifier, Tuple<ADGroup, DateTime>>();
distinguishedNameCache = new ConcurrentDictionary<string, Tuple<ADGroup, DateTime>>(StringComparer.OrdinalIgnoreCase);
cacheCleanNext = DateTime.Now.AddMinutes(CacheCleanIntervalMinutes);
}
@@ -3,8 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using Disco.Data.Repository;
namespace Disco.Services.Interop.ActiveDirectory
@@ -436,7 +434,7 @@ namespace Disco.Services.Interop.ActiveDirectory
this.ManagedGroup = ManagedGroup;
this.ActionType = ActionType;
this.InvokingIdentifier = InvokingIdentifier;
this.memberResolver = MemberResolver;
memberResolver = MemberResolver;
}
public IEnumerable<ADManagedGroupScheduledActionItem> ResolveMembers(DiscoDataContext Database)
@@ -448,14 +446,14 @@ namespace Disco.Services.Interop.ActiveDirectory
return Enumerable.Empty<ADManagedGroupScheduledActionItem>();
else
return members.Select(m =>
new ADManagedGroupScheduledActionItem(this.ManagedGroup, this.ActionType, m)
new ADManagedGroupScheduledActionItem(ManagedGroup, ActionType, m)
);
}
else
{
return new ADManagedGroupScheduledActionItem[]
{
new ADManagedGroupScheduledActionItem(this.ManagedGroup, this.ActionType, this.InvokingIdentifier)
new ADManagedGroupScheduledActionItem(ManagedGroup, ActionType, InvokingIdentifier)
};
}
}
@@ -1,9 +1,7 @@
using Disco.Data.Repository;
using Disco.Services.Tasks;
using Newtonsoft.Json;
using Quartz;
using System;
using System.IO;
using System.Linq;
namespace Disco.Services.Interop.DiscoServices
@@ -25,7 +23,7 @@ namespace Disco.Services.Interop.DiscoServices
TriggerBuilder triggerBuilder = TriggerBuilder.Create().
WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(rndHour, rndMinute));
this.ScheduleTask(triggerBuilder);
ScheduleTask(triggerBuilder);
}
protected override void ExecuteTask()
@@ -34,7 +32,7 @@ namespace Disco.Services.Interop.DiscoServices
{
Status.UpdateStatus(1, "Updating Plugin Library Manifest", "Initializing");
var manifest = PluginLibrary.UpdateManifest(database, this.Status);
var manifest = PluginLibrary.UpdateManifest(database, Status);
Status.SetFinishedMessage("The Plugin Library Manifest was updated successfully");
}
@@ -23,7 +23,7 @@ namespace Disco.Services.Interop.DiscoServices
TriggerBuilder triggerBuilder = TriggerBuilder.Create().
WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(rndHour, rndMinute));
this.ScheduleTask(triggerBuilder);
ScheduleTask(triggerBuilder);
}
protected override void ExecuteTask()