Skip to content

Commit

Permalink
role-base sidebar profile url
Browse files Browse the repository at this point in the history
  • Loading branch information
hoseinzadehashraf committed Aug 4, 2023
1 parent 3496d88 commit 3fb96d8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 34 additions & 4 deletions Controllers/Modules/Components/Footer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Olive;
using Olive.Microservices.Hub;
using Olive.Mvc;
using vm = ViewModel;
using Microsoft.Extensions.Configuration;

namespace ViewComponents
{
Expand All @@ -15,10 +20,9 @@ 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 userRoles = user.Roles.Split(',');

var sidebarProfileUrl= Config.Get<string>("SidebarProfileUrl", "https://hub.%DOMAIN%/person/%EMAIL%")
.Replace("%EMAIL%",email)
.Replace("%ID%",user?.ID.ToString().OrEmpty());
var sidebarProfileUrl = GetSidebarProfileUrl(user?.ID.ToString().OrEmpty(), email, userRoles);

info = new vm.Footer()
{
Expand All @@ -31,6 +35,32 @@ public async Task<IViewComponentResult> InvokeAsync(vm.Footer info)

return View(info);
}

private string GetSidebarProfileUrl(string userId, string email, string[] userRoles)
{
var sidebarProfileUrl = "";

var roles = Config.GetSection("SidebarProfileUrl:Roles")
.GetChildren()
.ToDictionary(x => x.Key, x => x.Value);

if (roles != null)
sidebarProfileUrl = TryGetSidebarProfileUrlByRole(roles, userRoles);

if (sidebarProfileUrl.IsEmpty())
sidebarProfileUrl = Config.Get<string>("SidebarProfileUrl:Default", "https://hub.%DOMAIN%/person/%EMAIL%");

return sidebarProfileUrl.Replace("%EMAIL%", email).Replace("%ID%", userId);
}

private string TryGetSidebarProfileUrlByRole(Dictionary<string, string> roles, string[] userRoles)
{
foreach (var keyValue in roles)
if (userRoles.Any(a => a.Equals(keyValue.Key, false)))
return keyValue.Value;

return null;
}
}
}

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.9</Version>
<Version>1.5.10</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
Expand Down

0 comments on commit 3fb96d8

Please sign in to comment.