From 5279b00c9c6366ea2828192a74ec306488183996 Mon Sep 17 00:00:00 2001 From: "LAPTOP-CTLLJBUM\\man_l" Date: Wed, 7 Sep 2022 19:49:50 +0100 Subject: [PATCH 1/3] DLSV2-621 Make self assessment exported Excel report read only --- .../Extensions/ConfigurationExtensions.cs | 7 ++++ .../CandidateAssessmentDownloadFileService.cs | 35 +++++++++++++------ .../SelfAssessment.cs | 2 +- DigitalLearningSolutions.Web/appsettings.json | 3 +- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs b/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs index 1af7e9c2db..81e1c34156 100644 --- a/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs +++ b/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs @@ -27,6 +27,8 @@ public static class ConfigurationExtensions private const string JavascriptSearchSortFilterPaginateItemLimitKey = "JavascriptSearchSortFilterPaginateItemLimit"; + private const string ExcelPassword = "ExcelPassword"; + public static string GetAppRootPath(this IConfiguration config) { return config[AppRootPathName]; @@ -106,5 +108,10 @@ public static int GetJavascriptSearchSortFilterPaginateItemLimit(this IConfigura { return int.Parse(config[JavascriptSearchSortFilterPaginateItemLimitKey]); } + + public static string GetExcelPassword(this IConfiguration config) + { + return config[ExcelPassword]; + } } } diff --git a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs index e1a81c6eb8..029f198649 100644 --- a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs +++ b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs @@ -2,7 +2,9 @@ { using ClosedXML.Excel; using DigitalLearningSolutions.Data.DataServices.SelfAssessmentDataService; + using DigitalLearningSolutions.Data.Extensions; using DigitalLearningSolutions.Data.Models.SelfAssessments.Export; + using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; @@ -10,22 +12,24 @@ public interface ICandidateAssessmentDownloadFileService { - public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId); + public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId, bool isProtected); } public class CandidateAssessmentDownloadFileService : ICandidateAssessmentDownloadFileService { private readonly ISelfAssessmentDataService selfAssessmentDataService; + private readonly IConfiguration config; public CandidateAssessmentDownloadFileService( - ISelfAssessmentDataService selfAssessmentDataService + ISelfAssessmentDataService selfAssessmentDataService, + IConfiguration config ) { this.selfAssessmentDataService = selfAssessmentDataService; + this.config = config; } - public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId) + public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId, bool isProtected) { - var summaryData = selfAssessmentDataService.GetCandidateAssessmentExportSummary(candidateAssessmentId, candidateId); var detailData = selfAssessmentDataService.GetCandidateAssessmentExportDetails(candidateAssessmentId, candidateId); var details = detailData.Select( @@ -47,20 +51,26 @@ public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmen } ); using var workbook = new XLWorkbook(); - AddSummarySheet(workbook, summaryData); - AddSheetToWorkbook(workbook, "Details", details); + var excelPassword = config.GetExcelPassword(); + AddSummarySheet(workbook, summaryData, excelPassword, isProtected); + AddSheetToWorkbook(workbook, "Details", details, excelPassword, isProtected); using var stream = new MemoryStream(); workbook.SaveAs(stream); return stream.ToArray(); } - private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable? dataObjects) + private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable? dataObjects,string excelPassword, bool? isProtected = false) { var sheet = workbook.Worksheets.Add(sheetName); var table = sheet.Cell(1, 1).InsertTable(dataObjects); table.Theme = XLTableTheme.TableStyleLight9; sheet.Columns().AdjustToContents(); + if (isProtected.Value) + { + sheet.Protect(excelPassword); + sheet.Columns().Style.Protection.SetLocked(true); + } } - private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExportSummary candidateAssessmentExportSummary) + private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExportSummary candidateAssessmentExportSummary,string excelPassword, bool? isProtected = false) { var sheet = workbook.Worksheets.Add("Summary"); var rowNum = 1; @@ -99,7 +109,7 @@ private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExp sheet.Cell(rowNum, 1).Value = "Questions with no role requirements set"; sheet.Cell(rowNum, 1).Style.Fill.BackgroundColor = XLColor.LightBlue; sheet.Cell(rowNum, 2).Value = candidateAssessmentExportSummary.NoRequirementsSetCount; - rowNum++; + rowNum++; } if (candidateAssessmentExportSummary.MeetingCount > 0) @@ -153,7 +163,12 @@ private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExp sheet.Cells(true).Style.Font.FontSize = 16; sheet.Rows().AdjustToContents(); sheet.Columns().AdjustToContents(); - sheet.Columns("2").Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right).Font.SetBold(); + sheet.Columns("2").Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right).Font.SetBold(); + if (isProtected.Value) + { + sheet.Protect(excelPassword); + sheet.Columns().Style.Protection.SetLocked(true); + } } } } diff --git a/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs b/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs index 79fcf8bb67..40af7c257c 100644 --- a/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs +++ b/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs @@ -1230,7 +1230,7 @@ public IActionResult SignOffHistory(int selfAssessmentId, string vocabulary) } public IActionResult ExportCandidateAssessment(int candidateAssessmentId, string vocabulary) { - var content = candidateAssessmentDownloadFileService.GetCandidateAssessmentDownloadFileForCentre(candidateAssessmentId, GetCandidateId()); + var content = candidateAssessmentDownloadFileService.GetCandidateAssessmentDownloadFileForCentre(candidateAssessmentId, GetCandidateId(), true); var fileName = $"DLS {vocabulary} Assessment Export {DateTime.Today:yyyy-MM-dd}.xlsx"; return File( content, diff --git a/DigitalLearningSolutions.Web/appsettings.json b/DigitalLearningSolutions.Web/appsettings.json index a95887004e..c365ec4f76 100644 --- a/DigitalLearningSolutions.Web/appsettings.json +++ b/DigitalLearningSolutions.Web/appsettings.json @@ -27,5 +27,6 @@ "LoginEndpoint": "/login", "LinkingEndpoint": "/create-user", "ClientCode": "DigitalLearningSolutions" - } + }, + "ExcelPassword": "0f8cc6f0-9f21-4f0f-9cb9-365675490458" } From 6c5f74f1c7f0c2a238135581a4c76a6469caa73b Mon Sep 17 00:00:00 2001 From: "LAPTOP-CTLLJBUM\\man_l" Date: Thu, 8 Sep 2022 12:14:38 +0100 Subject: [PATCH 2/3] PR changes for isProtected to be not nullable --- .../Services/CandidateAssessmentDownloadFileService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs index 029f198649..b0f002e145 100644 --- a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs +++ b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs @@ -58,19 +58,19 @@ public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmen workbook.SaveAs(stream); return stream.ToArray(); } - private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable? dataObjects,string excelPassword, bool? isProtected = false) + private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable? dataObjects,string excelPassword, bool isProtected) { var sheet = workbook.Worksheets.Add(sheetName); var table = sheet.Cell(1, 1).InsertTable(dataObjects); table.Theme = XLTableTheme.TableStyleLight9; sheet.Columns().AdjustToContents(); - if (isProtected.Value) + if (isProtected) { sheet.Protect(excelPassword); sheet.Columns().Style.Protection.SetLocked(true); } } - private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExportSummary candidateAssessmentExportSummary,string excelPassword, bool? isProtected = false) + private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExportSummary candidateAssessmentExportSummary,string excelPassword, bool isProtected) { var sheet = workbook.Worksheets.Add("Summary"); var rowNum = 1; @@ -164,7 +164,7 @@ private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExp sheet.Rows().AdjustToContents(); sheet.Columns().AdjustToContents(); sheet.Columns("2").Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right).Font.SetBold(); - if (isProtected.Value) + if (isProtected) { sheet.Protect(excelPassword); sheet.Columns().Style.Protection.SetLocked(true); From f9e5c9fa18f7b40f7cd763a61f6330727a016bbb Mon Sep 17 00:00:00 2001 From: "LAPTOP-CTLLJBUM\\man_l" Date: Thu, 8 Sep 2022 14:19:57 +0100 Subject: [PATCH 3/3] DLSV2-621 comments incorporated --- .../Services/CandidateAssessmentDownloadFileService.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs index b0f002e145..f0506185f9 100644 --- a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs +++ b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs @@ -163,12 +163,7 @@ private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExp sheet.Cells(true).Style.Font.FontSize = 16; sheet.Rows().AdjustToContents(); sheet.Columns().AdjustToContents(); - sheet.Columns("2").Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right).Font.SetBold(); - if (isProtected) - { - sheet.Protect(excelPassword); - sheet.Columns().Style.Protection.SetLocked(true); - } + sheet.Columns("2").Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right).Font.SetBold(); } } }