qol: inline variable declaration

This commit is contained in:
Gary Sharp
2025-07-20 15:12:33 +10:00
parent 5792771ea1
commit 1add4ee0f5
89 changed files with 1229 additions and 1512 deletions
@@ -85,8 +85,7 @@ namespace Disco.Services.Interop.ActiveDirectory
foreach (var domainGroup in devices.GroupBy(d => d.ComputerDomainName).ToList())
{
ADDomain domain;
if (domainGroup.Key != null && ActiveDirectory.Context.TryGetDomainByNetBiosName(domainGroup.Key, out domain))
if (domainGroup.Key != null && ActiveDirectory.Context.TryGetDomainByNetBiosName(domainGroup.Key, out var domain))
{
var controller = domain.GetAvailableDomainController(RequireWritable: true);
@@ -311,8 +311,7 @@ namespace Disco.Services.Interop.ActiveDirectory
// Link Children
foreach (var ouChildren in indexedChildren)
{
ADOrganisationalUnit ouParent;
if (indexedOrganisationalUnits.TryGetValue(ouChildren.Key, out ouParent))
if (indexedOrganisationalUnits.TryGetValue(ouChildren.Key, out var ouParent))
{
ouParent.Children = ouChildren.Value.OrderBy(o => o.Name).ToList();
}
@@ -121,8 +121,7 @@ namespace Disco.Services.Interop.ActiveDirectory
case "member":
return Members.OfType<T>();
default:
object[] adProperty;
if (LoadedProperties.TryGetValue(PropertyName, out adProperty))
if (LoadedProperties.TryGetValue(PropertyName, out var adProperty))
return adProperty.OfType<T>();
else
return Enumerable.Empty<T>();
@@ -195,8 +195,7 @@ namespace Disco.Services.Interop.ActiveDirectory
case "userAccountControl":
return new int[] { (int)UserAccountControl }.OfType<T>();
default:
object[] adProperty;
if (LoadedProperties.TryGetValue(PropertyName, out adProperty))
if (LoadedProperties.TryGetValue(PropertyName, out var adProperty))
return adProperty.OfType<T>();
else
return Enumerable.Empty<T>();
@@ -59,10 +59,8 @@ namespace Disco.Services.Interop.ActiveDirectory
if (!string.IsNullOrEmpty(Device.DeviceDomainId) && Device.DeviceDomainId.Contains('\\'))
{
var context = ActiveDirectory.Context;
string deviceSamAccountName;
ADDomain deviceDomain;
ActiveDirectory.ParseDomainAccountId(Device.DeviceDomainId + "$", out deviceSamAccountName, out deviceDomain);
ActiveDirectory.ParseDomainAccountId(Device.DeviceDomainId + "$", out var deviceSamAccountName, out var deviceDomain);
var ldapFilter = string.Format(ldapFilterTemplate, ADHelpers.EscapeLdapQuery(deviceSamAccountName));
IEnumerable<ADDomainController> domainControllers;
@@ -177,8 +175,7 @@ namespace Disco.Services.Interop.ActiveDirectory
foreach (Device device in Database.Devices.Where(device => device.DeviceDomainId != null))
{
DateTime lastLogonDate;
if (queryResults.TryGetValue(device.DeviceDomainId.ToUpper(), out lastLogonDate))
if (queryResults.TryGetValue(device.DeviceDomainId.ToUpper(), out var lastLogonDate))
{
if (!device.LastNetworkLogonDate.HasValue)
device.LastNetworkLogonDate = lastLogonDate;
@@ -224,8 +224,7 @@ namespace Disco.Services.Interop.ActiveDirectory
case "userAccountControl":
return new int[] { (int)UserAccountControl }.OfType<T>();
default:
object[] adProperty;
if (LoadedProperties.TryGetValue(PropertyName, out adProperty))
if (LoadedProperties.TryGetValue(PropertyName, out var adProperty))
return adProperty.OfType<T>();
else
return Enumerable.Empty<T>();
@@ -79,8 +79,7 @@ namespace Disco.Services.Interop.ActiveDirectory
if (string.IsNullOrWhiteSpace(Term))
throw new ArgumentNullException("Term");
ADDomain searchDomain;
var term = RelevantSearchTerm(Term, out searchDomain);
var term = RelevantSearchTerm(Term, out var searchDomain);
if (string.IsNullOrWhiteSpace(term))
return Enumerable.Empty<ADUserAccount>();
@@ -139,8 +138,7 @@ namespace Disco.Services.Interop.ActiveDirectory
if (string.IsNullOrWhiteSpace(Term))
throw new ArgumentNullException("Term");
ADDomain searchDomain;
var term = RelevantSearchTerm(Term, out searchDomain);
var term = RelevantSearchTerm(Term, out var searchDomain);
if (string.IsNullOrWhiteSpace(term))
return Enumerable.Empty<ADGroup>();
@@ -200,10 +198,8 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public static string ParseDomainAccountId(string AccountId, string AccountDomain)
{
string accountUsername;
ADDomain domain;
return ParseDomainAccountId(AccountId, AccountDomain, out accountUsername, out domain);
return ParseDomainAccountId(AccountId, AccountDomain, out _, out _);
}
public static string ParseDomainAccountId(string AccountId, out string AccountUsername)
{
@@ -211,9 +207,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public static string ParseDomainAccountId(string AccountId, string AccountDomain, out string AccountUsername)
{
ADDomain domain;
return ParseDomainAccountId(AccountId, AccountDomain, out AccountUsername, out domain);
return ParseDomainAccountId(AccountId, AccountDomain, out AccountUsername, out _);
}
public static string ParseDomainAccountId(string AccountId, out ADDomain Domain)
{
@@ -221,9 +215,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public static string ParseDomainAccountId(string AccountId, string AccountDomain, out ADDomain Domain)
{
string accountUsername;
return ParseDomainAccountId(AccountId, AccountDomain, out accountUsername, out Domain);
return ParseDomainAccountId(AccountId, AccountDomain, out _, out Domain);
}
public static string ParseDomainAccountId(string AccountId, out string AccountUsername, out ADDomain Domain)
{
@@ -258,22 +250,15 @@ namespace Disco.Services.Interop.ActiveDirectory
public static bool IsValidDomainAccountId(string AccountId)
{
string accountUsername;
ADDomain domain;
return IsValidDomainAccountId(AccountId, out accountUsername, out domain);
return IsValidDomainAccountId(AccountId, out _, out _);
}
public static bool IsValidDomainAccountId(string AccountId, out string AccountUsername)
{
ADDomain domain;
return IsValidDomainAccountId(AccountId, out AccountUsername, out domain);
return IsValidDomainAccountId(AccountId, out AccountUsername, out _);
}
public static bool IsValidDomainAccountId(string AccountId, out ADDomain Domain)
{
string accountUsername;
return IsValidDomainAccountId(AccountId, out accountUsername, out Domain);
return IsValidDomainAccountId(AccountId, out _, out Domain);
}
public static bool IsValidDomainAccountId(string AccountId, out string AccountUsername, out ADDomain Domain)
{
@@ -137,8 +137,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public ADDomain GetDomainFromDistinguishedName(string DistinguishedName)
{
ADDomain domain;
if (!TryGetDomainFromDistinguishedName(DistinguishedName, out domain))
if (!TryGetDomainFromDistinguishedName(DistinguishedName, out var domain))
throw new ArgumentException($"The distinguished name is from an unknown domain: [{DistinguishedName}]", "DistinguishedName");
return domain;
}
@@ -150,8 +149,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public ADDomain GetDomainByNetBiosName(string NetBiosName)
{
ADDomain domain;
if (!TryGetDomainByNetBiosName(NetBiosName, out domain))
if (!TryGetDomainByNetBiosName(NetBiosName, out var domain))
throw new ArgumentException($"The domain for specified NetBios name is unknown [{NetBiosName}]", "NetBiosName");
return domain;
}
@@ -163,8 +161,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public ADDomain GetDomainByName(string Name)
{
ADDomain domain;
if (!TryGetDomainByName(Name, out domain))
if (!TryGetDomainByName(Name, out var domain))
throw new ArgumentException($"The domain for specified DNS name is unknown [{Name}]", "Name");
return domain;
}
@@ -176,8 +173,7 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public ADDomain GetDomainFromSecurityIdentifier(SecurityIdentifier SecurityIdentifier)
{
ADDomain domain;
if (!TryGetDomainFromSecurityIdentifier(SecurityIdentifier, out domain))
if (!TryGetDomainFromSecurityIdentifier(SecurityIdentifier, out var domain))
throw new ArgumentException($"The domain for specified Security Identifier is unknown [{SecurityIdentifier.ToString()}]", "SecurityIdentifier");
return domain;
}
@@ -328,8 +324,7 @@ namespace Disco.Services.Interop.ActiveDirectory
.Distinct()
.Select(c =>
{
ADDomain d;
if (TryGetDomainFromDistinguishedName(c, out d))
if (TryGetDomainFromDistinguishedName(c, out var d))
return Tuple.Create(d, c);
else
return null;
@@ -104,30 +104,28 @@ namespace Disco.Services.Interop.ActiveDirectory
private Tuple<ADGroup, DateTime> TryDistinguishedNameCache(string DistinguishedName)
{
Tuple<ADGroup, DateTime> groupRecord;
if (distinguishedNameCache.TryGetValue(DistinguishedName, out groupRecord))
if (distinguishedNameCache.TryGetValue(DistinguishedName, out var groupRecord))
{
if (groupRecord.Item2 > DateTime.Now)
return groupRecord;
else
{
if (distinguishedNameCache.TryRemove(DistinguishedName, out groupRecord))
securityIdentifierCache.TryRemove(groupRecord.Item1.SecurityIdentifier, out groupRecord);
securityIdentifierCache.TryRemove(groupRecord.Item1.SecurityIdentifier, out _);
}
}
return null;
}
private Tuple<ADGroup, DateTime> TrySecurityIdentifierCache(SecurityIdentifier SecurityIdentifier)
{
Tuple<ADGroup, DateTime> groupRecord;
if (securityIdentifierCache.TryGetValue(SecurityIdentifier, out groupRecord))
if (securityIdentifierCache.TryGetValue(SecurityIdentifier, out var groupRecord))
{
if (groupRecord.Item2 > DateTime.Now)
return groupRecord;
else
{
if (securityIdentifierCache.TryRemove(SecurityIdentifier, out groupRecord))
distinguishedNameCache.TryRemove(groupRecord.Item1.DistinguishedName, out groupRecord);
distinguishedNameCache.TryRemove(groupRecord.Item1.DistinguishedName, out _);
}
}
return null;
@@ -191,12 +189,11 @@ namespace Disco.Services.Interop.ActiveDirectory
var dnKeys = distinguishedNameCache.Keys.ToArray();
foreach (var dnKey in dnKeys)
{
Tuple<ADGroup, DateTime> groupRecord;
if (distinguishedNameCache.TryGetValue(dnKey, out groupRecord))
if (distinguishedNameCache.TryGetValue(dnKey, out var groupRecord))
{
if (groupRecord.Item2 <= now)
{
distinguishedNameCache.TryRemove(dnKey, out groupRecord);
distinguishedNameCache.TryRemove(dnKey, out _);
}
}
}
@@ -205,12 +202,11 @@ namespace Disco.Services.Interop.ActiveDirectory
var siKeys = securityIdentifierCache.Keys.ToArray();
foreach (var siKey in siKeys)
{
Tuple<ADGroup, DateTime> groupRecord;
if (securityIdentifierCache.TryGetValue(siKey, out groupRecord))
if (securityIdentifierCache.TryGetValue(siKey, out var groupRecord))
{
if (groupRecord.Item2 <= now)
{
securityIdentifierCache.TryRemove(siKey, out groupRecord);
securityIdentifierCache.TryRemove(siKey, out _);
}
}
}
@@ -51,9 +51,8 @@ namespace Disco.Services.Interop.ActiveDirectory
}
public bool Remove(string Key)
{
ADManagedGroup item;
if (managedGroups.TryRemove(Key, out item))
if (managedGroups.TryRemove(Key, out var item))
{
item.Dispose();
return true;
@@ -123,8 +122,7 @@ namespace Disco.Services.Interop.ActiveDirectory
.GroupBy(a => a.ManagedGroup)
.Where(g =>
{
ADManagedGroup item;
if (managedGroups.TryGetValue(g.Key.Key, out item))
if (managedGroups.TryGetValue(g.Key.Key, out var item))
return item == g.Key;
else
return false;
@@ -171,12 +169,9 @@ namespace Disco.Services.Interop.ActiveDirectory
// Discard non-existent users
var actionItems = actionGroup.Item2.Select(a =>
{
string distinguishedName;
if (!accountDNCache.TryGetValue(a.MemberId, out distinguishedName))
if (!accountDNCache.TryGetValue(a.MemberId, out var distinguishedName))
{
string memberUsername;
ADDomain memberDomain;
if (!ActiveDirectory.IsValidDomainAccountId(a.MemberId, out memberUsername, out memberDomain))
if (!ActiveDirectory.IsValidDomainAccountId(a.MemberId, out var memberUsername, out var memberDomain))
{
accountDNCache[a.MemberId] = null; // Add to cache (avoid retries)
return null;
@@ -333,12 +328,9 @@ namespace Disco.Services.Interop.ActiveDirectory
g.Item1,
g.Item2.Select(a =>
{
Tuple<string, string> definition;
if (!accountDNCache.TryGetValue(a.MemberId, out definition))
if (!accountDNCache.TryGetValue(a.MemberId, out var definition))
{
string memberUsername;
ADDomain memberDomain;
if (!ActiveDirectory.IsValidDomainAccountId(a.MemberId, out memberUsername, out memberDomain))
if (!ActiveDirectory.IsValidDomainAccountId(a.MemberId, out var memberUsername, out var memberDomain))
{
accountDNCache[a.MemberId] = null; // Add to cache (avoid retries)
return null;