Skip to content

Commit 0b8b5c8

Browse files
authored
Update email address automatically if not present in profile (#8266)
* Methods written, needs testing * Email address updates automatically if UserProfile.Email is null * Final pass, typo and comment deletion * Corrected Login typo * Feature functioning all typos corrected * Corrected addtl spacing, using statement, method logic * Removed Comments
1 parent b9de042 commit 0b8b5c8

5 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/dotnet/APIView/APIViewWeb/Controllers/AccountController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using APIViewWeb.Repositories;
66
using APIViewWeb.Models;
77

8-
98
namespace APIViewWeb.Controllers
109
{
1110
[AllowAnonymous]

src/dotnet/APIView/APIViewWeb/Managers/Interfaces/IUserProfileManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public interface IUserProfileManager
1313
public Task<UserProfileModel> TryGetUserProfileByNameAsync(string UserName);
1414
public Task UpdateUserPreferences(ClaimsPrincipal User, UserPreferenceModel preferences);
1515
public Task UpdateUserProfile(ClaimsPrincipal User, string email, HashSet<string> languages, UserPreferenceModel preferences);
16+
public Task SetUserEmailIfNullOrEmpty(ClaimsPrincipal User);
1617
}
1718
}

src/dotnet/APIView/APIViewWeb/Managers/UserProfileManager.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Security.Claims;
78
using System.Threading.Tasks;
9+
using APIView.Identity;
810
using APIViewWeb.Models;
911
using APIViewWeb.Repositories;
1012
using Microsoft.AspNetCore.Authorization;
11-
using Octokit;
1213

1314
namespace APIViewWeb.Managers
1415
{
@@ -62,5 +63,20 @@ public async Task UpdateUserProfile(ClaimsPrincipal User, string email, HashSet<
6263

6364
await _UserProfileRepository.UpsertUserProfileAsync(User, UserProfile);
6465
}
66+
67+
public async Task SetUserEmailIfNullOrEmpty(ClaimsPrincipal User)
68+
{
69+
var UserProfile = await TryGetUserProfileAsync(User);
70+
71+
if (string.IsNullOrWhiteSpace(UserProfile.Email))
72+
{
73+
var microsoftEmail = User.Claims.FirstOrDefault(c => c.Type == ClaimConstants.Email)?.Value;
74+
75+
if (!string.IsNullOrWhiteSpace(microsoftEmail))
76+
{
77+
await UpdateUserProfile(User, microsoftEmail, null, null);
78+
}
79+
}
80+
}
6581
}
6682
}

src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Index.cshtml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public async Task OnGetAsync(
5959
IEnumerable<string> search, IEnumerable<string> languages, IEnumerable<string> state,
6060
IEnumerable<string> status, int pageNo=1, int pageSize=_defaultPageSize, string sortField=_defaultSortField)
6161
{
62+
await _userProfileManager.SetUserEmailIfNullOrEmpty(User);
63+
6264
if (!search.Any() && !languages.Any() && !state.Any() && !status.Any())
6365
{
6466
UserPreferenceModel userPreference = await _preferenceCache.GetUserPreferences(User);

src/dotnet/APIView/APIViewWeb/Pages/Login.cshtml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.AspNetCore.Mvc.RazorPages;
44

@@ -17,4 +17,4 @@ public IActionResult OnGetAsync()
1717
return Page();
1818
}
1919
}
20-
}
20+
}

0 commit comments

Comments
 (0)