Skip to content

Commit 254d3ab

Browse files
committed
Develop/Fixes/TD-5399-CorrectingRoutingAndAddingServiceFilter
1 parent 618ee37 commit 254d3ab

File tree

4 files changed

+101
-16
lines changed

4 files changed

+101
-16
lines changed

DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
using Microsoft.AspNetCore.Mvc.ViewEngines;
2929
using Microsoft.AspNetCore.Mvc.ViewFeatures;
3030
using System.IO;
31-
31+
[ServiceFilter(typeof(RequireProcessAgreementFilter))]
3232
public partial class LearningPortalController
3333
{
3434
private const string CookieName = "DLSSelfAssessmentService";
@@ -70,13 +70,7 @@ public IActionResult SelfAssessment(int selfAssessmentId)
7070

7171
if (!selfAssessment.SelfAssessmentProcessAgreed && selfAssessment.IsSupervised)
7272
{
73-
var processmodel = new SelfAssessmentProcessViewModel()
74-
{
75-
SelfAssessmentID = selfAssessmentId,
76-
Vocabulary = selfAssessment.Vocabulary,
77-
VocabPlural = FrameworkVocabularyHelper.VocabularyPlural(selfAssessment.Vocabulary)
78-
};
79-
return View("SelfAssessments/AgreeSelfAssessmentProcess", processmodel);
73+
return RedirectToAction("AgreeSelfAssessmentProcess", new { selfAssessmentId });
8074
}
8175

8276
selfAssessmentService.IncrementLaunchCount(selfAssessmentId, delegateUserId);
@@ -90,7 +84,30 @@ public IActionResult SelfAssessment(int selfAssessmentId)
9084
return View("SelfAssessments/SelfAssessmentDescription", model);
9185
}
9286

93-
[HttpPost]
87+
[Route("/LearningPortal/SelfAssessment/{selfAssessmentId:int}/AgreeProcess")]
88+
public IActionResult AgreeSelfAssessmentProcess(int selfAssessmentId)
89+
{
90+
var delegateUserId = User.GetUserIdKnownNotNull();
91+
var selfAssessment = selfAssessmentService.GetSelfAssessmentForCandidateById(delegateUserId, selfAssessmentId);
92+
93+
if (selfAssessment == null)
94+
{
95+
logger.LogWarning(
96+
$"Attempt to display self assessment process for user {delegateUserId} with no self assessment"
97+
);
98+
return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
99+
}
100+
101+
var processmodel = new SelfAssessmentProcessViewModel()
102+
{
103+
SelfAssessmentID = selfAssessmentId,
104+
Vocabulary = selfAssessment.Vocabulary,
105+
VocabPlural = FrameworkVocabularyHelper.VocabularyPlural(selfAssessment.Vocabulary)
106+
};
107+
return View("SelfAssessments/AgreeSelfAssessmentProcess", processmodel);
108+
}
109+
110+
[HttpPost("/LearningPortal/SelfAssessment/{selfAssessmentId:int}/AgreeProcess")]
94111
public IActionResult ProcessAgreed(SelfAssessmentProcessViewModel model)
95112
{
96113
if (!ModelState.IsValid)
@@ -107,13 +124,9 @@ public IActionResult ProcessAgreed(SelfAssessmentProcessViewModel model)
107124
);
108125
return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
109126
}
110-
var supervisors = selfAssessmentService.GetAllSupervisorsForSelfAssessmentId(
111-
selfAssessmentId,
112-
delegateUserId
113-
).ToList();
114-
var selfAssessmentDescriptionViewModel = new SelfAssessmentDescriptionViewModel(selfAssessment, supervisors);
127+
115128
selfAssessmentService.MarkProgressAgreed(selfAssessmentId, delegateUserId);
116-
return View("SelfAssessments/SelfAssessmentDescription", selfAssessmentDescriptionViewModel);
129+
return RedirectToAction("SelfAssessment", new { selfAssessmentId });
117130

118131
}
119132

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace DigitalLearningSolutions.Web.ServiceFilter
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.AspNetCore.Mvc.Filters;
5+
using DigitalLearningSolutions.Web.Services;
6+
using DigitalLearningSolutions.Web.Helpers;
7+
using Microsoft.Extensions.Logging;
8+
9+
public class RequireProcessAgreementFilter : IActionFilter
10+
{
11+
private readonly ISelfAssessmentService selfAssessmentService;
12+
private readonly ILogger<RequireProcessAgreementFilter> logger;
13+
14+
public RequireProcessAgreementFilter(
15+
ISelfAssessmentService selfAssessmentService,
16+
ILogger<RequireProcessAgreementFilter> logger
17+
)
18+
{
19+
this.selfAssessmentService = selfAssessmentService;
20+
this.logger = logger;
21+
}
22+
23+
public void OnActionExecuted(ActionExecutedContext context) { }
24+
25+
public void OnActionExecuting(ActionExecutingContext context)
26+
{
27+
if (!(context.Controller is Controller controller))
28+
{
29+
return;
30+
}
31+
32+
if (!context.ActionArguments.ContainsKey("selfAssessmentId"))
33+
{
34+
return;
35+
}
36+
37+
var selfAssessmentId = int.Parse(context.ActionArguments["selfAssessmentId"].ToString()!);
38+
var delegateUserId = controller.User.GetUserIdKnownNotNull();
39+
40+
var selfAssessment = selfAssessmentService.GetSelfAssessmentForCandidateById(delegateUserId, selfAssessmentId);
41+
42+
if (selfAssessment == null)
43+
{
44+
logger.LogWarning(
45+
$"Attempt to access self assessment {selfAssessmentId} by user {delegateUserId}, but no such assessment found"
46+
);
47+
context.Result = new RedirectToActionResult("StatusCode", "LearningSolutions", new { code = 403 });
48+
return;
49+
}
50+
51+
var actionName = context.RouteData.Values["action"]?.ToString();
52+
if (actionName == "AgreeSelfAssessmentProcess" || actionName == "ProcessAgreed")
53+
{
54+
return;
55+
}
56+
57+
if (!selfAssessment.SelfAssessmentProcessAgreed && selfAssessment.IsSupervised)
58+
{
59+
logger.LogInformation(
60+
$"Redirecting user {delegateUserId} to agree process page for self assessment {selfAssessmentId}"
61+
);
62+
63+
context.Result = new RedirectToActionResult(
64+
"AgreeSelfAssessmentProcess",
65+
"LearningPortal",
66+
new { selfAssessmentId }
67+
);
68+
}
69+
}
70+
}
71+
}

DigitalLearningSolutions.Web/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ private static void RegisterWebServiceFilters(IServiceCollection services)
582582
services.AddScoped<VerifyUserHasVerifiedPrimaryEmail>();
583583
services.AddScoped<VerifyAdminAndDelegateUserCentre>();
584584
services.AddScoped<IsCentreAuthorizedSelfAssessment>();
585+
services.AddScoped<RequireProcessAgreementFilter>();
585586
services.AddScoped<VerifyAdminUserCanAccessSelfAssessment>();
586587
}
587588

DigitalLearningSolutions.Web/Views/LearningPortal/SelfAssessments/AgreeSelfAssessmentProcess.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<li>If you don’t fully meet the requirements, you can still document your progress and next steps for future assessment.</li>
3232
</ol>
3333

34-
<form method="post" asp-controller="LearningPortal" asp-action="ProcessAgreed">
34+
<form method="post" asp-controller="LearningPortal" asp-action="ProcessAgreed" asp-route-selfAssessmentId="@Model.SelfAssessmentID">
3535
<div class="nhsuk-checkboxes__item">
3636
<vc:single-checkbox asp-for="@nameof(Model.ActionConfirmed)"
3737
label=" I understand and agree to the self-assessment process described above."

0 commit comments

Comments
 (0)