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");
@@ -90,6 +90,14 @@ namespace Disco.Services
// Disable AD Account // Disable AD Account
if (ActiveDirectory.IsValidDomainAccountId(d.DeviceDomainId)) if (ActiveDirectory.IsValidDomainAccountId(d.DeviceDomainId))
{
// Don't disable if another active device with the same name exists
var duplicateNamedDevice = database.Devices
.Where(i => i.DeviceDomainId == d.DeviceDomainId &&
i.SerialNumber != d.SerialNumber &&
i.DecommissionedDate == null)
.Any();
if (!duplicateNamedDevice)
{ {
var adAccount = d.ActiveDirectoryAccount(); var adAccount = d.ActiveDirectoryAccount();
if (adAccount != null && !adAccount.IsCriticalSystemObject) if (adAccount != null && !adAccount.IsCriticalSystemObject)
@@ -99,6 +107,7 @@ namespace Disco.Services
} }
} }
} }
}
#endregion #endregion
#region Recommission #region Recommission
public static bool CanRecommission(this Device d) public static bool CanRecommission(this Device 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)