From f36871abe2c631989046e9bb7b4b7fc23ec00b7c Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Wed, 11 Dec 2024 15:11:33 +1100 Subject: [PATCH] feature: dont disable a computer account if a duplicate, commissioned device exists --- .../DeviceDecommissionedDateImportField.cs | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/Disco.Services/Devices/Importing/Fields/DeviceDecommissionedDateImportField.cs b/Disco.Services/Devices/Importing/Fields/DeviceDecommissionedDateImportField.cs index fe605f91..8cd547cd 100644 --- a/Disco.Services/Devices/Importing/Fields/DeviceDecommissionedDateImportField.cs +++ b/Disco.Services/Devices/Importing/Fields/DeviceDecommissionedDateImportField.cs @@ -6,6 +6,7 @@ using Disco.Services.Logging; using System; using System.Collections.Generic; using System.Data; +using System.Data.Entity; using System.Globalization; using System.Linq; @@ -100,33 +101,42 @@ namespace Disco.Services.Devices.Importing.Fields { if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId)) { - var adAccount = Device.ActiveDirectoryAccount(); - - if (adAccount != null && !adAccount.IsCriticalSystemObject) + // Don't disable if another active device with the same name exists + var duplicateNamedDevice = Database.Devices + .Where(i => i.DeviceDomainId == Device.DeviceDomainId && + i.SerialNumber != Device.SerialNumber && + i.DecommissionedDate == null) + .Any(); + if (!duplicateNamedDevice) { - if (Device.DecommissionedDate.HasValue) - { - // Disable AD Account - adAccount.DisableAccount(); - } - else - { - // Enable AD Account - adAccount.EnableAccount(); - } + var adAccount = Device.ActiveDirectoryAccount(); - 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}"); - throw; + // Enable AD Account + 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; } } }