From 05593a1466b9d3052410c719cb32ac39ec2cb1a9 Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Sun, 4 Dec 2022 13:38:18 +1100 Subject: [PATCH] maintenance: document template refactoring --- .../Documents/DocumentTemplateExtensions.cs | 62 ++++++++++++++++--- .../Documents/DocumentUniqueIdentifier.cs | 7 +++ .../Controllers/DocumentTemplateController.cs | 2 +- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/Disco.Services/Documents/DocumentTemplateExtensions.cs b/Disco.Services/Documents/DocumentTemplateExtensions.cs index b01c9554..8d776540 100644 --- a/Disco.Services/Documents/DocumentTemplateExtensions.cs +++ b/Disco.Services/Documents/DocumentTemplateExtensions.cs @@ -1,8 +1,10 @@ using Disco.Data.Repository; using Disco.Models.Repository; +using Disco.Services.Authorization; using Disco.Services.Interop.ActiveDirectory; using Disco.Services.Users; using System; +using System.Data.Entity; using System.Drawing; using System.IO; using System.Linq; @@ -57,7 +59,7 @@ namespace Disco.Services // Fill white background graphics.FillRectangle(Brushes.White, destination); - using (var image = pdfDocument.Render(pageIndex, (int)previewWidth, (int)previewHeight, 72F, 72F, false)) + using (var image = pdfDocument.Render(pageIndex, (int)previewWidth, (int)previewHeight, 72F, 72F, true)) { graphics.DrawImage(image, destination.X, destination.Y); } @@ -85,6 +87,9 @@ namespace Disco.Services } public static IAttachmentTarget ResolveScopeTarget(this AttachmentTypes scope, DiscoDataContext database, string targetId) + => ResolveScopeTarget(scope, database, targetId, out _); + + public static IAttachmentTarget ResolveScopeTarget(this AttachmentTypes scope, DiscoDataContext database, string targetId, out User targetUser) { if (database == null) throw new ArgumentNullException(nameof(database)); @@ -94,33 +99,74 @@ namespace Disco.Services switch (scope) { case AttachmentTypes.Device: - return database.Devices.Find(targetId); + var device = database.Devices.Find(targetId); + targetUser = device?.AssignedUser; + return device; case AttachmentTypes.Job: if (!int.TryParse(targetId, out var targetIdInt)) throw new ArgumentOutOfRangeException(nameof(targetId)); - return database.Jobs.Find(targetIdInt); + var job = database.Jobs.Find(targetIdInt); + targetUser = job?.User; + return job; case AttachmentTypes.User: // special usecase in resolving users (they may not exist in the database yet) targetId = ActiveDirectory.ParseDomainAccountId(targetId); - var target = database.Users.Find(targetId); - if (target == null) + var user = database.Users.Find(targetId); + if (user == null) { // try importing user - target = UserService.GetUser(targetId, database, true); + user = UserService.GetUser(targetId, database, true); } - return target; + targetUser = user; + return user; default: throw new InvalidOperationException("Unexpected DocumentType Scope"); } } public static IAttachmentTarget ResolveScopeTarget(this DocumentTemplate template, DiscoDataContext database, string targetId) + => ResolveScopeTarget(template, database, targetId, out _); + + public static IAttachmentTarget ResolveScopeTarget(this DocumentTemplate template, DiscoDataContext database, string targetId, out User targetUser) { if (template == null) throw new ArgumentNullException(nameof(template)); - return ResolveScopeTarget(template.AttachmentType, database, targetId); + return ResolveScopeTarget(template.AttachmentType, database, targetId, out targetUser); } + public static void GetTemplateAndTarget(DiscoDataContext database, AuthorizationToken authorization, string templateId, string targetId, out DocumentTemplate template, out IAttachmentTarget target, out User targetUser) + { + if (string.IsNullOrWhiteSpace(templateId)) + throw new ArgumentNullException(nameof(templateId)); + if (string.IsNullOrWhiteSpace(targetId)) + throw new ArgumentNullException(nameof(targetId)); + + // get template + template = database.DocumentTemplates.Find(templateId); + if (template == null) + throw new ArgumentException("Invalid document template id", nameof(templateId)); + + // validate authorization + switch (template.Scope) + { + case DocumentTemplate.DocumentTemplateScopes.Device: + authorization.Require(Claims.Device.Actions.GenerateDocuments); + break; + case DocumentTemplate.DocumentTemplateScopes.Job: + authorization.Require(Claims.Job.Actions.GenerateDocuments); + break; + case DocumentTemplate.DocumentTemplateScopes.User: + authorization.Require(Claims.User.Actions.GenerateDocuments); + break; + default: + throw new InvalidOperationException("Unknown DocumentType Scope"); + } + + // resolve target + target = template.ResolveScopeTarget(database, targetId, out targetUser); + if (target == null) + throw new ArgumentException("Target not found", nameof(targetId)); + } } } diff --git a/Disco.Services/Documents/DocumentUniqueIdentifier.cs b/Disco.Services/Documents/DocumentUniqueIdentifier.cs index 4956ba1a..01223977 100644 --- a/Disco.Services/Documents/DocumentUniqueIdentifier.cs +++ b/Disco.Services/Documents/DocumentUniqueIdentifier.cs @@ -303,6 +303,13 @@ namespace Disco.Services.Documents return new DocumentUniqueIdentifier(Database, CurrentVersion, deploymentChecksum, DocumentTemplateId, TargetId, CreatorId, TimeStamp, PageIndex, null); } + public static DocumentUniqueIdentifier Create(DiscoDataContext Database, string DocumentTemplateId, AttachmentTypes attachmentType, string TargetId, string CreatorId, DateTime TimeStamp, int PageIndex) + { + var deploymentChecksum = Database.DiscoConfiguration.DeploymentChecksum; + + return new DocumentUniqueIdentifier(Database, CurrentVersion, deploymentChecksum, DocumentTemplateId, TargetId, CreatorId, TimeStamp, PageIndex, attachmentType); + } + public static DocumentUniqueIdentifier Parse(DiscoDataContext Database, byte[] UniqueIdentifier) { DocumentUniqueIdentifier identifier; diff --git a/Disco.Web/Areas/API/Controllers/DocumentTemplateController.cs b/Disco.Web/Areas/API/Controllers/DocumentTemplateController.cs index 4fbda51b..ca319bdd 100644 --- a/Disco.Web/Areas/API/Controllers/DocumentTemplateController.cs +++ b/Disco.Web/Areas/API/Controllers/DocumentTemplateController.cs @@ -612,7 +612,7 @@ namespace Disco.Web.Areas.API.Controllers var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf")); var identifier = DocumentUniqueIdentifier.Create(Database, DocumentTemplateId, DataId, UserService.CurrentUser.UserId, DateTime.Now, 0); - if (Disco.Services.Documents.AttachmentImport.Importer.ImportPdfAttachment(identifier, Database, filename)) + if (Disco.Services.Documents.AttachmentImport.Importer.ImportPdfAttachment(identifier, Database, filename) != null) { // Delete File System.IO.File.Delete(filename);