Update: Device Trusted Enrolment Import/Export

Added AllowUnauthenticatedEnrol to import and export functionality.
This commit is contained in:
Gary Sharp
2014-05-27 17:08:39 +10:00
parent 4e69253852
commit 7cad69598f
8 changed files with 140 additions and 4 deletions
@@ -34,6 +34,8 @@ namespace Disco.Models.Services.Devices.Exporting
public bool DeviceFirstEnrolledDate { get; set; }
[Display(ShortName = "Device", Name = "Last Enrolled Date", Description = "The date the device was last enrolled in Disco")]
public bool DeviceLastEnrolledDate { get; set; }
[Display(ShortName = "Device", Name = "Enrolment Trusted", Description = "The device is trusted to complete an unauthenticated enrolment")]
public bool DeviceAllowUnauthenticatedEnrol { get; set; }
[Display(ShortName = "Device", Name = "Decommissioned Date", Description = "The date the device was decommissioned in Disco")]
public bool DeviceDecommissionedDate { get; set; }
[Display(ShortName = "Device", Name = "Decommissioned Reason", Description = "The reason the device was decommissioned")]
@@ -15,6 +15,8 @@ namespace Disco.Models.Services.Devices.Importing
DeviceAssetNumber,
[Display(Name = "Device Location", Description = "The device location")]
DeviceLocation,
[Display(Name = "Device Enrolment Trusted", Description = "The device is trusted to complete an unauthenticated enrolment")]
DeviceAllowUnauthenticatedEnrol,
[Display(Name = "Device Decommissioned Date", Description = "The date the device was decommissioned in Disco")]
DeviceDecommissionedDate,
[Display(Name = "Device Decommissioned Reason", Description = "The reason the device was decommissioned")]
@@ -203,6 +203,7 @@ namespace Disco.Services.Devices.Exporting
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceCreatedDate", r => r.Device.CreatedDate.ToString(DateTimeFormat), false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceFirstEnrolledDate", r => r.Device.EnrolledDate.HasValue ? r.Device.EnrolledDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceLastEnrolledDate", r => r.Device.LastEnrolDate.HasValue ? r.Device.LastEnrolDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceAllowUnauthenticatedEnrol", r => r.Device.AllowUnauthenticatedEnrol.ToString(), false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceDecommissionedDate", r => r.Device.DecommissionedDate.HasValue ? r.Device.DecommissionedDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceDecommissionedReason", r => r.Device.DecommissionReason.HasValue ? r.Device.DecommissionReason.Value.ToString() : null, true);
@@ -24,6 +24,7 @@ namespace Disco.Services.Devices.Importing
{ DeviceImportFieldTypes.DeviceSerialNumber, typeof(DeviceSerialNumberImportField) },
{ DeviceImportFieldTypes.DeviceAssetNumber, typeof(DeviceAssetNumberImportField) },
{ DeviceImportFieldTypes.DeviceLocation, typeof(DeviceLocationImportField) },
{ DeviceImportFieldTypes.DeviceAllowUnauthenticatedEnrol, typeof(DeviceAllowUnauthenticatedEnrolImportField) },
{ DeviceImportFieldTypes.DeviceDecommissionedDate, typeof(DeviceDecommissionedDateImportField) },
{ DeviceImportFieldTypes.DeviceDecommissionedReason, typeof(DeviceDecommissionedReasonImportField) },
@@ -0,0 +1,129 @@
using Disco.Data.Repository;
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Users;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace Disco.Services.Devices.Importing.Fields
{
internal class DeviceAllowUnauthenticatedEnrolImportField : DeviceImportFieldBase
{
private static string[] TrueValues = { "true", "1", "yes", "-1", "on" };
private static string[] FalseValues = { "false", "0", "no", "off" };
private bool parsedValue;
private string friendlyValue;
private string friendlyPreviousValue;
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DeviceAllowUnauthenticatedEnrol; } }
public override object RawParsedValue { get { return parsedValue; } }
public override string FriendlyValue { get { return friendlyValue; } }
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
{
friendlyValue = Value;
if (!ParseBoolean(Value, out parsedValue))
return Error("Expected a Boolean expression (True, 1, Yes, On, False, 0, No, Off)");
friendlyValue = parsedValue.ToString();
if (parsedValue == true)
{
// Check Decommissioned
bool? importDecommissioning = null;
if (Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedDate) || Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedReason))
importDecommissioning = Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedDate) && !string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedDate]) ||
Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedReason) && !string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedReason]);
if (importDecommissioning.HasValue && importDecommissioning.Value)
return Error("Cannot enrol a device being decommissioned");
if (ExistingDevice != null && ExistingDevice.DecommissionedDate.HasValue && !importDecommissioning.HasValue)
{
return Error("Cannot enrol a decommissioned device");
}
}
if (ExistingDevice == null && parsedValue != false) // Default: True
{
return Success(EntityState.Added);
}
else if (ExistingDevice != null && ExistingDevice.AllowUnauthenticatedEnrol != parsedValue)
{
friendlyPreviousValue = ExistingDevice.AllowUnauthenticatedEnrol.ToString();
return Success(EntityState.Modified);
}
else
return Success(EntityState.Unchanged);
}
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Modified ||
this.FieldAction == EntityState.Added)
{
Device.AllowUnauthenticatedEnrol = parsedValue;
return true;
}
else
{
return false;
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
{
// column name
var possibleColumns = Context.Header
.Select((h, i) => Tuple.Create(h, i))
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
h.Item1.Item1.IndexOf("trust enrol", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Item1.Item1.IndexOf("enrolment trusted", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Item1.Item1.IndexOf("trust", System.StringComparison.OrdinalIgnoreCase) >= 0
);
// All Boolean
possibleColumns = possibleColumns.Where(h =>
{
bool lastValue;
return Context.RawData.Select(v => v[h.Item2]).Take(100).Where(v => !string.IsNullOrWhiteSpace(v)).All(v => ParseBoolean(v, out lastValue));
}).ToList();
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
}
private static bool ParseBoolean(string value, out bool result)
{
if (string.IsNullOrWhiteSpace(value))
{
result = false;
return false;
}
value = value.Trim();
if (TrueValues.Contains(value, StringComparer.OrdinalIgnoreCase))
{
result = true;
return true;
}
else if (FalseValues.Contains(value, StringComparer.OrdinalIgnoreCase))
{
result = false;
return true;
}
else
{
result = false;
return false;
}
}
}
}
+1
View File
@@ -188,6 +188,7 @@
<Compile Include="Devices\Importing\DeviceImportRecord.cs" />
<Compile Include="Devices\Importing\Fields\BatchIdImportField.cs" />
<Compile Include="Devices\Importing\Fields\AssignedUserIdImportField.cs" />
<Compile Include="Devices\Importing\Fields\DeviceAllowUnauthenticatedEnrolImportField.cs" />
<Compile Include="Devices\Importing\Fields\DetailBatteryImportField.cs" />
<Compile Include="Devices\Importing\Fields\DeviceDecommissionedReasonImportField.cs" />
<Compile Include="Devices\Importing\Fields\DeviceDecommissionedDateImportField.cs" />
+2 -2
View File
@@ -13,7 +13,7 @@
<div id="Devices_Export">
@using (Html.BeginForm(MVC.API.Device.Export()))
{
<div id="Devices_Export_Type" class="form" style="width: 530px">
<div id="Devices_Export_Type" class="form" style="width: 570px">
<h2>Export Type</h2>
<table>
<tr>
@@ -40,7 +40,7 @@
</tr>
</table>
</div>
<div id="Devices_Export_Fields" class="form" style="width: 530px; margin-top: 15px;">
<div id="Devices_Export_Fields" class="form" style="width: 570px; margin-top: 15px;">
<h2>Export Fields <a id="Devices_Export_Fields_Defaults" href="#">(Defaults)</a></h2>
<table>
@foreach (var optionGroup in optionGroups)
+2 -2
View File
@@ -96,7 +96,7 @@ WriteLiteral(" id=\"Devices_Export_Type\"");
WriteLiteral(" class=\"form\"");
WriteLiteral(" style=\"width: 530px\"");
WriteLiteral(" style=\"width: 570px\"");
WriteLiteral(">\r\n <h2>Export Type</h2>\r\n <table>\r\n <tr>\r\n " +
" <th");
@@ -191,7 +191,7 @@ WriteLiteral(" id=\"Devices_Export_Fields\"");
WriteLiteral(" class=\"form\"");
WriteLiteral(" style=\"width: 530px; margin-top: 15px;\"");
WriteLiteral(" style=\"width: 570px; margin-top: 15px;\"");
WriteLiteral(">\r\n <h2>Export Fields <a");