diff --git a/LearningHub.Nhs.WebUI/Controllers/HomeController.cs b/LearningHub.Nhs.WebUI/Controllers/HomeController.cs index b187f3af4..9b91ed9ac 100644 --- a/LearningHub.Nhs.WebUI/Controllers/HomeController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/HomeController.cs @@ -6,6 +6,7 @@ namespace LearningHub.Nhs.WebUI.Controllers using System.Linq; using System.Net.Http; using System.Threading.Tasks; + using AspNetCoreRateLimit; using elfhHub.Nhs.Models.Common; using elfhHub.Nhs.Models.Enums; using LearningHub.Nhs.Models.Content; @@ -26,6 +27,7 @@ namespace LearningHub.Nhs.WebUI.Controllers using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.FeatureManagement; + using UAParser; using Settings = LearningHub.Nhs.WebUI.Configuration.Settings; /// @@ -206,43 +208,54 @@ public async Task Index(string myLearningDashboard = "my-in-progr { if (this.User?.Identity.IsAuthenticated == true) { - this.Settings.ConcurrentId = this.CurrentUserId; - this.Logger.LogInformation("User is authenticated: User is {fullname} and userId is: {lhuserid}", this.User.Identity.GetCurrentName(), this.User.Identity.GetCurrentUserId()); - if (this.User.IsInRole("Administrator") || this.User.IsInRole("BlueUser") || this.User.IsInRole("ReadOnly") || this.User.IsInRole("BasicUser")) + var userHistoryDetail = await this.userService.CheckUserHasAnActiveSessionAsync(this.CurrentUserId); + var uaParser = Parser.GetDefault(); + var clientInfo = uaParser.Parse(this.Request.Headers["User-Agent"]); + + if (userHistoryDetail.Items.Count == 0 || userHistoryDetail.Items[0].BrowserName == clientInfo.UA.Family) { - var learningTask = this.dashboardService.GetMyAccessLearningsAsync(myLearningDashboard, 1); - var resourcesTask = this.dashboardService.GetResourcesAsync(resourceDashboard, 1); - var cataloguesTask = this.dashboardService.GetCataloguesAsync(catalogueDashboard, 1); - - var enrolledCoursesTask = Task.FromResult(new List()); - var enableMoodle = Task.Run(() => this.featureManager.IsEnabledAsync(FeatureFlags.EnableMoodle)).Result; - this.ViewBag.EnableMoodle = enableMoodle; - this.ViewBag.ValidMoodleUser = this.CurrentMoodleUserId > 0; - if (enableMoodle && myLearningDashboard == "my-enrolled-courses") + this.Settings.ConcurrentId = this.CurrentUserId; + this.Logger.LogInformation("User is authenticated: User is {fullname} and userId is: {lhuserid}", this.User.Identity.GetCurrentName(), this.User.Identity.GetCurrentUserId()); + if (this.User.IsInRole("Administrator") || this.User.IsInRole("BlueUser") || this.User.IsInRole("ReadOnly") || this.User.IsInRole("BasicUser")) { - enrolledCoursesTask = this.dashboardService.GetEnrolledCoursesFromMoodleAsync(this.CurrentMoodleUserId, 1); + var learningTask = this.dashboardService.GetMyAccessLearningsAsync(myLearningDashboard, 1); + var resourcesTask = this.dashboardService.GetResourcesAsync(resourceDashboard, 1); + var cataloguesTask = this.dashboardService.GetCataloguesAsync(catalogueDashboard, 1); + + var enrolledCoursesTask = Task.FromResult(new List()); + var enableMoodle = Task.Run(() => this.featureManager.IsEnabledAsync(FeatureFlags.EnableMoodle)).Result; + this.ViewBag.EnableMoodle = enableMoodle; + this.ViewBag.ValidMoodleUser = this.CurrentMoodleUserId > 0; + if (enableMoodle && myLearningDashboard == "my-enrolled-courses") + { + enrolledCoursesTask = this.dashboardService.GetEnrolledCoursesFromMoodleAsync(this.CurrentMoodleUserId, 1); + } + + await Task.WhenAll(learningTask, resourcesTask, cataloguesTask); + + var model = new DashboardViewModel() + { + MyLearnings = await learningTask, + Resources = await resourcesTask, + Catalogues = await cataloguesTask, + EnrolledCourses = await enrolledCoursesTask, + }; + + if (!string.IsNullOrEmpty(this.Request.Query["preview"]) && Convert.ToBoolean(this.Request.Query["preview"])) + { + return this.View("LandingPage", await this.GetLandingPageContent(Convert.ToBoolean(this.Request.Query["preview"]))); + } + + return this.View("Dashboard", model); } - - await Task.WhenAll(learningTask, resourcesTask, cataloguesTask); - - var model = new DashboardViewModel() + else { - MyLearnings = await learningTask, - Resources = await resourcesTask, - Catalogues = await cataloguesTask, - EnrolledCourses = await enrolledCoursesTask, - }; - - if (!string.IsNullOrEmpty(this.Request.Query["preview"]) && Convert.ToBoolean(this.Request.Query["preview"])) - { - return this.View("LandingPage", await this.GetLandingPageContent(Convert.ToBoolean(this.Request.Query["preview"]))); + return this.RedirectToAction("InvalidUserAccount", "Account"); } - - return this.View("Dashboard", model); } else { - return this.RedirectToAction("InvalidUserAccount", "Account"); + return this.RedirectToAction("AlreadyAnActiveSession", "Account"); } } else diff --git a/LearningHub.Nhs.WebUI/Interfaces/IUserService.cs b/LearningHub.Nhs.WebUI/Interfaces/IUserService.cs index 1efd29ad5..19ee0a927 100644 --- a/LearningHub.Nhs.WebUI/Interfaces/IUserService.cs +++ b/LearningHub.Nhs.WebUI/Interfaces/IUserService.cs @@ -465,6 +465,13 @@ public interface IUserService /// providers. Task> GetProvidersByUserIdAsync(int userId); + /// + /// To Check User Has An ActiveSession. + /// + /// The userId. + /// A representing the result of the asynchronous operation. + Task> CheckUserHasAnActiveSessionAsync(int userId); + /// /// To get the Base64MD5HashDigest value. /// diff --git a/LearningHub.Nhs.WebUI/LearningHub.Nhs.WebUI.csproj b/LearningHub.Nhs.WebUI/LearningHub.Nhs.WebUI.csproj index 1b4ac83d1..dc2cf661d 100644 --- a/LearningHub.Nhs.WebUI/LearningHub.Nhs.WebUI.csproj +++ b/LearningHub.Nhs.WebUI/LearningHub.Nhs.WebUI.csproj @@ -143,6 +143,7 @@ + diff --git a/LearningHub.Nhs.WebUI/Services/UserService.cs b/LearningHub.Nhs.WebUI/Services/UserService.cs index 942360eae..b6e277368 100644 --- a/LearningHub.Nhs.WebUI/Services/UserService.cs +++ b/LearningHub.Nhs.WebUI/Services/UserService.cs @@ -1864,6 +1864,30 @@ public async Task> GetProvidersByUserIdAsync(int userId) return viewmodel; } + /// + public async Task> CheckUserHasAnActiveSessionAsync(int userId) + { + PagedResultSet userHistoryViewModel = new PagedResultSet(); + + var client = await this.userApiHttpClient.GetClientAsync(); + var request = $"UserHistory/CheckUserHasActiveSession/{userId}"; + var response = await client.GetAsync(request).ConfigureAwait(false); + + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsStringAsync(); + userHistoryViewModel = JsonConvert.DeserializeObject>(result); + } + else if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden) + { + throw new Exception("AccessDenied"); + } + + return userHistoryViewModel; + } + + + /// /// The base 64 m d 5 hash digest. /// diff --git a/LearningHub.Nhs.WebUI/Views/Account/AlreadyAnActiveSession.cshtml b/LearningHub.Nhs.WebUI/Views/Account/AlreadyAnActiveSession.cshtml new file mode 100644 index 000000000..1f5d3b6a7 --- /dev/null +++ b/LearningHub.Nhs.WebUI/Views/Account/AlreadyAnActiveSession.cshtml @@ -0,0 +1,15 @@ +@{ + ViewData["Title"] = "Already active session"; +} +
+
+
+
+

@ViewData["Title"]

+

You are already logged in from another browser. Please continue using the same browser or close the existing session and try again with a new one.

+

If you have any questions, please contact the support team.

+

@DateTimeOffset.Now.ToString("d MMMM yyyy HH:mm:ss")

+
+
+
+
\ No newline at end of file