Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<PackageReference Include="HtmlSanitizer" Version="6.0.453" />
<PackageReference Include="IdentityModel" Version="4.6.0" />
<PackageReference Include="LearningHub.Nhs.Caching" Version="2.0.2" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.19.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.36" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.36" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="elfhHub.Nhs.Models" Version="3.0.14" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Selenium.Axe" Version="4.0.19" />
Expand Down
9 changes: 9 additions & 0 deletions LearningHub.Nhs.WebUI/Controllers/MyAccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using GDS.MultiPageFormData;
using GDS.MultiPageFormData.Enums;
using LearningHub.Nhs.Caching;
using LearningHub.Nhs.Models.User;
using LearningHub.Nhs.WebUI.Configuration;
using LearningHub.Nhs.WebUI.Helpers;
using LearningHub.Nhs.WebUI.Interfaces;
Expand Down Expand Up @@ -97,6 +98,7 @@ public MyAccountController(
this.multiPageFormService = multiPageFormService;
this.cacheService = cacheService;
this.configuration = configuration;
this.moodleBridgeApiService = moodleBridgeApiService;
}

private string LoginWizardCacheKey => $"{this.CurrentUserId}:LoginWizard";
Expand Down Expand Up @@ -1497,12 +1499,18 @@ public async Task<IActionResult> ConfirmEmail(string token, string loctoken)
var validationResult = await this.userService.ValidateEmailChangeTokenAsync(token, loctoken, isUserRoleUpgrade);

EmailChangeValidateViewModel model = new EmailChangeValidateViewModel();
UpdateEmailaddressViewModel emailModel = new UpdateEmailaddressViewModel()
{
OldEmail = user.EmailAddress,
NewEmail = validationResult.Email,
};

if (validationResult.Valid)
{
if (isUserRoleUpgrade)
{
await this.userService.UpgradeAsFullAccessUserAsync(validationResult.UserId, validationResult.Email);
await this.moodleBridgeApiService.UpdateEmail(emailModel);
this.ViewBag.SuccessMessage = CommonValidationErrorMessages.EmailConfirmSucessMessage;
model.Token = token;
model.Loctoken = loctoken;
Expand All @@ -1511,6 +1519,7 @@ public async Task<IActionResult> ConfirmEmail(string token, string loctoken)
else
{
await this.userService.UpdateUserPrimaryEmailAsync(validationResult.Email);
await this.moodleBridgeApiService.UpdateEmail(emailModel);

// Add UserHistory entry
UserHistoryViewModel userHistory = new UserHistoryViewModel()
Expand Down
8 changes: 8 additions & 0 deletions LearningHub.Nhs.WebUI/Interfaces/IMoodleBridgeApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using LearningHub.Nhs.Models.Moodle;
using LearningHub.Nhs.Models.Moodle.API;
using LearningHub.Nhs.Models.User;
using LearningHub.Nhs.WebUI.Models;
using MoodleCourseCompletionModel = LearningHub.Nhs.Models.Moodle.API.MoodleCourseCompletionModel;

Expand All @@ -18,5 +19,12 @@ public interface IMoodleBridgeApiService
/// <param name="email">The email.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<MoodleInstanceUserIdsViewModel> GetUserInstancesByEmail(string email);

/// <summary>
/// UpdateEmail.
/// </summary>
/// <param name="updateEmailaddressViewModel">The updateEmailaddressViewModel.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<MoodleUpdateEmailResponseModel> UpdateEmail(UpdateEmailaddressViewModel updateEmailaddressViewModel);
}
}
2 changes: 1 addition & 1 deletion LearningHub.Nhs.WebUI/LearningHub.Nhs.WebUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
<PackageReference Include="IdentityModel" Version="4.6.0" />
<PackageReference Include="LearningHub.Nhs.Caching" Version="2.0.0" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="linqtotwitter" Version="6.9.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.19.0" />
<PackageReference Include="Microsoft.ApplicationInsights.EventCounterCollector" Version="2.21.0" />
Expand Down
42 changes: 42 additions & 0 deletions LearningHub.Nhs.WebUI/Services/MoodleBridgeApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
{
using System;
using System.Collections.Generic;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
using LearningHub.Nhs.Models.Moodle;
using LearningHub.Nhs.Models.Moodle.API;
using LearningHub.Nhs.Models.User;
using LearningHub.Nhs.WebUI.Configuration;
using LearningHub.Nhs.WebUI.Interfaces;
using LearningHub.Nhs.WebUI.Models;
Expand Down Expand Up @@ -63,5 +65,45 @@ public async Task<MoodleInstanceUserIdsViewModel> GetUserInstancesByEmail(string
return viewmodel;
}
}

/// <summary>
/// UpdateEmail.
/// </summary>
/// <param name="updateEmailaddressViewModel">The updateEmailaddressViewModel.</param>
/// <returns>email update status.</returns>
public async Task<MoodleUpdateEmailResponseModel> UpdateEmail(UpdateEmailaddressViewModel updateEmailaddressViewModel)
{
try
{
var client = await this.openApiHttpClient.GetClientAsync();

var requestUrl = "MoodleBridge/UpdateEmail";

var response = await client.PostAsJsonAsync(requestUrl, updateEmailaddressViewModel)
.ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var viewModel = await response.Content
.ReadFromJsonAsync<MoodleUpdateEmailResponseModel>();

return viewModel;
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}
else
{
var errorContent = await response.Content.ReadAsStringAsync();
throw new Exception($"API Error: {response.StatusCode}, Details: {errorContent}");
}
}
catch (Exception ex)
{
throw new Exception("Failed to update user email on moodle instances");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<PackageReference Include="Azure.Search.Documents" Version="11.7.0" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.15.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="IdentityModel" Version="4.6.0" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.20" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<PackageReference Include="elfhHub.Nhs.Models" Version="3.0.14" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LearningHub.Nhs.Models.Moodle;
using LearningHub.Nhs.Models.Moodle.API;
using LearningHub.Nhs.Models.MyLearning;
using LearningHub.Nhs.Models.User;

namespace LearningHub.Nhs.OpenApi.Services.Interface.Services
{
Expand All @@ -19,6 +20,13 @@ public interface IMoodleBridgeApiService
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<MoodleInstanceUserIdsViewModel> GetUserInstancesByEmail(string email);

/// <summary>
/// UpdateEmail.
/// </summary>
/// <param name="updateEmailaddressViewModel">TheupdateEmailaddressViewModel.</param>
/// <returns></returns>
Task<MoodleUpdateEmailResponseModel> UpdateEmail(UpdateEmailaddressViewModel updateEmailaddressViewModel);

/// <summary>
/// GetRecentEnrolledCoursesAsync.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.8.0" />
<PackageReference Include="LearningHub.Nhs.Caching" Version="2.0.0" />
<PackageReference Include="elfhHub.Nhs.Models" Version="3.0.14" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using static System.Net.WebRequestMethods;
using IdentityModel.Client;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using LearningHub.Nhs.Models.User;

/// <summary>
/// MoodleBridgeApiService.
Expand Down Expand Up @@ -84,7 +85,54 @@ public async Task<MoodleInstanceUserIdsViewModel> GetUserInstancesByEmail(string
catch (Exception ex)
{
this.logger.LogError(ex, "An error occurred while fetching user instances by email.");
throw;
throw;
}
}

/// <summary>
/// UpdateEmail.
/// </summary>
/// <param name="updateEmailaddressViewModel">The UpdateEmailaddressViewModel.</param>
/// <returns></returns>
public async Task<MoodleUpdateEmailResponseModel> UpdateEmail(UpdateEmailaddressViewModel updateEmailaddressViewModel)
{
try
{
var client = await this.moodleBridgeHttpClient.GetClient();

var requestUrl = "/api/v1/Users/update-email";

var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var response = await client.PostAsJsonAsync(requestUrl, updateEmailaddressViewModel)
.ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();

var viewModel = System.Text.Json.JsonSerializer.Deserialize<MoodleUpdateEmailResponseModel>(result, options);

return viewModel;
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}
else
{
var errorContent = await response.Content.ReadAsStringAsync();
throw new Exception($"API Error: {response.StatusCode}, Details: {errorContent}");
}
}
catch (Exception ex)
{
this.logger.LogError(ex, "An error occurred while updating email.");
throw;
}
}

Expand Down Expand Up @@ -168,7 +216,7 @@ public async Task<MoodleCompletionsApiResponseModel> GetRecentEnrolledCoursesAsy
catch (Exception ex)
{
this.logger.LogError(ex, "An error occurred while fetching user's recent learning activities ");
throw;
throw;
}
}

Expand Down Expand Up @@ -333,7 +381,7 @@ public async Task<MoodleCertificateResponseModel> GetUserCertificateAsync(string
{
queryParams.Add($"searchterm={Uri.EscapeDataString(filterText)}");
}

var queryString = queryParams.Any()
? "?" + string.Join("&", queryParams)
: string.Empty;
Expand Down Expand Up @@ -468,7 +516,7 @@ public async Task<List<CategoryResult>> GetAllMoodleCategoriesAsync()
catch (Exception ex)
{
this.logger.LogError(ex, "An error occurred while fetching user instances by email.");
throw;
throw;
}
}

Expand Down Expand Up @@ -553,7 +601,7 @@ public async Task<List<SubCategoryResult>> GetSubCategoryByCategoryIdAsync(strin
catch (Exception ex)
{
this.logger.LogError(ex, "An error occurred while fetching sub categories by category id.");
throw;
throw;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="elfhHub.Nhs.Models" Version="3.0.14" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="NBuilder" Version="6.1.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using LearningHub.Nhs.OpenApi.Services.Interface.Services;
using LearningHub.Nhs.Models.User;

/// <summary>
/// Moodle Bridge operations.
Expand Down Expand Up @@ -37,6 +38,19 @@ public async Task<IActionResult> GetUserInstancesByEmail(string email)
return this.Ok(moodleUser);
}

/// <summary>
/// The GetMoodle Instances UserIds.
/// </summary>
/// <param name="email">The LH user email.</param>
/// <returns>The <see cref="Task{IActionResult}"/>.</returns>
[HttpPost]
[Route("UpdateEmail")]
public async Task<IActionResult> UpdateEmail([FromBody] UpdateEmailaddressViewModel updateEmailaddressViewModel)
{
var emailUpdateResponse = await this.moodleBridgeApiService.UpdateEmail(updateEmailaddressViewModel);
return this.Ok(emailUpdateResponse);
}

/// <summary>
/// GetAllMoodleCategoriesAsync.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="8.0.1" />
<PackageReference Include="elfhHub.Nhs.Models" Version="3.0.14" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.NLogTarget" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.36" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.3" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.36" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.15" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.16" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading