feature: batch device decommissioning
This commit is contained in:
@@ -5,28 +5,28 @@ using Disco.Services.Devices.ManagedGroups;
|
||||
using Disco.Services.Interop.ActiveDirectory;
|
||||
using Disco.Services.Users;
|
||||
using System;
|
||||
using System.Data.Entity;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services
|
||||
{
|
||||
public static class DeviceBatchExtensions
|
||||
{
|
||||
public static bool CanDelete(this DeviceBatch db, DiscoDataContext Database)
|
||||
public static bool CanDelete(this DeviceBatch db, DiscoDataContext database)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Config.DeviceBatch.Delete))
|
||||
return false;
|
||||
|
||||
// Can't Delete if Contains Devices
|
||||
var deviceCount = Database.Devices.Count(d => d.DeviceBatchId == db.Id);
|
||||
if (deviceCount > 0)
|
||||
if (database.Devices.Any(d => d.DeviceBatchId == db.Id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void Delete(this DeviceBatch db, DiscoDataContext Database)
|
||||
public static void Delete(this DeviceBatch db, DiscoDataContext database)
|
||||
{
|
||||
if (!db.CanDelete(Database))
|
||||
if (!db.CanDelete(database))
|
||||
throw new InvalidOperationException("The state of this Device Batch doesn't allow it to be deleted");
|
||||
|
||||
// Remove Linked Group
|
||||
@@ -34,7 +34,26 @@ namespace Disco.Services
|
||||
ActiveDirectory.Context.ManagedGroups.Remove(DeviceBatchAssignedUsersManagedGroup.GetKey(db));
|
||||
|
||||
// Delete Batch
|
||||
Database.DeviceBatches.Remove(db);
|
||||
database.DeviceBatches.Remove(db);
|
||||
}
|
||||
|
||||
public static bool CanDecommission(this DeviceBatch db, DiscoDataContext database)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.Import))
|
||||
return false;
|
||||
|
||||
if (!database.Devices.Any(d => d.DeviceBatchId == db.Id && d.DecommissionedDate == null))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void Decommission(this DeviceBatch db, DiscoDataContext database, DecommissionReasons Reason, bool unassignUsers)
|
||||
{
|
||||
if (!db.CanDecommission(database))
|
||||
throw new InvalidOperationException("Decommission of Device Batch is Denied");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,8 @@ namespace Disco.Services.Devices.Importing
|
||||
public List<IDeviceImportRecord> Records { get; set; }
|
||||
public int AffectedRecords { get; set; }
|
||||
|
||||
public bool AllowBacktracking { get; } = true;
|
||||
|
||||
public int ColumnCount { get { return columns.Count; } }
|
||||
public IEnumerable<IDeviceImportColumn> Columns
|
||||
{
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using Disco.Services.Devices.Importing.Fields;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Entity;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
public class DeviceDecommissionImportContext : IDeviceImportContext
|
||||
{
|
||||
private readonly List<IDeviceImportRecord> records;
|
||||
|
||||
public string SessionId { get; }
|
||||
public string Filename { get; }
|
||||
public string DatasetName { get; }
|
||||
public int ColumnCount { get; }
|
||||
public IEnumerable<IDeviceImportColumn> Columns { get; }
|
||||
|
||||
public int RecordCount => records.Count;
|
||||
public List<IDeviceImportRecord> Records { get => records; set => throw new NotImplementedException(); }
|
||||
public int AffectedRecords { get; set; }
|
||||
public bool AllowBacktracking { get; } = false;
|
||||
|
||||
private DeviceDecommissionImportContext(string sourceName, List<Device> devices, DecommissionReasons decommissionReason, bool unassignUsers)
|
||||
{
|
||||
SessionId = Guid.NewGuid().ToString("D");
|
||||
Filename = DatasetName = sourceName;
|
||||
|
||||
var columns = new List<IDeviceImportColumn>(3)
|
||||
{
|
||||
new DeviceImportColumn()
|
||||
{
|
||||
Index = 0,
|
||||
Type = DeviceImportFieldTypes.DeviceSerialNumber,
|
||||
Handler = typeof(DeviceSerialNumberImportField),
|
||||
Name = "Device Serial Number",
|
||||
},
|
||||
new DeviceImportColumn()
|
||||
{
|
||||
Index = 1,
|
||||
Type = DeviceImportFieldTypes.DeviceDecommissionedReason,
|
||||
Handler = typeof(DeviceDecommissionedReasonImportField),
|
||||
Name = "Device Decommissioned Reason",
|
||||
}
|
||||
};
|
||||
|
||||
if (unassignUsers && devices.Any(d => d.AssignedUserId != null))
|
||||
{
|
||||
ColumnCount = 3;
|
||||
columns.Add(new DeviceImportColumn()
|
||||
{
|
||||
Index = 2,
|
||||
Type = DeviceImportFieldTypes.AssignedUserId,
|
||||
Handler = typeof(AssignedUserIdImportField),
|
||||
Name = "Assigned User Identifier",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
unassignUsers = false;
|
||||
}
|
||||
|
||||
Columns = columns;
|
||||
ColumnCount = columns.Count;
|
||||
|
||||
records = devices.Select<Device, IDeviceImportRecord>((d, i) =>
|
||||
{
|
||||
var fields = new List<IDeviceImportField>(ColumnCount)
|
||||
{
|
||||
DeviceSerialNumberImportField.Create(d),
|
||||
DeviceDecommissionedReasonImportField.Create(d, decommissionReason, true, unassignUsers),
|
||||
};
|
||||
if (unassignUsers)
|
||||
{
|
||||
fields.Add(AssignedUserIdImportField.CreateUnassigned(d));
|
||||
}
|
||||
return new DeviceDecommissionImportRecord(d, i, fields);
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
|
||||
public static DeviceDecommissionImportContext Create(DiscoDataContext database, DeviceBatch deviceBatch, DecommissionReasons decommissionReason, bool unassignUsers)
|
||||
{
|
||||
var devices = database.Devices
|
||||
.Include(d => d.Jobs)
|
||||
.Include(d => d.AssignedUser)
|
||||
.Where(d => d.DeviceBatchId == deviceBatch.Id && d.DecommissionedDate == null)
|
||||
.ToList();
|
||||
return new DeviceDecommissionImportContext($"Batch: {deviceBatch.Name} ({deviceBatch.Id})", devices, decommissionReason, unassignUsers);
|
||||
}
|
||||
|
||||
public IDeviceImportColumn GetColumn(int Index)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public int? GetColumnByType(DeviceImportFieldTypes FieldType)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public IDeviceImportDataReader GetDataReader()
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public IEnumerable<KeyValuePair<DeviceImportFieldTypes, Type>> GetFieldHandlers()
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public void SetColumnType(int Index, DeviceImportFieldTypes Type)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
internal class DeviceDecommissionImportRecord : IDeviceImportRecord
|
||||
{
|
||||
public int Index { get; }
|
||||
public string DeviceSerialNumber { get; }
|
||||
public IEnumerable<IDeviceImportField> Fields { get; }
|
||||
public EntityState RecordAction { get; }
|
||||
public bool HasError { get; }
|
||||
|
||||
public DeviceDecommissionImportRecord(Device device, int index, IEnumerable<IDeviceImportField> fields)
|
||||
{
|
||||
Index = index;
|
||||
DeviceSerialNumber = device.SerialNumber;
|
||||
|
||||
if (fields.Any(f => !f.FieldAction.HasValue))
|
||||
RecordAction = EntityState.Detached;
|
||||
else if (fields.Any(f => f.FieldAction == EntityState.Modified))
|
||||
RecordAction = EntityState.Modified;
|
||||
else
|
||||
RecordAction = EntityState.Unchanged;
|
||||
|
||||
Fields = fields;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ namespace Disco.Services.Devices.Importing
|
||||
return context;
|
||||
}
|
||||
|
||||
public static IDeviceImportContext BeginDecommissionImport(DiscoDataContext database, DeviceBatch deviceBatch, DecommissionReasons decommissionReason, bool unassignUsers)
|
||||
=> DeviceDecommissionImportContext.Create(database, deviceBatch, decommissionReason, unassignUsers);
|
||||
|
||||
private static void GuessHeaderTypes(this IDeviceImportContext Context, DiscoDataContext Database)
|
||||
{
|
||||
using (var dataReader = Context.GetDataReader())
|
||||
@@ -151,11 +154,11 @@ namespace Disco.Services.Devices.Importing
|
||||
|
||||
int affectedRecords = 0;
|
||||
|
||||
foreach (var record in Context.Records.Cast<DeviceImportRecord>().Select((r, i) => Tuple.Create(r, i)))
|
||||
foreach (var (record, index) in Context.Records.Select((r, i) => Tuple.Create(r, i)))
|
||||
{
|
||||
Status.UpdateStatus(((double)record.Item2 / Context.Records.Count) * 100, string.Format("Applying: {0}", record.Item1.DeviceSerialNumber));
|
||||
Status.UpdateStatus(((double)index / Context.Records.Count) * 100, $"Applying: {record.DeviceSerialNumber}");
|
||||
|
||||
if (record.Item1.Apply(Database))
|
||||
if (DeviceImportRecord.Apply(record, Database))
|
||||
affectedRecords++;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,22 +30,22 @@ namespace Disco.Services.Devices.Importing
|
||||
this.RecordAction = RecordAction;
|
||||
}
|
||||
|
||||
public bool Apply(DiscoDataContext Database)
|
||||
public static bool Apply(IDeviceImportRecord record, DiscoDataContext Database)
|
||||
{
|
||||
if (RecordAction == EntityState.Detached || !HasError)
|
||||
if (record.RecordAction == EntityState.Detached || !record.HasError)
|
||||
{
|
||||
Device device;
|
||||
|
||||
if (RecordAction == EntityState.Unchanged)
|
||||
if (record.RecordAction == EntityState.Unchanged)
|
||||
{
|
||||
// Unchanged - No Action Required
|
||||
return false;
|
||||
}
|
||||
else if (RecordAction == EntityState.Modified)
|
||||
else if (record.RecordAction == EntityState.Modified)
|
||||
{
|
||||
device = Database.Devices.Find(DeviceSerialNumber);
|
||||
device = Database.Devices.Find(record.DeviceSerialNumber);
|
||||
}
|
||||
else if (RecordAction == EntityState.Added)
|
||||
else if (record.RecordAction == EntityState.Added)
|
||||
{
|
||||
// Use 'Add Device Offline' default if available
|
||||
var deviceProfileId = Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId;
|
||||
@@ -57,7 +57,7 @@ namespace Disco.Services.Devices.Importing
|
||||
// Create Device
|
||||
device = new Device()
|
||||
{
|
||||
SerialNumber = DeviceSerialNumber.ToUpper(),
|
||||
SerialNumber = record.DeviceSerialNumber.ToUpper(),
|
||||
CreatedDate = DateTime.Now,
|
||||
AllowUnauthenticatedEnrol = true,
|
||||
DeviceProfileId = deviceProfileId,
|
||||
@@ -71,9 +71,9 @@ namespace Disco.Services.Devices.Importing
|
||||
return false;
|
||||
}
|
||||
|
||||
bool changesMade = (RecordAction == EntityState.Added);
|
||||
bool changesMade = (record.RecordAction == EntityState.Added);
|
||||
|
||||
foreach (var field in Fields.Cast<DeviceImportFieldBase>())
|
||||
foreach (var field in record.Fields.Cast<DeviceImportFieldBase>())
|
||||
{
|
||||
changesMade = field.Apply(Database, device) || changesMade;
|
||||
}
|
||||
@@ -84,7 +84,7 @@ namespace Disco.Services.Devices.Importing
|
||||
|
||||
bool adDescriptionSet = false;
|
||||
|
||||
foreach (var field in Fields.Cast<DeviceImportFieldBase>())
|
||||
foreach (var field in record.Fields.Cast<DeviceImportFieldBase>())
|
||||
{
|
||||
field.Applied(Database, device, ref adDescriptionSet);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,25 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public static AssignedUserIdImportField CreateUnassigned(Device device)
|
||||
{
|
||||
var field = new AssignedUserIdImportField()
|
||||
{
|
||||
parsedValue = null,
|
||||
friendlyValue = null,
|
||||
};
|
||||
if (device.AssignedUser != null)
|
||||
{
|
||||
field.friendlyPreviousValue = $"{device.AssignedUser.DisplayName} [{device.AssignedUser.UserId}]";
|
||||
field.Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
{
|
||||
field.Success(EntityState.Unchanged);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
var value = friendlyValue = DataReader.GetString(ColumnIndex);
|
||||
|
||||
@@ -158,36 +158,48 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static bool CanDecommissionDevice(Device Device, IDeviceImportContext Context, IDeviceImportDataReader DataReader, out string ErrorMessage)
|
||||
public static bool CanDecommissionDevice(Device device, IDeviceImportContext context, IDeviceImportDataReader dataReader, out string errorMessage)
|
||||
{
|
||||
if (Device == null)
|
||||
var isAssigningUser = false;
|
||||
var assigningUserId = default(string);
|
||||
var assignedUserIndex = context.GetColumnByType(DeviceImportFieldTypes.AssignedUserId);
|
||||
if (assignedUserIndex.HasValue)
|
||||
{
|
||||
ErrorMessage = "Cannot decommission new devices";
|
||||
isAssigningUser = true;
|
||||
assigningUserId = dataReader.GetString(assignedUserIndex.Value);
|
||||
}
|
||||
var hasOpenJobs = device.Jobs.Any(j => !j.ClosedDate.HasValue);
|
||||
|
||||
return CanDecommissionDevice(device, isAssigningUser, assigningUserId, hasOpenJobs, out errorMessage);
|
||||
}
|
||||
|
||||
public static bool CanDecommissionDevice(Device device, bool isAssigningUser, string assigningUserId, bool hasOpenJobs, out string errorMessage)
|
||||
{
|
||||
if (device == null)
|
||||
{
|
||||
errorMessage = "Cannot decommission new devices";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check device is assigned (or being removed in this import)
|
||||
|
||||
var assignedUserIndex = Context.GetColumnByType(DeviceImportFieldTypes.AssignedUserId);
|
||||
if ((!assignedUserIndex.HasValue && Device.AssignedUserId != null) ||
|
||||
(assignedUserIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(assignedUserIndex.Value))))
|
||||
if (isAssigningUser && !string.IsNullOrEmpty(assigningUserId) ||
|
||||
(!isAssigningUser && device.AssignedUserId != null))
|
||||
{
|
||||
if (Device.AssignedUserId != null)
|
||||
ErrorMessage = $"The device is assigned to a user ({Device.AssignedUser.DisplayName} [{Device.AssignedUser.UserId}]) and cannot be decommissioned";
|
||||
if (!isAssigningUser)
|
||||
errorMessage = $"The device is assigned to a user ({device.AssignedUser.DisplayName} [{device.AssignedUser.UserId}]) and cannot be decommissioned";
|
||||
else
|
||||
ErrorMessage = $"The device is being assigned to a user ({DataReader.GetString(assignedUserIndex.Value)}) and cannot be decommissioned";
|
||||
errorMessage = $"The device is being assigned to a user ({assigningUserId}) and cannot be decommissioned";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check device doesn't have any open jobs
|
||||
var openJobCount = Device.Jobs.Count(j => !j.ClosedDate.HasValue);
|
||||
if (openJobCount > 0)
|
||||
if (hasOpenJobs)
|
||||
{
|
||||
ErrorMessage = $"The device is associated with {openJobCount} open job{(openJobCount == 1 ? null : "s")} and cannot be decommissioned";
|
||||
errorMessage = $"The device is associated with an open job and cannot be decommissioned";
|
||||
return false;
|
||||
}
|
||||
|
||||
ErrorMessage = null;
|
||||
errorMessage = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,26 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return parsedValue.HasValue ? parsedValue.Value.ToString() : rawValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue.HasValue ? previousValue.Value.ToString() : null; } }
|
||||
|
||||
public static DeviceDecommissionedReasonImportField Create(Device device, DecommissionReasons? decommissionReason, bool setDate, bool isUnassigningUser)
|
||||
{
|
||||
var field = new DeviceDecommissionedReasonImportField()
|
||||
{
|
||||
rawValue = decommissionReason?.ToString(),
|
||||
parsedValue = decommissionReason,
|
||||
previousValue = device.DecommissionReason,
|
||||
setDate = setDate
|
||||
};
|
||||
var hasOpenJobs = device.Jobs.Any(j => !j.ClosedDate.HasValue);
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(device, isUnassigningUser, null, hasOpenJobs, out var errorMessage))
|
||||
field.Error(errorMessage);
|
||||
else if (device.DecommissionReason == decommissionReason)
|
||||
field.Success(EntityState.Unchanged);
|
||||
else
|
||||
field.Success(EntityState.Modified);
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
var value = DataReader.GetString(ColumnIndex);
|
||||
@@ -50,8 +70,7 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
var decommissionedDateIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedDate);
|
||||
if (parsedValue.HasValue && !decommissionedDateIndex.HasValue)
|
||||
{
|
||||
string errorMessage;
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Context, DataReader, out errorMessage))
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Context, DataReader, out var errorMessage))
|
||||
return Error(errorMessage);
|
||||
|
||||
setDate = true;
|
||||
|
||||
@@ -19,6 +19,16 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return parsedValue; } }
|
||||
|
||||
public static DeviceSerialNumberImportField Create(Device device)
|
||||
{
|
||||
var field = new DeviceSerialNumberImportField()
|
||||
{
|
||||
parsedValue = device.SerialNumber,
|
||||
};
|
||||
field.Success(EntityState.Unchanged);
|
||||
return field;
|
||||
}
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
var value = DataReader.GetString(ColumnIndex);
|
||||
|
||||
@@ -366,6 +366,8 @@
|
||||
<Compile Include="Devices\DeviceFlags\DeviceFlagService.cs" />
|
||||
<Compile Include="Devices\DeviceFlags\DeviceFlagDeviceAssignedUsersManagedGroup.cs" />
|
||||
<Compile Include="Devices\DeviceFlags\DeviceFlagDevicesManagedGroup.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceDecommissionImportContext.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceDecommissionImportRecord.cs" />
|
||||
<Compile Include="Documents\DocumentExport.cs" />
|
||||
<Compile Include="Exporting\Exporter.cs" />
|
||||
<Compile Include="Exporting\ExportTask.cs" />
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Users;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
Reference in New Issue
Block a user