Permissions & Authorization for Users #24
Initial Release; Includes Database and MVC refactoring
This commit is contained in:
@@ -4,6 +4,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
@@ -13,39 +15,60 @@ namespace Disco.BI.Extensions
|
||||
#region Delete
|
||||
public static bool CanDelete(this DeviceAttachment da)
|
||||
{
|
||||
return true; // Placeholder - Currently Can Always Delete;
|
||||
if (UserService.CurrentAuthorization.Has(Claims.Device.Actions.RemoveAnyAttachments))
|
||||
return true;
|
||||
|
||||
if (UserService.CurrentAuthorization.Has(Claims.Device.Actions.RemoveOwnAttachments)
|
||||
&& da.TechUserId == UserService.CurrentUserId)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
public static void OnDelete(this DeviceAttachment da, DiscoDataContext dbContext)
|
||||
public static void OnDelete(this DeviceAttachment da, DiscoDataContext Database)
|
||||
{
|
||||
if (!da.CanDelete())
|
||||
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
||||
|
||||
da.RepositoryDelete(dbContext);
|
||||
dbContext.DeviceAttachments.Remove(da);
|
||||
da.RepositoryDelete(Database);
|
||||
Database.DeviceAttachments.Remove(da);
|
||||
}
|
||||
public static bool CanDelete(this JobAttachment ja)
|
||||
{
|
||||
return true; // Placeholder - Currently Can Always Delete;
|
||||
if (UserService.CurrentAuthorization.Has(Claims.Job.Actions.RemoveAnyAttachments))
|
||||
return true;
|
||||
|
||||
if (UserService.CurrentAuthorization.Has(Claims.Job.Actions.RemoveOwnAttachments)
|
||||
&& ja.TechUserId == UserService.CurrentUserId)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
public static void OnDelete(this JobAttachment ja, DiscoDataContext dbContext)
|
||||
public static void OnDelete(this JobAttachment ja, DiscoDataContext Database)
|
||||
{
|
||||
if (!ja.CanDelete())
|
||||
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
||||
|
||||
ja.RepositoryDelete(dbContext);
|
||||
dbContext.JobAttachments.Remove(ja);
|
||||
ja.RepositoryDelete(Database);
|
||||
Database.JobAttachments.Remove(ja);
|
||||
}
|
||||
public static bool CanDelete(this UserAttachment ua)
|
||||
{
|
||||
return true; // Placeholder - Currently Can Always Delete;
|
||||
if (UserService.CurrentAuthorization.Has(Claims.User.Actions.RemoveAnyAttachments))
|
||||
return true;
|
||||
|
||||
if (UserService.CurrentAuthorization.Has(Claims.User.Actions.RemoveOwnAttachments)
|
||||
&& ua.TechUserId == UserService.CurrentUserId)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
public static void OnDelete(this UserAttachment ua, DiscoDataContext dbContext)
|
||||
public static void OnDelete(this UserAttachment ua, DiscoDataContext Database)
|
||||
{
|
||||
if (!ua.CanDelete())
|
||||
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
||||
|
||||
ua.RepositoryDelete(dbContext);
|
||||
dbContext.UserAttachments.Remove(ua);
|
||||
ua.RepositoryDelete(Database);
|
||||
Database.UserAttachments.Remove(ua);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -6,16 +6,17 @@ using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using System.IO;
|
||||
using Disco.BI.DocumentTemplateBI;
|
||||
using Disco.Services.Users;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class AttachmentExtensions
|
||||
{
|
||||
|
||||
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier UniqueIdentifier, DiscoDataContext dbContext, System.IO.Stream PdfContent, byte[] PdfThumbnail)
|
||||
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier UniqueIdentifier, DiscoDataContext Database, System.IO.Stream PdfContent, byte[] PdfThumbnail)
|
||||
{
|
||||
|
||||
UniqueIdentifier.LoadComponents(dbContext);
|
||||
UniqueIdentifier.LoadComponents(Database);
|
||||
DocumentTemplate documentTemplate = UniqueIdentifier.DocumentTemplate;
|
||||
string filename;
|
||||
string comments;
|
||||
@@ -31,25 +32,25 @@ namespace Disco.BI.Extensions
|
||||
comments = string.Format("Generated: {0:s}", UniqueIdentifier.TimeStamp);
|
||||
}
|
||||
|
||||
User creatorUser = UserBI.UserCache.GetUser(UniqueIdentifier.CreatorId, dbContext);
|
||||
User creatorUser = UserService.GetUser(UniqueIdentifier.CreatorId, Database);
|
||||
if (creatorUser == null)
|
||||
{
|
||||
// No Creator User (or Username invalid)
|
||||
creatorUser = UserBI.UserCache.CurrentUser;
|
||||
creatorUser = UserService.CurrentUser;
|
||||
}
|
||||
switch (UniqueIdentifier.DataScope)
|
||||
{
|
||||
case DocumentTemplate.DocumentTemplateScopes.Device:
|
||||
Device d = (Device)UniqueIdentifier.Data;
|
||||
d.CreateAttachment(dbContext, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, documentTemplate, PdfThumbnail);
|
||||
d.CreateAttachment(Database, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, documentTemplate, PdfThumbnail);
|
||||
return true;
|
||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||
Job j = (Job)UniqueIdentifier.Data;
|
||||
j.CreateAttachment(dbContext, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, documentTemplate, PdfThumbnail);
|
||||
j.CreateAttachment(Database, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, documentTemplate, PdfThumbnail);
|
||||
return true;
|
||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||
User u = (User)UniqueIdentifier.Data;
|
||||
u.CreateAttachment(dbContext, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, documentTemplate, PdfThumbnail);
|
||||
u.CreateAttachment(Database, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, documentTemplate, PdfThumbnail);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
@@ -57,47 +58,47 @@ namespace Disco.BI.Extensions
|
||||
|
||||
}
|
||||
|
||||
public static string RepositoryFilename(this DeviceAttachment da, DiscoDataContext dbContext)
|
||||
public static string RepositoryFilename(this DeviceAttachment da, DiscoDataContext Database)
|
||||
{
|
||||
return Path.Combine(DataStore.CreateLocation(dbContext, "DeviceAttachments", da.Timestamp), string.Format("{0}_{1}_file", da.DeviceSerialNumber, da.Id));
|
||||
return Path.Combine(DataStore.CreateLocation(Database, "DeviceAttachments", da.Timestamp), string.Format("{0}_{1}_file", da.DeviceSerialNumber, da.Id));
|
||||
}
|
||||
public static string RepositoryFilename(this JobAttachment ja, DiscoDataContext dbContext)
|
||||
public static string RepositoryFilename(this JobAttachment ja, DiscoDataContext Database)
|
||||
{
|
||||
return Path.Combine(DataStore.CreateLocation(dbContext, "JobAttachments", ja.Timestamp), string.Format("{0}_{1}_file", ja.JobId, ja.Id));
|
||||
return Path.Combine(DataStore.CreateLocation(Database, "JobAttachments", ja.Timestamp), string.Format("{0}_{1}_file", ja.JobId, ja.Id));
|
||||
}
|
||||
public static string RepositoryFilename(this UserAttachment ua, DiscoDataContext dbContext)
|
||||
public static string RepositoryFilename(this UserAttachment ua, DiscoDataContext Database)
|
||||
{
|
||||
return Path.Combine(DataStore.CreateLocation(dbContext, "UserAttachments", ua.Timestamp), string.Format("{0}_{1}_file", ua.UserId, ua.Id));
|
||||
return Path.Combine(DataStore.CreateLocation(Database, "UserAttachments", ua.Timestamp), string.Format("{0}_{1}_file", ua.UserId, ua.Id));
|
||||
}
|
||||
|
||||
private static string RepositoryThumbnailFilenameInternal(string DirectoryPath, string Filename)
|
||||
{
|
||||
return Path.Combine(DirectoryPath, Filename);
|
||||
}
|
||||
public static string RepositoryThumbnailFilename(this DeviceAttachment da, DiscoDataContext dbContext)
|
||||
public static string RepositoryThumbnailFilename(this DeviceAttachment da, DiscoDataContext Database)
|
||||
{
|
||||
return RepositoryThumbnailFilenameInternal(DataStore.CreateLocation(dbContext, "DeviceAttachments", da.Timestamp), string.Format("{0}_{1}_thumb.jpg", da.DeviceSerialNumber, da.Id));
|
||||
return RepositoryThumbnailFilenameInternal(DataStore.CreateLocation(Database, "DeviceAttachments", da.Timestamp), string.Format("{0}_{1}_thumb.jpg", da.DeviceSerialNumber, da.Id));
|
||||
}
|
||||
public static string RepositoryThumbnailFilename(this JobAttachment ja, DiscoDataContext dbContext)
|
||||
public static string RepositoryThumbnailFilename(this JobAttachment ja, DiscoDataContext Database)
|
||||
{
|
||||
return RepositoryThumbnailFilenameInternal(DataStore.CreateLocation(dbContext, "JobAttachments", ja.Timestamp), string.Format("{0}_{1}_thumb.jpg", ja.JobId, ja.Id));
|
||||
return RepositoryThumbnailFilenameInternal(DataStore.CreateLocation(Database, "JobAttachments", ja.Timestamp), string.Format("{0}_{1}_thumb.jpg", ja.JobId, ja.Id));
|
||||
}
|
||||
public static string RepositoryThumbnailFilename(this UserAttachment ua, DiscoDataContext dbContext)
|
||||
public static string RepositoryThumbnailFilename(this UserAttachment ua, DiscoDataContext Database)
|
||||
{
|
||||
return RepositoryThumbnailFilenameInternal(DataStore.CreateLocation(dbContext, "UserAttachments", ua.Timestamp), string.Format("{0}_{1}_thumb.jpg", ua.UserId, ua.Id));
|
||||
return RepositoryThumbnailFilenameInternal(DataStore.CreateLocation(Database, "UserAttachments", ua.Timestamp), string.Format("{0}_{1}_thumb.jpg", ua.UserId, ua.Id));
|
||||
}
|
||||
|
||||
public static void RepositoryDelete(this DeviceAttachment da, DiscoDataContext dbContext)
|
||||
public static void RepositoryDelete(this DeviceAttachment da, DiscoDataContext Database)
|
||||
{
|
||||
RepositoryDelete(da.RepositoryFilename(dbContext), da.RepositoryThumbnailFilename(dbContext));
|
||||
RepositoryDelete(da.RepositoryFilename(Database), da.RepositoryThumbnailFilename(Database));
|
||||
}
|
||||
public static void RepositoryDelete(this JobAttachment ja, DiscoDataContext dbContext)
|
||||
public static void RepositoryDelete(this JobAttachment ja, DiscoDataContext Database)
|
||||
{
|
||||
RepositoryDelete(ja.RepositoryFilename(dbContext), ja.RepositoryThumbnailFilename(dbContext));
|
||||
RepositoryDelete(ja.RepositoryFilename(Database), ja.RepositoryThumbnailFilename(Database));
|
||||
}
|
||||
public static void RepositoryDelete(this UserAttachment ua, DiscoDataContext dbContext)
|
||||
public static void RepositoryDelete(this UserAttachment ua, DiscoDataContext Database)
|
||||
{
|
||||
RepositoryDelete(ua.RepositoryFilename(dbContext), ua.RepositoryThumbnailFilename(dbContext));
|
||||
RepositoryDelete(ua.RepositoryFilename(Database), ua.RepositoryThumbnailFilename(Database));
|
||||
}
|
||||
private static void RepositoryDelete(params string[] filePaths)
|
||||
{
|
||||
@@ -108,39 +109,39 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
public static string SaveAttachment(this DeviceAttachment da, DiscoDataContext dbContext, Stream FileContent)
|
||||
public static string SaveAttachment(this DeviceAttachment da, DiscoDataContext Database, Stream FileContent)
|
||||
{
|
||||
string filePath = da.RepositoryFilename(dbContext);
|
||||
string filePath = da.RepositoryFilename(Database);
|
||||
SaveAttachment(filePath, FileContent);
|
||||
return filePath;
|
||||
}
|
||||
public static string SaveAttachment(this JobAttachment ja, DiscoDataContext dbContext, Stream FileContent)
|
||||
public static string SaveAttachment(this JobAttachment ja, DiscoDataContext Database, Stream FileContent)
|
||||
{
|
||||
string filePath = ja.RepositoryFilename(dbContext);
|
||||
string filePath = ja.RepositoryFilename(Database);
|
||||
SaveAttachment(filePath, FileContent);
|
||||
return filePath;
|
||||
}
|
||||
public static string SaveAttachment(this UserAttachment ua, DiscoDataContext dbContext, Stream FileContent)
|
||||
public static string SaveAttachment(this UserAttachment ua, DiscoDataContext Database, Stream FileContent)
|
||||
{
|
||||
string filePath = ua.RepositoryFilename(dbContext);
|
||||
string filePath = ua.RepositoryFilename(Database);
|
||||
SaveAttachment(filePath, FileContent);
|
||||
return filePath;
|
||||
}
|
||||
public static string SaveThumbnailAttachment(this DeviceAttachment da, DiscoDataContext dbContext, byte[] FileContent)
|
||||
public static string SaveThumbnailAttachment(this DeviceAttachment da, DiscoDataContext Database, byte[] FileContent)
|
||||
{
|
||||
string filePath = da.RepositoryThumbnailFilename(dbContext);
|
||||
string filePath = da.RepositoryThumbnailFilename(Database);
|
||||
File.WriteAllBytes(filePath, FileContent);
|
||||
return filePath;
|
||||
}
|
||||
public static string SaveThumbnailAttachment(this JobAttachment ja, DiscoDataContext dbContext, byte[] FileContent)
|
||||
public static string SaveThumbnailAttachment(this JobAttachment ja, DiscoDataContext Database, byte[] FileContent)
|
||||
{
|
||||
string filePath = ja.RepositoryThumbnailFilename(dbContext);
|
||||
string filePath = ja.RepositoryThumbnailFilename(Database);
|
||||
File.WriteAllBytes(filePath, FileContent);
|
||||
return filePath;
|
||||
}
|
||||
public static string SaveThumbnailAttachment(this UserAttachment ua, DiscoDataContext dbContext, byte[] FileContent)
|
||||
public static string SaveThumbnailAttachment(this UserAttachment ua, DiscoDataContext Database, byte[] FileContent)
|
||||
{
|
||||
string filePath = ua.RepositoryThumbnailFilename(dbContext);
|
||||
string filePath = ua.RepositoryThumbnailFilename(Database);
|
||||
File.WriteAllBytes(filePath, FileContent);
|
||||
return filePath;
|
||||
}
|
||||
@@ -154,39 +155,39 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
public static string GenerateThumbnail(this DeviceAttachment da, DiscoDataContext dbContext)
|
||||
public static string GenerateThumbnail(this DeviceAttachment da, DiscoDataContext Database)
|
||||
{
|
||||
string filePath = da.RepositoryThumbnailFilename(dbContext);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(da.RepositoryFilename(dbContext), da.MimeType, filePath);
|
||||
string filePath = da.RepositoryThumbnailFilename(Database);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(da.RepositoryFilename(Database), da.MimeType, filePath);
|
||||
return filePath;
|
||||
}
|
||||
public static string GenerateThumbnail(this JobAttachment ja, DiscoDataContext dbContext)
|
||||
public static string GenerateThumbnail(this JobAttachment ja, DiscoDataContext Database)
|
||||
{
|
||||
string filePath = ja.RepositoryThumbnailFilename(dbContext);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(ja.RepositoryFilename(dbContext), ja.MimeType, filePath);
|
||||
string filePath = ja.RepositoryThumbnailFilename(Database);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(ja.RepositoryFilename(Database), ja.MimeType, filePath);
|
||||
return filePath;
|
||||
}
|
||||
public static string GenerateThumbnail(this UserAttachment ua, DiscoDataContext dbContext)
|
||||
public static string GenerateThumbnail(this UserAttachment ua, DiscoDataContext Database)
|
||||
{
|
||||
string filePath = ua.RepositoryThumbnailFilename(dbContext);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(ua.RepositoryFilename(dbContext), ua.MimeType, filePath);
|
||||
string filePath = ua.RepositoryThumbnailFilename(Database);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(ua.RepositoryFilename(Database), ua.MimeType, filePath);
|
||||
return filePath;
|
||||
}
|
||||
public static string GenerateThumbnail(this DeviceAttachment da, DiscoDataContext dbContext, Stream SourceFile)
|
||||
public static string GenerateThumbnail(this DeviceAttachment da, DiscoDataContext Database, Stream SourceFile)
|
||||
{
|
||||
string filePath = da.RepositoryThumbnailFilename(dbContext);
|
||||
string filePath = da.RepositoryThumbnailFilename(Database);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(SourceFile, da.MimeType, filePath);
|
||||
return filePath;
|
||||
}
|
||||
public static string GenerateThumbnail(this JobAttachment ja, DiscoDataContext dbContext, Stream SourceFile)
|
||||
public static string GenerateThumbnail(this JobAttachment ja, DiscoDataContext Database, Stream SourceFile)
|
||||
{
|
||||
string filePath = ja.RepositoryThumbnailFilename(dbContext);
|
||||
string filePath = ja.RepositoryThumbnailFilename(Database);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(SourceFile, ja.MimeType, filePath);
|
||||
return filePath;
|
||||
}
|
||||
public static string GenerateThumbnail(this UserAttachment ua, DiscoDataContext dbContext, Stream SourceFile)
|
||||
public static string GenerateThumbnail(this UserAttachment ua, DiscoDataContext Database, Stream SourceFile)
|
||||
{
|
||||
string filePath = ua.RepositoryThumbnailFilename(dbContext);
|
||||
string filePath = ua.RepositoryThumbnailFilename(Database);
|
||||
AttachmentBI.Utilities.GenerateThumbnail(SourceFile, ua.MimeType, filePath);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Authorization;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Users;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class AuthorizationRoleExtensions
|
||||
{
|
||||
|
||||
public static void Delete(this IRoleToken roleToken, DiscoDataContext Database)
|
||||
{
|
||||
var role = Database.AuthorizationRoles.Find(roleToken.Role.Id);
|
||||
UserService.DeleteAuthorizationRole(Database, roleToken.Role);
|
||||
}
|
||||
|
||||
public static void Delete(this AuthorizationRole role, DiscoDataContext Database)
|
||||
{
|
||||
UserService.DeleteAuthorizationRole(Database, role);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Disco.Models.ClientServices;
|
||||
using System.Web;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Users;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
@@ -20,10 +21,10 @@ namespace Disco.BI.Extensions
|
||||
if (HttpContext.Current.Request.IsAuthenticated)
|
||||
username = HttpContext.Current.User.Identity.Name;
|
||||
|
||||
using (DiscoDataContext dbContext = new DiscoDataContext())
|
||||
using (DiscoDataContext database = new DiscoDataContext())
|
||||
{
|
||||
EnrolResponse response = DeviceBI.DeviceEnrol.Enrol(dbContext, username, request);
|
||||
dbContext.SaveChanges();
|
||||
EnrolResponse response = DeviceBI.DeviceEnrol.Enrol(database, username, request);
|
||||
database.SaveChanges();
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -40,14 +41,14 @@ namespace Disco.BI.Extensions
|
||||
if (username == null)
|
||||
throw new InvalidOperationException("Unauthenticated Http Context");
|
||||
|
||||
using (DiscoDataContext dbContext = new DiscoDataContext())
|
||||
using (DiscoDataContext database = new DiscoDataContext())
|
||||
{
|
||||
User user = UserBI.UserCache.GetUser(username, dbContext, true);
|
||||
User user = UserService.GetUser(username, database, true);
|
||||
WhoAmIResponse response = new WhoAmIResponse()
|
||||
{
|
||||
Username = user.Id,
|
||||
DisplayName = user.DisplayName,
|
||||
Type = user.Type
|
||||
Type = "TODO!"
|
||||
};
|
||||
return response;
|
||||
}
|
||||
@@ -58,10 +59,10 @@ namespace Disco.BI.Extensions
|
||||
if (HttpContext.Current == null)
|
||||
throw new PlatformNotSupportedException("This function can only be accessed from within ASP.NET");
|
||||
|
||||
using (DiscoDataContext dbContext = new DiscoDataContext())
|
||||
using (DiscoDataContext database = new DiscoDataContext())
|
||||
{
|
||||
MacEnrolResponse response = DeviceBI.DeviceEnrol.MacEnrol(dbContext, request, false);
|
||||
dbContext.SaveChanges();
|
||||
MacEnrolResponse response = DeviceBI.DeviceEnrol.MacEnrol(database, request, false);
|
||||
database.SaveChanges();
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ using System.Text;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.BI.Interop.ActiveDirectory;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
@@ -17,36 +19,57 @@ namespace Disco.BI.Extensions
|
||||
|
||||
public static bool CanCreateJob(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.Create))
|
||||
return false;
|
||||
|
||||
return !d.IsDecommissioned();
|
||||
}
|
||||
|
||||
public static bool CanUpdateAssignment(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.AssignUser))
|
||||
return false;
|
||||
|
||||
return !d.IsDecommissioned();
|
||||
}
|
||||
|
||||
public static bool CanUpdateDeviceProfile(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Properties.DeviceProfile))
|
||||
return false;
|
||||
|
||||
return !d.IsDecommissioned();
|
||||
}
|
||||
|
||||
public static bool CanUpdateDeviceBatch(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Properties.DeviceBatch))
|
||||
return false;
|
||||
|
||||
return !d.IsDecommissioned();
|
||||
}
|
||||
|
||||
public static bool CanUpdateTrustEnrol(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.AllowUnauthenticatedEnrol))
|
||||
return false;
|
||||
|
||||
return !d.IsDecommissioned() && !d.AllowUnauthenticatedEnrol;
|
||||
}
|
||||
public static bool CanUpdateUntrustEnrol(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.AllowUnauthenticatedEnrol))
|
||||
return false;
|
||||
|
||||
return !d.IsDecommissioned() && d.AllowUnauthenticatedEnrol;
|
||||
}
|
||||
|
||||
#region Decommission
|
||||
public static bool CanDecommission(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.Decommission))
|
||||
return false;
|
||||
|
||||
if (d.DecommissionedDate.HasValue)
|
||||
return false; // Already Decommissioned
|
||||
|
||||
@@ -80,6 +103,9 @@ namespace Disco.BI.Extensions
|
||||
#region Recommission
|
||||
public static bool CanRecommission(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.Recommission))
|
||||
return false;
|
||||
|
||||
return d.DecommissionedDate.HasValue;
|
||||
}
|
||||
public static void OnRecommission(this Device d)
|
||||
@@ -105,17 +131,20 @@ namespace Disco.BI.Extensions
|
||||
#region Delete
|
||||
public static bool CanDelete(this Device d)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Device.Actions.Delete))
|
||||
return false;
|
||||
|
||||
return d.DecommissionedDate.HasValue;
|
||||
}
|
||||
public static void OnDelete(this Device d, DiscoDataContext dbContext)
|
||||
public static void OnDelete(this Device d, DiscoDataContext Database)
|
||||
{
|
||||
// Delete Jobs
|
||||
foreach (Job j in dbContext.Jobs.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
foreach (Job j in Database.Jobs.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
{
|
||||
if (j.UserId == null)
|
||||
{ // No User associated, thus must Delete whole Job
|
||||
if (j.CanDelete())
|
||||
j.OnDelete(dbContext);
|
||||
j.OnDelete(Database);
|
||||
else
|
||||
throw new InvalidOperationException(string.Format("Deletion of Device is Denied (See Job# {0})", j.Id));
|
||||
}
|
||||
@@ -128,35 +157,35 @@ namespace Disco.BI.Extensions
|
||||
JobLog jobLog = new JobLog()
|
||||
{
|
||||
JobId = j.Id,
|
||||
TechUserId = UserBI.UserCache.CurrentUser.Id,
|
||||
TechUserId = UserService.CurrentUser.Id,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = string.Format("Device Deleted{0}{0}Serial Number: {1}{0}Computer Name: {2}{0}Model: {3}{0}Profile: {4}",
|
||||
Environment.NewLine, d.SerialNumber, d.ComputerName, d.DeviceModel, d.DeviceProfile)
|
||||
};
|
||||
dbContext.JobLogs.Add(jobLog);
|
||||
Database.JobLogs.Add(jobLog);
|
||||
}
|
||||
}
|
||||
|
||||
// Disable Wireless Certificates
|
||||
foreach (var wc in dbContext.DeviceCertificates.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
foreach (var wc in Database.DeviceCertificates.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
{
|
||||
wc.DeviceSerialNumber = null;
|
||||
wc.Enabled = false;
|
||||
}
|
||||
// Delete Device Details
|
||||
foreach (var dd in dbContext.DeviceDetails.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
dbContext.DeviceDetails.Remove(dd);
|
||||
foreach (var dd in Database.DeviceDetails.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
Database.DeviceDetails.Remove(dd);
|
||||
// Delete Device Attachments
|
||||
foreach (var da in dbContext.DeviceAttachments.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
foreach (var da in Database.DeviceAttachments.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
{
|
||||
da.RepositoryDelete(dbContext);
|
||||
dbContext.DeviceAttachments.Remove(da);
|
||||
da.RepositoryDelete(Database);
|
||||
Database.DeviceAttachments.Remove(da);
|
||||
}
|
||||
// Delete Device User Assignments
|
||||
foreach (var dua in dbContext.DeviceUserAssignments.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
dbContext.DeviceUserAssignments.Remove(dua);
|
||||
foreach (var dua in Database.DeviceUserAssignments.Where(i => i.DeviceSerialNumber == d.SerialNumber))
|
||||
Database.DeviceUserAssignments.Remove(dua);
|
||||
|
||||
dbContext.Devices.Remove(d);
|
||||
Database.Devices.Remove(d);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -4,28 +4,33 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class DeviceBatchExtensions
|
||||
{
|
||||
public static bool CanDelete(this DeviceBatch db, DiscoDataContext dbContext)
|
||||
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 = dbContext.Devices.Count(d => d.DeviceBatchId == db.Id);
|
||||
var deviceCount = Database.Devices.Count(d => d.DeviceBatchId == db.Id);
|
||||
if (deviceCount > 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void Delete(this DeviceBatch db, DiscoDataContext dbContext)
|
||||
public static void Delete(this DeviceBatch db, DiscoDataContext Database)
|
||||
{
|
||||
if (!db.CanDelete(dbContext))
|
||||
if (!db.CanDelete(Database))
|
||||
throw new InvalidOperationException("The state of this Device Batch doesn't allow it to be deleted");
|
||||
|
||||
// Delete Batch
|
||||
dbContext.DeviceBatches.Remove(db);
|
||||
Database.DeviceBatches.Remove(db);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,10 @@ namespace Disco.BI.Extensions
|
||||
public static class DeviceCertificateExtensions
|
||||
{
|
||||
|
||||
public static Tuple<DeviceCertificate, List<string>> AllocateCertificate(this Device device, DiscoDataContext dbContext)
|
||||
public static Tuple<DeviceCertificate, List<string>> AllocateCertificate(this Device device, DiscoDataContext Database)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(device.DeviceProfile.CertificateProviderId))
|
||||
{
|
||||
// REMOVED 2012-07-18 G# - Plugin is responsible for checking
|
||||
//var deviceCertificates = dbContext.DeviceCertificates.Where(c =>
|
||||
// c.DeviceSerialNumber == device.SerialNumber &&
|
||||
// c.ProviderId == device.DeviceProfile.CertificateProviderId &&
|
||||
// c.Enabled == true).ToList();
|
||||
|
||||
// Load Plugin
|
||||
PluginFeatureManifest featureManifest = Plugins.GetPluginFeature(device.DeviceProfile.CertificateProviderId, typeof(CertificateProviderFeature));
|
||||
|
||||
@@ -32,7 +26,7 @@ namespace Disco.BI.Extensions
|
||||
// return new Tuple<DeviceCertificate, List<string>>(deviceCertificates[0], providerPlugin.RemoveExistingCertificateNames());
|
||||
//else
|
||||
|
||||
return providerFeature.AllocateCertificate(dbContext, device);
|
||||
return providerFeature.AllocateCertificate(Database, device);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,13 +8,15 @@ using System.Collections.Generic;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Disco.Models.Interop.ActiveDirectory;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class DeviceExtensions
|
||||
{
|
||||
|
||||
public static string ComputerNameRender(this Device device, DiscoDataContext context)
|
||||
public static string ComputerNameRender(this Device device, DiscoDataContext Database)
|
||||
{
|
||||
DeviceProfile deviceProfile = device.DeviceProfile;
|
||||
Expressions.Expression computerNameTemplateExpression = null;
|
||||
@@ -24,7 +26,7 @@ namespace Disco.BI.Extensions
|
||||
//return Expressions.Expression.TokenizeSingleDynamic(null, deviceProfile.Configuration(context).ComputerNameTemplate, 0);
|
||||
return Expressions.Expression.TokenizeSingleDynamic(null, deviceProfile.ComputerNameTemplate, 0);
|
||||
});
|
||||
System.Collections.IDictionary evaluatorVariables = Expressions.Expression.StandardVariables(null, context, UserBI.UserCache.CurrentUser, System.DateTime.Now, null);
|
||||
System.Collections.IDictionary evaluatorVariables = Expressions.Expression.StandardVariables(null, Database, UserService.CurrentUser, System.DateTime.Now, null);
|
||||
string rendered;
|
||||
try
|
||||
{
|
||||
@@ -40,12 +42,12 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
return rendered.ToString();
|
||||
}
|
||||
public static System.Collections.Generic.List<DocumentTemplate> AvailableDocumentTemplates(this Device d, DiscoDataContext Context, User User, System.DateTime TimeStamp)
|
||||
public static System.Collections.Generic.List<DocumentTemplate> AvailableDocumentTemplates(this Device d, DiscoDataContext Database, User User, System.DateTime TimeStamp)
|
||||
{
|
||||
List<DocumentTemplate> ats = Context.DocumentTemplates
|
||||
List<DocumentTemplate> ats = Database.DocumentTemplates
|
||||
.Where(at => at.Scope == Disco.Models.Repository.DocumentTemplate.DocumentTemplateScopes.Device).ToList();
|
||||
|
||||
return ats.Where(at => at.FilterExpressionMatches(d, Context, User, TimeStamp, DocumentState.DefaultState())).ToList();
|
||||
return ats.Where(at => at.FilterExpressionMatches(d, Database, User, TimeStamp, DocumentState.DefaultState())).ToList();
|
||||
}
|
||||
|
||||
public static bool UpdateLastNetworkLogonDate(this Device Device)
|
||||
@@ -53,7 +55,7 @@ namespace Disco.BI.Extensions
|
||||
return ActiveDirectoryUpdateLastNetworkLogonDateJob.UpdateLastNetworkLogonDate(Device);
|
||||
}
|
||||
|
||||
public static DeviceAttachment CreateAttachment(this Device Device, DiscoDataContext dbContext, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
|
||||
public static DeviceAttachment CreateAttachment(this Device Device, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
MimeType = Interop.MimeTypes.ResolveMimeType(Filename);
|
||||
@@ -71,20 +73,20 @@ namespace Disco.BI.Extensions
|
||||
if (DocumentTemplate != null)
|
||||
da.DocumentTemplateId = DocumentTemplate.Id;
|
||||
|
||||
dbContext.DeviceAttachments.Add(da);
|
||||
dbContext.SaveChanges();
|
||||
Database.DeviceAttachments.Add(da);
|
||||
Database.SaveChanges();
|
||||
|
||||
da.SaveAttachment(dbContext, Content);
|
||||
da.SaveAttachment(Database, Content);
|
||||
Content.Position = 0;
|
||||
if (PdfThumbnail == null)
|
||||
da.GenerateThumbnail(dbContext, Content);
|
||||
da.GenerateThumbnail(Database, Content);
|
||||
else
|
||||
da.SaveThumbnailAttachment(dbContext, PdfThumbnail);
|
||||
da.SaveThumbnailAttachment(Database, PdfThumbnail);
|
||||
|
||||
return da;
|
||||
}
|
||||
|
||||
public static Device AddOffline(this Device d, DiscoDataContext dbContext)
|
||||
public static Device AddOffline(this Device d, DiscoDataContext Database)
|
||||
{
|
||||
// Just Include:
|
||||
// - Serial Number
|
||||
@@ -93,17 +95,31 @@ namespace Disco.BI.Extensions
|
||||
// - Assigned User Id
|
||||
// - Batch
|
||||
|
||||
// Enforce Authorization
|
||||
var auth = UserService.CurrentAuthorization;
|
||||
if (!auth.Has(Claims.Device.Properties.AssetNumber))
|
||||
d.AssetNumber = null;
|
||||
if (!auth.Has(Claims.Device.Properties.Location))
|
||||
d.Location = null;
|
||||
if (!auth.Has(Claims.Device.Properties.DeviceBatch))
|
||||
d.DeviceBatchId = null;
|
||||
if (!auth.Has(Claims.Device.Properties.DeviceProfile))
|
||||
d.DeviceProfileId = Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId;
|
||||
if (!auth.Has(Claims.Device.Actions.AssignUser))
|
||||
d.AssignedUserId = null;
|
||||
|
||||
|
||||
// Batch
|
||||
DeviceBatch db = default(DeviceBatch);
|
||||
if (d.DeviceBatchId.HasValue)
|
||||
db = dbContext.DeviceBatches.Find(d.DeviceBatchId.Value);
|
||||
db = Database.DeviceBatches.Find(d.DeviceBatchId.Value);
|
||||
|
||||
// Default Device Model
|
||||
DeviceModel dm = default(DeviceModel);
|
||||
if (db != null && db.DefaultDeviceModelId.HasValue)
|
||||
dm = dbContext.DeviceModels.Find(db.DefaultDeviceModelId); // From Batch
|
||||
dm = Database.DeviceModels.Find(db.DefaultDeviceModelId); // From Batch
|
||||
else
|
||||
dm = dbContext.DeviceModels.Find(1); // Default
|
||||
dm = Database.DeviceModels.Find(1); // Default
|
||||
|
||||
Device d2 = new Device()
|
||||
{
|
||||
@@ -112,7 +128,7 @@ namespace Disco.BI.Extensions
|
||||
Location = d.Location,
|
||||
CreatedDate = DateTime.Now,
|
||||
DeviceProfileId = d.DeviceProfileId,
|
||||
DeviceProfile = dbContext.DeviceProfiles.Find(d.DeviceProfileId),
|
||||
DeviceProfile = Database.DeviceProfiles.Find(d.DeviceProfileId),
|
||||
AllowUnauthenticatedEnrol = true,
|
||||
DeviceModelId = dm.Id,
|
||||
DeviceModel = dm,
|
||||
@@ -120,22 +136,22 @@ namespace Disco.BI.Extensions
|
||||
DeviceBatch = db
|
||||
};
|
||||
|
||||
dbContext.Devices.Add(d2);
|
||||
Database.Devices.Add(d2);
|
||||
if (!string.IsNullOrEmpty(d.AssignedUserId))
|
||||
{
|
||||
User u = UserBI.UserCache.GetUser(d.AssignedUserId, dbContext, true);
|
||||
d2.AssignDevice(dbContext, u);
|
||||
User u = UserService.GetUser(d.AssignedUserId, Database, true);
|
||||
d2.AssignDevice(Database, u);
|
||||
}
|
||||
|
||||
return d2;
|
||||
}
|
||||
|
||||
public static DeviceUserAssignment AssignDevice(this Device d, DiscoDataContext dbContext, User u)
|
||||
public static DeviceUserAssignment AssignDevice(this Device d, DiscoDataContext Database, User u)
|
||||
{
|
||||
DeviceUserAssignment newDua = default(DeviceUserAssignment);
|
||||
|
||||
// Mark existing assignments as Unassigned
|
||||
foreach (var dua in dbContext.DeviceUserAssignments.Where(m => m.DeviceSerialNumber == d.SerialNumber && !m.UnassignedDate.HasValue))
|
||||
foreach (var dua in Database.DeviceUserAssignments.Where(m => m.DeviceSerialNumber == d.SerialNumber && !m.UnassignedDate.HasValue))
|
||||
dua.UnassignedDate = DateTime.Now;
|
||||
|
||||
if (u != null)
|
||||
@@ -147,7 +163,7 @@ namespace Disco.BI.Extensions
|
||||
AssignedUserId = u.Id,
|
||||
AssignedDate = DateTime.Now
|
||||
};
|
||||
dbContext.DeviceUserAssignments.Add(newDua);
|
||||
Database.DeviceUserAssignments.Add(newDua);
|
||||
|
||||
d.AssignedUserId = u.Id;
|
||||
d.AssignedUser = u;
|
||||
|
||||
@@ -6,6 +6,8 @@ using Disco.Models.Repository;
|
||||
using System.IO;
|
||||
using System.Drawing;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
@@ -73,22 +75,24 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
|
||||
#region Actions
|
||||
// Added 2012-11-26 G# - Need ability to delete Device Models
|
||||
public static bool CanDelete(this DeviceModel dm, DiscoDataContext dbContext)
|
||||
public static bool CanDelete(this DeviceModel dm, DiscoDataContext Database)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Config.DeviceModel.Delete))
|
||||
return false;
|
||||
|
||||
// Can't Delete Default Model (Id: 1)
|
||||
if (dm.Id == 1)
|
||||
return false;
|
||||
|
||||
// Can't Delete if Contains Devices
|
||||
if (dbContext.Devices.Count(d => d.DeviceModelId == dm.Id) > 0)
|
||||
if (Database.Devices.Count(d => d.DeviceModelId == dm.Id) > 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
public static void Delete(this DeviceModel dm, DiscoDataContext dbContext)
|
||||
public static void Delete(this DeviceModel dm, DiscoDataContext Database)
|
||||
{
|
||||
if (!dm.CanDelete(dbContext))
|
||||
if (!dm.CanDelete(Database))
|
||||
throw new InvalidOperationException("The state of this Device Model doesn't allow it to be deleted");
|
||||
|
||||
// Delete Image
|
||||
@@ -97,13 +101,13 @@ namespace Disco.BI.Extensions
|
||||
File.Delete(deviceModelImagePath);
|
||||
|
||||
// Delete any Device Model Components
|
||||
foreach (var deviceModelComponent in dbContext.DeviceComponents.Where(dc => dc.DeviceModelId == dm.Id).ToList())
|
||||
foreach (var deviceModelComponent in Database.DeviceComponents.Where(dc => dc.DeviceModelId == dm.Id).ToList())
|
||||
{
|
||||
dbContext.DeviceComponents.Remove(deviceModelComponent);
|
||||
Database.DeviceComponents.Remove(deviceModelComponent);
|
||||
}
|
||||
|
||||
// Delete Model
|
||||
dbContext.DeviceModels.Remove(dm);
|
||||
Database.DeviceModels.Remove(dm);
|
||||
}
|
||||
// End Added 2012-11-26 G#
|
||||
#endregion
|
||||
|
||||
@@ -6,6 +6,8 @@ using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Data.Configuration.Modules;
|
||||
using Disco.Models.BI.Config;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
@@ -18,11 +20,11 @@ namespace Disco.BI.Extensions
|
||||
Expressions.ExpressionCache.InvalidateKey(ComputerNameExpressionCacheModule, deviceProfile.Id.ToString());
|
||||
}
|
||||
|
||||
public static OrganisationAddress DefaultOrganisationAddressDetails(this DeviceProfile deviceProfile, DiscoDataContext dbContext)
|
||||
public static OrganisationAddress DefaultOrganisationAddressDetails(this DeviceProfile deviceProfile, DiscoDataContext Database)
|
||||
{
|
||||
if (deviceProfile.DefaultOrganisationAddress.HasValue)
|
||||
{
|
||||
return dbContext.DiscoConfiguration.OrganisationAddresses.GetAddress(deviceProfile.DefaultOrganisationAddress.Value);
|
||||
return Database.DiscoConfiguration.OrganisationAddresses.GetAddress(deviceProfile.DefaultOrganisationAddress.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -30,38 +32,35 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CanDelete(this DeviceProfile dp, DiscoDataContext dbContext)
|
||||
public static bool CanDelete(this DeviceProfile dp, DiscoDataContext Database)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Config.DeviceProfile.Delete))
|
||||
return false;
|
||||
|
||||
// Can't Delete Default Profile (Id: 1)
|
||||
if (dp.Id == 1)
|
||||
return false;
|
||||
|
||||
// Can't Delete if Contains Devices
|
||||
if (dbContext.Devices.Count(d => d.DeviceProfileId == dp.Id) > 0)
|
||||
if (Database.Devices.Count(d => d.DeviceProfileId == dp.Id) > 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
public static void Delete(this DeviceProfile dp, DiscoDataContext dbContext)
|
||||
public static void Delete(this DeviceProfile dp, DiscoDataContext Database)
|
||||
{
|
||||
if (!dp.CanDelete(dbContext))
|
||||
if (!dp.CanDelete(Database))
|
||||
throw new InvalidOperationException("The state of this Device Profile doesn't allow it to be deleted");
|
||||
|
||||
// Update Defaults
|
||||
if (dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId == dp.Id)
|
||||
dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId = 1;
|
||||
if (dbContext.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId == dp.Id)
|
||||
dbContext.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId = 1;
|
||||
if (Database.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId == dp.Id)
|
||||
Database.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId = 1;
|
||||
if (Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId == dp.Id)
|
||||
Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId = 1;
|
||||
|
||||
// Delete Profile
|
||||
dbContext.DeviceProfiles.Remove(dp);
|
||||
Database.DeviceProfiles.Remove(dp);
|
||||
}
|
||||
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//public static DeviceProfileConfiguration Configuration(this DeviceProfile dp, DiscoDataContext dbContext)
|
||||
//{
|
||||
// return dbContext.DiscoConfiguration.DeviceProfiles.DeviceProfile(dp);
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,13 @@ namespace Disco.BI.Extensions
|
||||
{
|
||||
private const string DocumentTemplateExpressionCacheTemplate = "DocumentTemplate_{0}";
|
||||
|
||||
public static string RepositoryFilename(this DocumentTemplate dt, DiscoDataContext dbContext)
|
||||
public static string RepositoryFilename(this DocumentTemplate dt, DiscoDataContext Database)
|
||||
{
|
||||
return System.IO.Path.Combine(DataStore.CreateLocation(dbContext, "DocumentTemplates"), string.Format("{0}.pdf", dt.Id));
|
||||
return System.IO.Path.Combine(DataStore.CreateLocation(Database, "DocumentTemplates"), string.Format("{0}.pdf", dt.Id));
|
||||
}
|
||||
public static string SavePdfTemplate(this DocumentTemplate dt, DiscoDataContext dbContext, Stream TemplateFile)
|
||||
public static string SavePdfTemplate(this DocumentTemplate dt, DiscoDataContext Database, Stream TemplateFile)
|
||||
{
|
||||
string filePath = dt.RepositoryFilename(dbContext);
|
||||
string filePath = dt.RepositoryFilename(Database);
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
TemplateFile.CopyTo(fs);
|
||||
@@ -39,14 +39,14 @@ namespace Disco.BI.Extensions
|
||||
return Interop.Pdf.PdfImporter.GetPageImages(pdfReader, PageNumber);
|
||||
}
|
||||
|
||||
public static ConcurrentDictionary<string, Expression> PdfExpressionsFromCache(this DocumentTemplate dt, DiscoDataContext dbContext)
|
||||
public static ConcurrentDictionary<string, Expression> PdfExpressionsFromCache(this DocumentTemplate dt, DiscoDataContext Database)
|
||||
{
|
||||
string cacheModuleKey = string.Format(DocumentTemplateExpressionCacheTemplate, dt.Id);
|
||||
var module = Expressions.ExpressionCache.GetModule(cacheModuleKey);
|
||||
if (module == null)
|
||||
{
|
||||
// Cache
|
||||
string templateFilename = dt.RepositoryFilename(dbContext);
|
||||
string templateFilename = dt.RepositoryFilename(Database);
|
||||
PdfReader pdfReader = new PdfReader(templateFilename);
|
||||
int pdfFieldOrdinal = 0;
|
||||
foreach (string pdfFieldKey in pdfReader.AcroFields.Fields.Keys)
|
||||
@@ -61,21 +61,21 @@ namespace Disco.BI.Extensions
|
||||
return module;
|
||||
}
|
||||
|
||||
public static List<BI.Expressions.Expression> ExtractPdfExpressions(this DocumentTemplate dt, DiscoDataContext dbContext)
|
||||
public static List<BI.Expressions.Expression> ExtractPdfExpressions(this DocumentTemplate dt, DiscoDataContext Database)
|
||||
{
|
||||
return dt.PdfExpressionsFromCache(dbContext).Values.OrderBy(e => e.Ordinal).ToList();
|
||||
return dt.PdfExpressionsFromCache(Database).Values.OrderBy(e => e.Ordinal).ToList();
|
||||
}
|
||||
public static System.IO.Stream GeneratePdfBulk(this DocumentTemplate dt, DiscoDataContext dbContext, User CreatorUser, System.DateTime Timestamp, params string[] DataObjectsIds)
|
||||
public static System.IO.Stream GeneratePdfBulk(this DocumentTemplate dt, DiscoDataContext Database, User CreatorUser, System.DateTime Timestamp, params string[] DataObjectsIds)
|
||||
{
|
||||
return Interop.Pdf.PdfGenerator.GenerateBulkFromTemplate(dt, dbContext, CreatorUser, Timestamp, DataObjectsIds);
|
||||
return Interop.Pdf.PdfGenerator.GenerateBulkFromTemplate(dt, Database, CreatorUser, Timestamp, DataObjectsIds);
|
||||
}
|
||||
public static System.IO.Stream GeneratePdfBulk(this DocumentTemplate dt, DiscoDataContext dbContext, User CreatorUser, System.DateTime Timestamp, params object[] DataObjects)
|
||||
public static System.IO.Stream GeneratePdfBulk(this DocumentTemplate dt, DiscoDataContext Database, User CreatorUser, System.DateTime Timestamp, params object[] DataObjects)
|
||||
{
|
||||
return Interop.Pdf.PdfGenerator.GenerateBulkFromTemplate(dt, dbContext, CreatorUser, Timestamp, DataObjects);
|
||||
return Interop.Pdf.PdfGenerator.GenerateBulkFromTemplate(dt, Database, CreatorUser, Timestamp, DataObjects);
|
||||
}
|
||||
public static System.IO.Stream GeneratePdf(this DocumentTemplate dt, DiscoDataContext dbContext, object Data, User CreatorUser, System.DateTime TimeStamp, DocumentState State, bool FlattenFields = false)
|
||||
public static System.IO.Stream GeneratePdf(this DocumentTemplate dt, DiscoDataContext Database, object Data, User CreatorUser, System.DateTime TimeStamp, DocumentState State, bool FlattenFields = false)
|
||||
{
|
||||
return Interop.Pdf.PdfGenerator.GenerateFromTemplate(dt, dbContext, Data, CreatorUser, TimeStamp, State, FlattenFields);
|
||||
return Interop.Pdf.PdfGenerator.GenerateFromTemplate(dt, Database, Data, CreatorUser, TimeStamp, State, FlattenFields);
|
||||
}
|
||||
|
||||
public static Expression FilterExpressionFromCache(this DocumentTemplate dt)
|
||||
@@ -86,12 +86,12 @@ namespace Disco.BI.Extensions
|
||||
{
|
||||
ExpressionCache.InvalidateKey("DocumentTemplateFilterExpression", dt.Id);
|
||||
}
|
||||
public static bool FilterExpressionMatches(this DocumentTemplate dt, object Data, DiscoDataContext DataContext, User User, System.DateTime TimeStamp, DocumentState State)
|
||||
public static bool FilterExpressionMatches(this DocumentTemplate dt, object Data, DiscoDataContext Database, User User, System.DateTime TimeStamp, DocumentState State)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(dt.FilterExpression))
|
||||
{
|
||||
Expression compiledExpression = dt.FilterExpressionFromCache();
|
||||
System.Collections.IDictionary evaluatorVariables = Expression.StandardVariables(dt, DataContext, User, TimeStamp, State);
|
||||
System.Collections.IDictionary evaluatorVariables = Expression.StandardVariables(dt, Database, User, TimeStamp, State);
|
||||
try
|
||||
{
|
||||
object er = compiledExpression.EvaluateFirst<object>(Data, evaluatorVariables);
|
||||
@@ -170,14 +170,14 @@ namespace Disco.BI.Extensions
|
||||
Page
|
||||
);
|
||||
}
|
||||
public static List<RectangleF> QRCodeLocations(this DocumentTemplate dt, DiscoDataContext dbContext)
|
||||
public static List<RectangleF> QRCodeLocations(this DocumentTemplate dt, DiscoDataContext Database)
|
||||
{
|
||||
return DocumentTemplateBI.DocumentTemplateQRCodeLocationCache.GetLocations(dt, dbContext);
|
||||
return DocumentTemplateBI.DocumentTemplateQRCodeLocationCache.GetLocations(dt, Database);
|
||||
}
|
||||
public static void Delete(this DocumentTemplate dt, DiscoDataContext Context)
|
||||
public static void Delete(this DocumentTemplate dt, DiscoDataContext Database)
|
||||
{
|
||||
// Find & Rename all references
|
||||
foreach (DeviceAttachment a in Context.DeviceAttachments.Where(a => a.DocumentTemplateId == dt.Id))
|
||||
foreach (DeviceAttachment a in Database.DeviceAttachments.Where(a => a.DocumentTemplateId == dt.Id))
|
||||
{
|
||||
a.Comments = string.Format("{0} - {1}", dt.Description, a.Comments);
|
||||
if (a.Comments.Length > 500)
|
||||
@@ -185,7 +185,7 @@ namespace Disco.BI.Extensions
|
||||
a.DocumentTemplateId = null;
|
||||
a.DocumentTemplate = null;
|
||||
}
|
||||
foreach (JobAttachment a in Context.JobAttachments.Where(a => a.DocumentTemplateId == dt.Id))
|
||||
foreach (JobAttachment a in Database.JobAttachments.Where(a => a.DocumentTemplateId == dt.Id))
|
||||
{
|
||||
a.Comments = string.Format("{0} - {1}", dt.Description, a.Comments);
|
||||
if (a.Comments.Length > 500)
|
||||
@@ -193,7 +193,7 @@ namespace Disco.BI.Extensions
|
||||
a.DocumentTemplateId = null;
|
||||
a.DocumentTemplate = null;
|
||||
}
|
||||
foreach (UserAttachment a in Context.UserAttachments.Where(a => a.DocumentTemplateId == dt.Id))
|
||||
foreach (UserAttachment a in Database.UserAttachments.Where(a => a.DocumentTemplateId == dt.Id))
|
||||
{
|
||||
a.Comments = string.Format("{0} - {1}", dt.Description, a.Comments);
|
||||
if (a.Comments.Length > 500)
|
||||
@@ -206,7 +206,7 @@ namespace Disco.BI.Extensions
|
||||
dt.JobSubTypes.Clear();
|
||||
|
||||
// Delete Template
|
||||
string templateRepositoryFilename = dt.RepositoryFilename(Context);
|
||||
string templateRepositoryFilename = dt.RepositoryFilename(Database);
|
||||
if (System.IO.File.Exists(templateRepositoryFilename))
|
||||
System.IO.File.Delete(templateRepositoryFilename);
|
||||
|
||||
@@ -214,7 +214,7 @@ namespace Disco.BI.Extensions
|
||||
dt.FilterExpressionInvalidateCache();
|
||||
|
||||
// Delete Document Template from Repository
|
||||
Context.DocumentTemplates.Remove(dt);
|
||||
Database.DocumentTemplates.Remove(dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ using Disco.Models.BI.Config;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Plugins;
|
||||
using Disco.Services.Plugins.Features.WarrantyProvider;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
@@ -15,6 +17,9 @@ namespace Disco.BI.Extensions
|
||||
#region Device Held
|
||||
public static bool CanDeviceHeld(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.DeviceHeld))
|
||||
return false;
|
||||
|
||||
return (!j.ClosedDate.HasValue) && (j.DeviceSerialNumber != null) &&
|
||||
(!j.DeviceHeld.HasValue || j.DeviceReturnedDate.HasValue);
|
||||
}
|
||||
@@ -35,6 +40,9 @@ namespace Disco.BI.Extensions
|
||||
#region Device Ready for Return
|
||||
public static bool CanDeviceReadyForReturn(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.DeviceReadyForReturn))
|
||||
return false;
|
||||
|
||||
return (!j.ClosedDate.HasValue) && j.DeviceHeld.HasValue &&
|
||||
!j.DeviceReadyForReturn.HasValue && !j.DeviceReturnedDate.HasValue;
|
||||
}
|
||||
@@ -51,6 +59,9 @@ namespace Disco.BI.Extensions
|
||||
#region Device Returned
|
||||
public static bool CanDeviceReturned(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.DeviceReturned))
|
||||
return false;
|
||||
|
||||
return (!j.ClosedDate.HasValue) && j.DeviceHeld.HasValue &&
|
||||
!j.DeviceReturnedDate.HasValue;
|
||||
}
|
||||
@@ -67,9 +78,12 @@ namespace Disco.BI.Extensions
|
||||
#region Waiting For User Action
|
||||
public static bool CanWaitingForUserAction(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.WaitingForUserAction))
|
||||
return false;
|
||||
|
||||
return !j.ClosedDate.HasValue && (j.UserId != null) && !j.WaitingForUserAction.HasValue;
|
||||
}
|
||||
public static void OnWaitingForUserAction(this Job j, DiscoDataContext dbContext, User Technician, string Reason)
|
||||
public static void OnWaitingForUserAction(this Job j, DiscoDataContext Database, User Technician, string Reason)
|
||||
{
|
||||
if (!j.CanWaitingForUserAction())
|
||||
throw new InvalidOperationException("Waiting for User Action was Denied");
|
||||
@@ -84,16 +98,19 @@ namespace Disco.BI.Extensions
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = string.Format("Waiting on User Action{0}Reason: {1}", Environment.NewLine, Reason)
|
||||
};
|
||||
dbContext.JobLogs.Add(jobLog);
|
||||
Database.JobLogs.Add(jobLog);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Not Waiting For User Action
|
||||
public static bool CanNotWaitingForUserAction(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.NotWaitingForUserAction))
|
||||
return false;
|
||||
|
||||
return j.WaitingForUserAction.HasValue;
|
||||
}
|
||||
public static void OnNotWaitingForUserAction(this Job j, DiscoDataContext dbContext, User Technician, string Resolution)
|
||||
public static void OnNotWaitingForUserAction(this Job j, DiscoDataContext Database, User Technician, string Resolution)
|
||||
{
|
||||
if (!j.CanNotWaitingForUserAction())
|
||||
throw new InvalidOperationException("Not Waiting for User Action was Denied");
|
||||
@@ -108,31 +125,34 @@ namespace Disco.BI.Extensions
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = string.Format("User Action Resolved{0}Resolution: {1}", Environment.NewLine, Resolution)
|
||||
};
|
||||
dbContext.JobLogs.Add(jobLog);
|
||||
Database.JobLogs.Add(jobLog);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Log Warranty
|
||||
public static bool CanLogWarranty(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.LogWarranty))
|
||||
return false;
|
||||
|
||||
return !j.ClosedDate.HasValue &&
|
||||
(j.DeviceSerialNumber != null) &&
|
||||
j.JobTypeId == JobType.JobTypeIds.HWar &&
|
||||
string.IsNullOrEmpty(j.JobMetaWarranty.ExternalReference);
|
||||
}
|
||||
public static void OnLogWarranty(this Job j, DiscoDataContext dbContext, string FaultDescription, PluginFeatureManifest WarrantyProviderDefinition, OrganisationAddress Address, User TechUser, Dictionary<string, string> WarrantyProviderProperties)
|
||||
public static void OnLogWarranty(this Job j, DiscoDataContext Database, string FaultDescription, PluginFeatureManifest WarrantyProviderDefinition, OrganisationAddress Address, User TechUser, Dictionary<string, string> WarrantyProviderProperties)
|
||||
{
|
||||
if (!j.CanLogWarranty())
|
||||
throw new InvalidOperationException("Log Warranty was Denied");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(FaultDescription))
|
||||
FaultDescription = j.GenerateFaultDescriptionFooter(dbContext, WarrantyProviderDefinition);
|
||||
FaultDescription = j.GenerateFaultDescriptionFooter(Database, WarrantyProviderDefinition);
|
||||
else
|
||||
FaultDescription = string.Concat(FaultDescription, Environment.NewLine, Environment.NewLine, j.GenerateFaultDescriptionFooter(dbContext, WarrantyProviderDefinition));
|
||||
FaultDescription = string.Concat(FaultDescription, Environment.NewLine, Environment.NewLine, j.GenerateFaultDescriptionFooter(Database, WarrantyProviderDefinition));
|
||||
|
||||
using (WarrantyProviderFeature WarrantyProvider = WarrantyProviderDefinition.CreateInstance<WarrantyProviderFeature>())
|
||||
{
|
||||
string providerRef = WarrantyProvider.SubmitJob(dbContext, j, Address, TechUser, FaultDescription, WarrantyProviderProperties);
|
||||
string providerRef = WarrantyProvider.SubmitJob(Database, j, Address, TechUser, FaultDescription, WarrantyProviderProperties);
|
||||
|
||||
j.JobMetaWarranty.ExternalLoggedDate = DateTime.Now;
|
||||
j.JobMetaWarranty.ExternalName = WarrantyProvider.WarrantyProviderId;
|
||||
@@ -150,7 +170,7 @@ namespace Disco.BI.Extensions
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = string.Format("Warranty Claim Submitted{0}{0}Provider: {1}{0}Repair Address: {2}{0}Provider Reference: {3}{0}{0}{4}", Environment.NewLine, WarrantyProvider.Manifest.Name, Address.Name, providerRef, FaultDescription)
|
||||
};
|
||||
dbContext.JobLogs.Add(jobLog);
|
||||
Database.JobLogs.Add(jobLog);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@@ -158,32 +178,35 @@ namespace Disco.BI.Extensions
|
||||
#region Convert HWar to HNWar
|
||||
public static bool CanConvertHWarToHNWar(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.ConvertHWarToHNWar))
|
||||
return false;
|
||||
|
||||
return !j.ClosedDate.HasValue && (j.DeviceSerialNumber != null) &&
|
||||
j.JobTypeId == JobType.JobTypeIds.HWar && string.IsNullOrEmpty(j.JobMetaWarranty.ExternalReference);
|
||||
}
|
||||
public static void OnConvertHWarToHNWar(this Job j, DiscoDataContext dbContext)
|
||||
public static void OnConvertHWarToHNWar(this Job j, DiscoDataContext Database)
|
||||
{
|
||||
if (!j.CanConvertHWarToHNWar())
|
||||
throw new InvalidOperationException("Convert HWar to HNWar was Denied");
|
||||
|
||||
var techUser = UserBI.UserCache.CurrentUser;
|
||||
var techUser = UserService.CurrentUser;
|
||||
|
||||
// Remove JobMetaWarranty
|
||||
if (j.JobMetaWarranty != null)
|
||||
dbContext.JobMetaWarranties.Remove(j.JobMetaWarranty);
|
||||
Database.JobMetaWarranties.Remove(j.JobMetaWarranty);
|
||||
|
||||
// Add JobMetaNonWarranty
|
||||
var metaHNWar = new JobMetaNonWarranty() { Job = j };
|
||||
dbContext.JobMetaNonWarranties.Add(metaHNWar);
|
||||
Database.JobMetaNonWarranties.Add(metaHNWar);
|
||||
|
||||
// Swap Job Sub Types
|
||||
List<string> jobSubTypes = j.JobSubTypes.Select(jst => jst.Id).ToList();
|
||||
j.JobSubTypes.Clear();
|
||||
foreach (var jst in dbContext.JobSubTypes.Where(i => i.JobTypeId == JobType.JobTypeIds.HNWar && jobSubTypes.Contains(i.Id)))
|
||||
foreach (var jst in Database.JobSubTypes.Where(i => i.JobTypeId == JobType.JobTypeIds.HNWar && jobSubTypes.Contains(i.Id)))
|
||||
j.JobSubTypes.Add(jst);
|
||||
|
||||
// Add Components
|
||||
var components = dbContext.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId);
|
||||
var components = Database.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId);
|
||||
var jobComponents = new List<DeviceComponent>();
|
||||
foreach (var component in components)
|
||||
{
|
||||
@@ -210,7 +233,7 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
foreach (var component in jobComponents)
|
||||
{
|
||||
dbContext.JobComponents.Add(new JobComponent()
|
||||
Database.JobComponents.Add(new JobComponent()
|
||||
{
|
||||
Job = j,
|
||||
TechUserId = techUser.Id,
|
||||
@@ -225,9 +248,9 @@ namespace Disco.BI.Extensions
|
||||
JobId = j.Id,
|
||||
TechUserId = techUser.Id,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = string.Format("Job Type Converted{0}From: {1}{0}To: {2}", Environment.NewLine, dbContext.JobTypes.Find(JobType.JobTypeIds.HWar), dbContext.JobTypes.Find(JobType.JobTypeIds.HNWar))
|
||||
Comments = string.Format("Job Type Converted{0}From: {1}{0}To: {2}", Environment.NewLine, Database.JobTypes.Find(JobType.JobTypeIds.HWar), Database.JobTypes.Find(JobType.JobTypeIds.HNWar))
|
||||
};
|
||||
dbContext.JobLogs.Add(jobLog);
|
||||
Database.JobLogs.Add(jobLog);
|
||||
|
||||
j.JobTypeId = JobType.JobTypeIds.HNWar;
|
||||
}
|
||||
@@ -252,6 +275,9 @@ namespace Disco.BI.Extensions
|
||||
#region Insurance Claim Form Sent
|
||||
public static bool CanInsuranceClaimFormSent(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.NonWarrantyProperties.InsuranceClaimFormSent))
|
||||
return false;
|
||||
|
||||
return (j.JobTypeId == JobType.JobTypeIds.HNWar) &&
|
||||
j.JobMetaNonWarranty.IsInsuranceClaim &&
|
||||
!j.JobMetaInsurance.ClaimFormSentDate.HasValue;
|
||||
@@ -261,7 +287,7 @@ namespace Disco.BI.Extensions
|
||||
if (!j.CanInsuranceClaimFormSent())
|
||||
throw new InvalidOperationException("Insurance Claim Form Sent was Denied");
|
||||
|
||||
var techUser = UserBI.UserCache.CurrentUser;
|
||||
var techUser = UserService.CurrentUser;
|
||||
|
||||
j.JobMetaInsurance.ClaimFormSentDate = DateTime.Now;
|
||||
j.JobMetaInsurance.ClaimFormSentUserId = techUser.Id;
|
||||
@@ -271,6 +297,9 @@ namespace Disco.BI.Extensions
|
||||
#region Log Repair
|
||||
public static bool CanLogRepair(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.LogRepair))
|
||||
return false;
|
||||
|
||||
return (j.JobTypeId == JobType.JobTypeIds.HNWar) &&
|
||||
(j.DeviceSerialNumber != null) &&
|
||||
!j.JobMetaNonWarranty.RepairerLoggedDate.HasValue &&
|
||||
@@ -292,6 +321,9 @@ namespace Disco.BI.Extensions
|
||||
#region Repair Complete
|
||||
public static bool CanRepairComplete(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Properties.NonWarrantyProperties.RepairerCompletedDate))
|
||||
return false;
|
||||
|
||||
return (j.JobTypeId == JobType.JobTypeIds.HNWar) &&
|
||||
j.JobMetaNonWarranty.RepairerLoggedDate.HasValue &&
|
||||
!j.JobMetaNonWarranty.RepairerCompletedDate.HasValue;
|
||||
@@ -308,6 +340,9 @@ namespace Disco.BI.Extensions
|
||||
#region Close
|
||||
public static bool CanClose(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.Close))
|
||||
return false;
|
||||
|
||||
if (j.ClosedDate.HasValue)
|
||||
return false; // Job already Closed
|
||||
|
||||
@@ -352,6 +387,9 @@ namespace Disco.BI.Extensions
|
||||
#region Force Close
|
||||
public static bool CanForceClose(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.ForceClose))
|
||||
return false;
|
||||
|
||||
var canCloseNormally = j.CanClose();
|
||||
|
||||
if (canCloseNormally)
|
||||
@@ -389,7 +427,7 @@ namespace Disco.BI.Extensions
|
||||
|
||||
return false;
|
||||
}
|
||||
public static void OnForceClose(this Job j, DiscoDataContext dbContext, User Technician, string Reason)
|
||||
public static void OnForceClose(this Job j, DiscoDataContext Database, User Technician, string Reason)
|
||||
{
|
||||
if (!j.CanForceClose())
|
||||
throw new InvalidOperationException("Force Close was Denied");
|
||||
@@ -402,7 +440,7 @@ namespace Disco.BI.Extensions
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = string.Format("Job Forcibly Closed{0}Reason: {1}", Environment.NewLine, Reason)
|
||||
};
|
||||
dbContext.JobLogs.Add(jobLog);
|
||||
Database.JobLogs.Add(jobLog);
|
||||
|
||||
j.ClosedDate = DateTime.Now;
|
||||
j.ClosedTechUserId = Technician.Id;
|
||||
@@ -412,6 +450,9 @@ namespace Disco.BI.Extensions
|
||||
#region Reopen
|
||||
public static bool CanReopen(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.Reopen))
|
||||
return false;
|
||||
|
||||
return j.ClosedDate.HasValue;
|
||||
}
|
||||
public static void OnReopen(this Job j)
|
||||
@@ -427,47 +468,50 @@ namespace Disco.BI.Extensions
|
||||
#region Delete
|
||||
public static bool CanDelete(this Job j)
|
||||
{
|
||||
if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.Delete))
|
||||
return false;
|
||||
|
||||
return j.ClosedDate.HasValue;
|
||||
}
|
||||
public static void OnDelete(this Job j, DiscoDataContext dbContext)
|
||||
public static void OnDelete(this Job j, DiscoDataContext Database)
|
||||
{
|
||||
// Job Sub Types
|
||||
j.JobSubTypes.Clear();
|
||||
|
||||
// Job Attachments
|
||||
foreach (var ja in j.JobAttachments.ToArray())
|
||||
ja.OnDelete(dbContext);
|
||||
ja.OnDelete(Database);
|
||||
j.JobAttachments.Clear();
|
||||
|
||||
// Job Components
|
||||
foreach (var jc in j.JobComponents.ToArray())
|
||||
dbContext.JobComponents.Remove(jc);
|
||||
Database.JobComponents.Remove(jc);
|
||||
j.JobComponents.Clear();
|
||||
|
||||
// Job Logs
|
||||
foreach (var jl in j.JobLogs.ToArray())
|
||||
dbContext.JobLogs.Remove(jl);
|
||||
Database.JobLogs.Remove(jl);
|
||||
j.JobLogs.Clear();
|
||||
|
||||
// Job Meta
|
||||
if (j.JobMetaInsurance != null)
|
||||
{
|
||||
dbContext.JobMetaInsurances.Remove(j.JobMetaInsurance);
|
||||
Database.JobMetaInsurances.Remove(j.JobMetaInsurance);
|
||||
j.JobMetaInsurance = null;
|
||||
}
|
||||
if (j.JobMetaNonWarranty != null)
|
||||
{
|
||||
dbContext.JobMetaNonWarranties.Remove(j.JobMetaNonWarranty);
|
||||
Database.JobMetaNonWarranties.Remove(j.JobMetaNonWarranty);
|
||||
j.JobMetaNonWarranty = null;
|
||||
}
|
||||
if (j.JobMetaWarranty != null)
|
||||
{
|
||||
dbContext.JobMetaWarranties.Remove(j.JobMetaWarranty);
|
||||
Database.JobMetaWarranties.Remove(j.JobMetaWarranty);
|
||||
j.JobMetaWarranty = null;
|
||||
}
|
||||
|
||||
// Job
|
||||
dbContext.Jobs.Remove(j);
|
||||
Database.Jobs.Remove(j);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class JobExtensions
|
||||
{
|
||||
public static JobAttachment CreateAttachment(this Job Job, DiscoDataContext dbContext, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
|
||||
public static JobAttachment CreateAttachment(this Job Job, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
MimeType = Interop.MimeTypes.ResolveMimeType(Filename);
|
||||
@@ -31,15 +31,15 @@ namespace Disco.BI.Extensions
|
||||
if (DocumentTemplate != null)
|
||||
ja.DocumentTemplateId = DocumentTemplate.Id;
|
||||
|
||||
dbContext.JobAttachments.Add(ja);
|
||||
dbContext.SaveChanges();
|
||||
Database.JobAttachments.Add(ja);
|
||||
Database.SaveChanges();
|
||||
|
||||
ja.SaveAttachment(dbContext, Content);
|
||||
ja.SaveAttachment(Database, Content);
|
||||
Content.Position = 0;
|
||||
if (PdfThumbnail == null)
|
||||
ja.GenerateThumbnail(dbContext, Content);
|
||||
ja.GenerateThumbnail(Database, Content);
|
||||
else
|
||||
ja.SaveThumbnailAttachment(dbContext, PdfThumbnail);
|
||||
ja.SaveThumbnailAttachment(Database, PdfThumbnail);
|
||||
|
||||
return ja;
|
||||
}
|
||||
@@ -146,9 +146,9 @@ namespace Disco.BI.Extensions
|
||||
return Job.JobStatusIds.Open;
|
||||
}
|
||||
|
||||
public static List<DocumentTemplate> AvailableDocumentTemplates(this Job j, DiscoDataContext dbContext, User User, DateTime TimeStamp)
|
||||
public static List<DocumentTemplate> AvailableDocumentTemplates(this Job j, DiscoDataContext Database, User User, DateTime TimeStamp)
|
||||
{
|
||||
var dts = dbContext.DocumentTemplates.Include("JobSubTypes")
|
||||
var dts = Database.DocumentTemplates.Include("JobSubTypes")
|
||||
.Where(dt => dt.Scope == DocumentTemplate.DocumentTemplateScopes.Job)
|
||||
.ToList();
|
||||
|
||||
@@ -171,7 +171,7 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
|
||||
// Evaluate Filters
|
||||
dts = dts.Where(dt => dt.FilterExpressionMatches(j, dbContext, User, TimeStamp, DocumentState.DefaultState())).ToList();
|
||||
dts = dts.Where(dt => dt.FilterExpressionMatches(j, Database, User, TimeStamp, DocumentState.DefaultState())).ToList();
|
||||
|
||||
return dts;
|
||||
}
|
||||
@@ -188,7 +188,7 @@ namespace Disco.BI.Extensions
|
||||
return d;
|
||||
}
|
||||
|
||||
public static string GenerateFaultDescription(this Job j, DiscoDataContext dbContext)
|
||||
public static string GenerateFaultDescription(this Job j, DiscoDataContext Database)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -199,14 +199,14 @@ namespace Disco.BI.Extensions
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static string GenerateFaultDescriptionFooter(this Job j, DiscoDataContext dbContext, PluginFeatureManifest WarrantyProviderDefinition)
|
||||
public static string GenerateFaultDescriptionFooter(this Job j, DiscoDataContext Database, PluginFeatureManifest WarrantyProviderDefinition)
|
||||
{
|
||||
var versionDisco = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
||||
return string.Format("Automation by Disco v{0}.{1}.{2:0000}.{3:0000} (Provider: {4} v{5})",
|
||||
versionDisco.Major, versionDisco.Minor, versionDisco.Build, versionDisco.Revision, WarrantyProviderDefinition.Id, WarrantyProviderDefinition.PluginManifest.Version.ToString(4));
|
||||
}
|
||||
|
||||
public static void UpdateSubTypes(this Job j, DiscoDataContext dbContext, List<JobSubType> SubTypes, bool AddComponents, User TechUser)
|
||||
public static void UpdateSubTypes(this Job j, DiscoDataContext Database, List<JobSubType> SubTypes, bool AddComponents, User TechUser)
|
||||
{
|
||||
if (SubTypes == null || SubTypes.Count == 0)
|
||||
throw new ArgumentException("The Job must contain at least one Sub Type");
|
||||
@@ -246,7 +246,7 @@ namespace Disco.BI.Extensions
|
||||
foreach (var t in addedSubTypes)
|
||||
logBuilder.Append("- ").AppendLine(t.ToString());
|
||||
}
|
||||
dbContext.JobLogs.Add(new JobLog()
|
||||
Database.JobLogs.Add(new JobLog()
|
||||
{
|
||||
JobId = j.Id,
|
||||
TechUserId = TechUser.Id,
|
||||
@@ -258,7 +258,7 @@ namespace Disco.BI.Extensions
|
||||
// Add Components
|
||||
if (AddComponents && addedSubTypes.Count > 0 && j.DeviceSerialNumber != null)
|
||||
{
|
||||
var components = dbContext.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId);
|
||||
var components = Database.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId);
|
||||
var addedComponents = new List<DeviceComponent>();
|
||||
foreach (var c in components)
|
||||
{
|
||||
@@ -280,7 +280,7 @@ namespace Disco.BI.Extensions
|
||||
{
|
||||
if (!j.JobComponents.Any(jc => jc.Description.Equals(c.Description, StringComparison.InvariantCultureIgnoreCase)))
|
||||
{ // Job Component with matching Description doesn't exist.
|
||||
dbContext.JobComponents.Add(new JobComponent()
|
||||
Database.JobComponents.Add(new JobComponent()
|
||||
{
|
||||
Job = j,
|
||||
TechUserId = TechUser.Id,
|
||||
|
||||
@@ -5,16 +5,94 @@ using System.Text;
|
||||
using Disco.Models.BI.Job;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Users;
|
||||
using Disco.Services.Authorization;
|
||||
|
||||
namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class JobTableExtensions
|
||||
{
|
||||
private static List<string> FilterAllowedTypes(AuthorizationToken Authorization)
|
||||
{
|
||||
if (!Authorization.HasAll(Claims.Job.Types.ShowHMisc, Claims.Job.Types.ShowHNWar, Claims.Job.Types.ShowHWar, Claims.Job.Types.ShowSApp, Claims.Job.Types.ShowSImg, Claims.Job.Types.ShowSOS, Claims.Job.Types.ShowUMgmt))
|
||||
{
|
||||
// Must Filter
|
||||
List<string> allowedTypes = new List<string>(6);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowHMisc))
|
||||
allowedTypes.Add(JobType.JobTypeIds.HMisc);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowHNWar))
|
||||
allowedTypes.Add(JobType.JobTypeIds.HNWar);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowHWar))
|
||||
allowedTypes.Add(JobType.JobTypeIds.HWar);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowSApp))
|
||||
allowedTypes.Add(JobType.JobTypeIds.SApp);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowSImg))
|
||||
allowedTypes.Add(JobType.JobTypeIds.SImg);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowSOS))
|
||||
allowedTypes.Add(JobType.JobTypeIds.SOS);
|
||||
if (Authorization.Has(Claims.Job.Types.ShowUMgmt))
|
||||
allowedTypes.Add(JobType.JobTypeIds.UMgmt);
|
||||
|
||||
public static List<JobTableModel.JobTableItemModel> DetermineItems(this JobTableModel model, DiscoDataContext dbContext, IQueryable<Job> Jobs)
|
||||
return allowedTypes;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IQueryable<Job> FilterPermissions(this JobTableModel model, IQueryable<Job> Jobs, AuthorizationToken Authorization)
|
||||
{
|
||||
var allowedTypes = FilterAllowedTypes(Authorization);
|
||||
|
||||
if (allowedTypes != null)
|
||||
{
|
||||
return Jobs.Where(j => allowedTypes.Contains(j.JobTypeId));
|
||||
}
|
||||
|
||||
return Jobs;
|
||||
}
|
||||
|
||||
public static List<JobTableModel.JobTableItemModel> PermissionsFiltered(this List<JobTableModel.JobTableItemModel> Items, AuthorizationToken Authorization)
|
||||
{
|
||||
if (Items != null && Items.Count > 0)
|
||||
{
|
||||
var allowedTypes = FilterAllowedTypes(Authorization);
|
||||
|
||||
if (allowedTypes != null)
|
||||
{
|
||||
return Items.Where(j => allowedTypes.Contains(j.TypeId)).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
return Items;
|
||||
}
|
||||
|
||||
public static List<JobTableModel.JobTableItemModel> DetermineItems(this JobTableModel model, DiscoDataContext Database, IQueryable<Job> Jobs)
|
||||
{
|
||||
List<JobTableModel.JobTableItemModel> items;
|
||||
|
||||
// Permissions
|
||||
var auth = UserService.CurrentAuthorization;
|
||||
if (!auth.HasAll(Claims.Job.Types.ShowHMisc, Claims.Job.Types.ShowHNWar, Claims.Job.Types.ShowHWar, Claims.Job.Types.ShowSApp, Claims.Job.Types.ShowSImg, Claims.Job.Types.ShowSOS, Claims.Job.Types.ShowUMgmt))
|
||||
{
|
||||
// Must Filter
|
||||
List<string> allowedTypes = new List<string>(6);
|
||||
if (auth.Has(Claims.Job.Types.ShowHMisc))
|
||||
allowedTypes.Add(JobType.JobTypeIds.HMisc);
|
||||
if (auth.Has(Claims.Job.Types.ShowHNWar))
|
||||
allowedTypes.Add(JobType.JobTypeIds.HNWar);
|
||||
if (auth.Has(Claims.Job.Types.ShowHWar))
|
||||
allowedTypes.Add(JobType.JobTypeIds.HWar);
|
||||
if (auth.Has(Claims.Job.Types.ShowSApp))
|
||||
allowedTypes.Add(JobType.JobTypeIds.SApp);
|
||||
if (auth.Has(Claims.Job.Types.ShowSImg))
|
||||
allowedTypes.Add(JobType.JobTypeIds.SImg);
|
||||
if (auth.Has(Claims.Job.Types.ShowSOS))
|
||||
allowedTypes.Add(JobType.JobTypeIds.SOS);
|
||||
if (auth.Has(Claims.Job.Types.ShowUMgmt))
|
||||
allowedTypes.Add(JobType.JobTypeIds.UMgmt);
|
||||
|
||||
Jobs = Jobs.Where(j => allowedTypes.Contains(j.JobTypeId));
|
||||
}
|
||||
|
||||
if (model.ShowStatus)
|
||||
{
|
||||
|
||||
@@ -86,18 +164,18 @@ namespace Disco.BI.Extensions
|
||||
}
|
||||
|
||||
if (!model.ShowDeviceAddress.HasValue)
|
||||
model.ShowDeviceAddress = dbContext.DiscoConfiguration.MultiSiteMode;
|
||||
model.ShowDeviceAddress = Database.DiscoConfiguration.MultiSiteMode;
|
||||
|
||||
foreach (var j in items)
|
||||
if (j.DeviceAddressId.HasValue)
|
||||
j.DeviceAddress = dbContext.DiscoConfiguration.OrganisationAddresses.GetAddress(j.DeviceAddressId.Value).Name;
|
||||
j.DeviceAddress = Database.DiscoConfiguration.OrganisationAddresses.GetAddress(j.DeviceAddressId.Value).Name;
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public static void Fill(this JobTableModel model, DiscoDataContext dbContext, IQueryable<Job> Jobs)
|
||||
public static void Fill(this JobTableModel model, DiscoDataContext Database, IQueryable<Job> Jobs)
|
||||
{
|
||||
model.Items = model.DetermineItems(dbContext, Jobs);
|
||||
model.Items = model.DetermineItems(Database, Jobs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Disco.BI.Extensions
|
||||
{
|
||||
public static class UserExtensions
|
||||
{
|
||||
public static UserAttachment CreateAttachment(this User User, DiscoDataContext dbContext, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
|
||||
public static UserAttachment CreateAttachment(this User User, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
MimeType = Interop.MimeTypes.ResolveMimeType(Filename);
|
||||
@@ -30,25 +30,25 @@ namespace Disco.BI.Extensions
|
||||
if (DocumentTemplate != null)
|
||||
ua.DocumentTemplateId = DocumentTemplate.Id;
|
||||
|
||||
dbContext.UserAttachments.Add(ua);
|
||||
dbContext.SaveChanges();
|
||||
Database.UserAttachments.Add(ua);
|
||||
Database.SaveChanges();
|
||||
|
||||
ua.SaveAttachment(dbContext, Content);
|
||||
ua.SaveAttachment(Database, Content);
|
||||
Content.Position = 0;
|
||||
if (PdfThumbnail == null)
|
||||
ua.GenerateThumbnail(dbContext, Content);
|
||||
ua.GenerateThumbnail(Database, Content);
|
||||
else
|
||||
ua.SaveThumbnailAttachment(dbContext, PdfThumbnail);
|
||||
ua.SaveThumbnailAttachment(Database, PdfThumbnail);
|
||||
|
||||
return ua;
|
||||
}
|
||||
|
||||
public static List<DocumentTemplate> AvailableDocumentTemplates(this User u, DiscoDataContext dbContext, User User, DateTime TimeStamp)
|
||||
public static List<DocumentTemplate> AvailableDocumentTemplates(this User u, DiscoDataContext Database, User User, DateTime TimeStamp)
|
||||
{
|
||||
var dts = dbContext.DocumentTemplates.Include("JobSubTypes")
|
||||
var dts = Database.DocumentTemplates.Include("JobSubTypes")
|
||||
.Where(dt => dt.Scope == DocumentTemplate.DocumentTemplateScopes.User)
|
||||
.ToArray()
|
||||
.Where(dt => dt.FilterExpressionMatches(u, dbContext, User, TimeStamp, DocumentState.DefaultState())).ToList();
|
||||
.Where(dt => dt.FilterExpressionMatches(u, Database, User, TimeStamp, DocumentState.DefaultState())).ToList();
|
||||
|
||||
return dts;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user