skip disabling computer account during decommissioning if another active device with the same name exists

This commit is contained in:
Gary Sharp
2024-10-10 16:17:38 +11:00
parent 09f9f2d427
commit cb354cd13e
2 changed files with 15 additions and 6 deletions
@@ -80,7 +80,7 @@ namespace Disco.Services
return true; return true;
} }
public static void OnDecommission(this Device d, DecommissionReasons Reason) public static void OnDecommission(this Device d, DecommissionReasons Reason, DiscoDataContext database)
{ {
if (!d.CanDecommission()) if (!d.CanDecommission())
throw new InvalidOperationException("Decommission of Device is Denied"); throw new InvalidOperationException("Decommission of Device is Denied");
@@ -91,11 +91,20 @@ namespace Disco.Services
// Disable AD Account // Disable AD Account
if (ActiveDirectory.IsValidDomainAccountId(d.DeviceDomainId)) if (ActiveDirectory.IsValidDomainAccountId(d.DeviceDomainId))
{ {
var adAccount = d.ActiveDirectoryAccount(); // Don't disable if another active device with the same name exists
if (adAccount != null && !adAccount.IsCriticalSystemObject) var duplicateNamedDevice = database.Devices
.Where(i => i.DeviceDomainId == d.DeviceDomainId &&
i.SerialNumber != d.SerialNumber &&
i.DecommissionedDate == null)
.Any();
if (!duplicateNamedDevice)
{ {
adAccount.DisableAccount(); var adAccount = d.ActiveDirectoryAccount();
adAccount.SetDescription(d); if (adAccount != null && !adAccount.IsCriticalSystemObject)
{
adAccount.DisableAccount();
adAccount.SetDescription(d);
}
} }
} }
} }
@@ -358,7 +358,7 @@ namespace Disco.Web.Areas.API.Controllers
{ {
if (d.CanDecommission()) if (d.CanDecommission())
{ {
d.OnDecommission((DecommissionReasons)Reason); d.OnDecommission((DecommissionReasons)Reason, Database);
Database.SaveChanges(); Database.SaveChanges();
if (redirect) if (redirect)