Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Paymon Khamooshi committed Oct 16, 2024
1 parent 21fa07d commit 7aba522
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
8 changes: 7 additions & 1 deletion App_Start/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
Expand Down Expand Up @@ -33,7 +34,12 @@ public static Task LogOn(this PeopleService.UserInfo @this)
return loggingInfo.LogOn(remember: mobile);
}


public static async Task<PeopleService.UserInfo> LoadUser(this ClaimsPrincipal principal)
{
var email = Context.Current.User().GetEmail();
if (email.IsEmpty()) return null;
return await Context.Current.Database().FirstOrDefault<PeopleService.UserInfo>(x => x.Email == email);
}

private static void TryAddJwtToken(GenericLoginInfo loggingInfo, bool mobile)
{
Expand Down
7 changes: 4 additions & 3 deletions Controllers/Modules/Components/Footer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Olive;
using Olive.Mvc;
using vm = ViewModel;
using System;

namespace ViewComponents
{
Expand All @@ -20,10 +21,10 @@ public Footer(IThemeProvider themeProvider)

public async Task<IViewComponentResult> InvokeAsync(vm.Footer info)
{
var email = Context.Current.User().GetEmail();

var user = await Context.Current.Database().FirstOrDefault<PeopleService.UserInfo>(x => x.Email == email);
var user = await Context.Current.User().LoadUser();
var email = Context.Current.User().GetEmail();
if (user is null) return Content("User not recognised: " + email);

var userRoles = user.Roles.Split(',');

var sidebarProfileUrl = await _themeProvider.GetSidebarProfileUrl(userRoles, new Dictionary<string, string>
Expand Down
4 changes: 2 additions & 2 deletions Controllers/Modules/Components/UserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Olive;
using Olive.Mvc;
using vm = ViewModel;
using System;

namespace ViewComponents
{
Expand All @@ -20,9 +21,8 @@ public UserProfile(IThemeProvider themeProvider)

public async Task<IViewComponentResult> InvokeAsync(vm.UserProfile info)
{
var user = await Context.Current.User().LoadUser();
var email = Context.Current.User().GetEmail();

var user = await Context.Current.Database().FirstOrDefault<PeopleService.UserInfo>(x => x.Email == email);
if (user is null) return Content("User not recognised: " + email);

var userRoles = user.Roles.Split(',');
Expand Down
7 changes: 7 additions & 0 deletions Controllers/Pages/Unauthorized.Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using vm = ViewModel;
using Olive.Microservices.Hub;
using ViewModel;
using Microsoft.AspNetCore.Authentication;

namespace Controllers
{
Expand All @@ -40,6 +41,12 @@ public async Task<ActionResult> Index(Guid? feature)
if (item != null && User.Identity?.IsAuthenticated == true)
{
if (User.CanSee(item)) return Redirect(item.LoadUrl);

if (await Context.Current.User().LoadUser() is null)
{
await HttpContext.SignOutAsync();
return Content("Invalid user: " + HttpContext.User.GetEmail());
}
}

Response.StatusCode = User.Identity?.IsAuthenticated == true ? 403 : 401;
Expand Down
2 changes: 1 addition & 1 deletion Olive.Microservices.Hub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>Geeks Ltd</Authors>
<RepositoryUrl>https://github.com/Geeksltd/Olive.Microservices.Hub/tree/master/Olive.Microservices.Hub</RepositoryUrl>
<PackageIcon>icon.png</PackageIcon>
<Version>1.5.48</Version>
<Version>1.5.49</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<Nullable>warnings</Nullable>
Expand Down

0 comments on commit 7aba522

Please sign in to comment.