feature: dont disable a computer account if a duplicate, commissioned device exists

This commit is contained in:
Gary Sharp
2024-12-11 15:11:33 +11:00
parent cb354cd13e
commit f36871abe2
@@ -6,6 +6,7 @@ using Disco.Services.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Entity;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
@@ -100,33 +101,42 @@ namespace Disco.Services.Devices.Importing.Fields
{ {
if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId)) if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId))
{ {
var adAccount = Device.ActiveDirectoryAccount(); // Don't disable if another active device with the same name exists
var duplicateNamedDevice = Database.Devices
if (adAccount != null && !adAccount.IsCriticalSystemObject) .Where(i => i.DeviceDomainId == Device.DeviceDomainId &&
i.SerialNumber != Device.SerialNumber &&
i.DecommissionedDate == null)
.Any();
if (!duplicateNamedDevice)
{ {
if (Device.DecommissionedDate.HasValue) var adAccount = Device.ActiveDirectoryAccount();
{
// Disable AD Account
adAccount.DisableAccount();
}
else
{
// Enable AD Account
adAccount.EnableAccount();
}
if (!DeviceADDescriptionSet) if (adAccount != null && !adAccount.IsCriticalSystemObject)
{ {
try if (Device.DecommissionedDate.HasValue)
{ {
adAccount.SetDescription(Device); // Disable AD Account
adAccount.DisableAccount();
} }
catch (Exception ex) else
{ {
SystemLog.LogWarning($"Unable to update AD Machine Account Description for {Device.DeviceDomainId}: {ex.Message}"); // Enable AD Account
throw; adAccount.EnableAccount();
}
if (!DeviceADDescriptionSet)
{
try
{
adAccount.SetDescription(Device);
}
catch (Exception ex)
{
SystemLog.LogWarning($"Unable to update AD Machine Account Description for {Device.DeviceDomainId}: {ex.Message}");
throw;
}
DeviceADDescriptionSet = true;
} }
DeviceADDescriptionSet = true;
} }
} }
} }