From bdf3c37bdd038ad194e654226409bba784a99bf4 Mon Sep 17 00:00:00 2001 From: davidreneuw Date: Thu, 13 Feb 2025 11:36:38 -0500 Subject: [PATCH] Verify that user in tenant --- .../UserManagement/IUserInformationService.cs | 1 + .../OfflineUserInformationService.cs | 5 +++++ .../UserManagement/UserInformationService.cs | 9 +++++++++ .../Datahub.Portal/Pages/Public/Login.razor | 18 +++++++++++++++--- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Portal/src/Datahub.Application/Services/UserManagement/IUserInformationService.cs b/Portal/src/Datahub.Application/Services/UserManagement/IUserInformationService.cs index 62bfd5068..81fecd2a4 100644 --- a/Portal/src/Datahub.Application/Services/UserManagement/IUserInformationService.cs +++ b/Portal/src/Datahub.Application/Services/UserManagement/IUserInformationService.cs @@ -47,6 +47,7 @@ public interface IUserInformationService Task UpdatePortalUserAsync(PortalUser updatedUser); public event EventHandler PortalUserUpdated; Task IsDailyLogin(); + Task CheckUserInTenant(string email); } public static class UserInformationServiceConstants diff --git a/Portal/src/Datahub.Infrastructure.Offline/OfflineUserInformationService.cs b/Portal/src/Datahub.Infrastructure.Offline/OfflineUserInformationService.cs index 2a4adf3e9..aa5daff6e 100644 --- a/Portal/src/Datahub.Infrastructure.Offline/OfflineUserInformationService.cs +++ b/Portal/src/Datahub.Infrastructure.Offline/OfflineUserInformationService.cs @@ -238,4 +238,9 @@ public Task IsDailyLogin() { return Task.FromResult(false); } + + public Task CheckUserInTenant(string email) + { + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/Portal/src/Datahub.Infrastructure/Services/UserManagement/UserInformationService.cs b/Portal/src/Datahub.Infrastructure/Services/UserManagement/UserInformationService.cs index f1ee846b3..f5dff8511 100644 --- a/Portal/src/Datahub.Infrastructure/Services/UserManagement/UserInformationService.cs +++ b/Portal/src/Datahub.Infrastructure/Services/UserManagement/UserInformationService.cs @@ -562,4 +562,13 @@ public async Task GetPortalUserWithAchievementsAsync(string userGrap return portalUser; } + + public async Task CheckUserInTenant(string email) + { + PrepareAuthenticatedClient(); + var users = await graphServiceClient.Users.GetAsync( + test => test.QueryParameters.Filter = $"mail eq '{email}'"); + if (users?.Value != null) return users.Value.Count > 0; + return false; + } } \ No newline at end of file diff --git a/Portal/src/Datahub.Portal/Pages/Public/Login.razor b/Portal/src/Datahub.Portal/Pages/Public/Login.razor index 86d6b87f6..bac5c94b0 100644 --- a/Portal/src/Datahub.Portal/Pages/Public/Login.razor +++ b/Portal/src/Datahub.Portal/Pages/Public/Login.razor @@ -60,22 +60,34 @@ _loggingIn = true; var loginHint = loginModel.Email; var existingUser = await _userInformationService.GetPortalUserByEmailAsync(loginHint); + + // If user is deleted, redirect to register page with deleted flag if (existingUser is { IsDeleted: true }) { _navigationManager.NavigateTo($"{Localizer["/register"]}?email={loginHint}&s=d"); return; } + // If user is locked, redirect to locked page if (existingUser is { IsLocked: true }) { _navigationManager.NavigateTo(Localizer["/locked"]); return; } - + + // If there is no portal user associated with the email, then: if (existingUser is null) { - _navigationManager.NavigateTo($"{Localizer["/register"]}?email={loginHint}&s=n"); - return; + // - first check if the user is registered in the tenant (that means they have registered but not logged in yet) + var registered = await _userInformationService.CheckUserInTenant(loginHint); + + // - if not registered, redirect to register page with new flag + if (!registered) + { + _navigationManager.NavigateTo($"{Localizer["/register"]}?email={loginHint}&s=n"); + return; + } + // - if they are registered, proceed with the normal login flow } if (string.IsNullOrWhiteSpace(redirectUri))