Skip to content

Commit

Permalink
feat: Setup Role based UI rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
isala404 committed Jan 2, 2022
1 parent d3b94de commit 7a69edc
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 40 deletions.
5 changes: 0 additions & 5 deletions WorkFlow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkFlow.Client", "WorkFlow
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkFlow.Shared", "WorkFlow\Shared\WorkFlow.Shared.csproj", "{3F17C59C-86DF-4895-87D3-173D609F40FA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9B378F77-0E25-4459-AE9B-8CE4C54B4DBB}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
1 change: 0 additions & 1 deletion WorkFlow/Client/Pages/Company/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@
}
_navService.NavigateToHome(true);
}

}
2 changes: 1 addition & 1 deletion WorkFlow/Client/Pages/Company/Manage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ else

protected override async Task OnInitializedAsync()
{
if(!_navService.IsAdmin(true)) return;
try
{
var companyId = _navService.GetCompanyByUri(Company).Id;
Expand Down Expand Up @@ -127,5 +128,4 @@ else
Toast?.Exception(e);
}
}

}
1 change: 1 addition & 0 deletions WorkFlow/Client/Pages/Forecast.razor
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

protected override void OnInitialized()
{
if(!_navService.IsAdmin(true)) return;
FetchNewData();
}

Expand Down
5 changes: 5 additions & 0 deletions WorkFlow/Client/Pages/Project/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@
_navService.NavigateToProjects(true);
}

protected override void OnInitialized()
{
if(!_navService.IsAdmin(true)) return;
}

}
9 changes: 6 additions & 3 deletions WorkFlow/Client/Pages/Project/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

<div class="container">
<h1 class="inline text-4xl mt-8">Projects</h1>
<a href="@Company/project/create">
<button class="p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-md rounded-lg focus:border-4 border-blue-300 float-right">Create Project</button>
</a>
@if (_navService.IsAdmin())
{
<a href="@Company/project/create">
<button class="p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-md rounded-lg focus:border-4 border-blue-300 float-right">Create Project</button>
</a>
}
<table class="table-auto border mt-10 xl:text-lg container">
<thead class="border bg-gray-50 ">
<tr class="border">
Expand Down
1 change: 1 addition & 0 deletions WorkFlow/Client/Pages/Project/Manage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ else

protected override async Task OnInitializedAsync()
{
if(!_navService.IsAdmin(true)) return;
try
{
_project = await _projectService.Get(Project);
Expand Down
15 changes: 9 additions & 6 deletions WorkFlow/Client/Pages/Project/View.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

<div>
<div class="flex flex-row-reverse mb-5">
<a href="@CompanyUri/project/@ProjectUri/ticket/create">
<button class="p-2 pl-5 pr-5 bg-blue-500 border-blue-300 text-gray-100 text-md rounded-lg focus:border-4 mr-14">Create Ticket</button>
</a>
<a href="@CompanyUri/project/@ProjectUri/manage">
<button class="p-2 pl-5 pr-5 bg-yellow-600 border-yellow-300 text-gray-100 text-md rounded-lg focus:border-4 mr-5">Manage Project</button>
</a>
@if (_navService.IsAdmin())
{
<a href="@CompanyUri/project/@ProjectUri/ticket/create">
<button class="p-2 pl-5 pr-5 bg-blue-500 border-blue-300 text-gray-100 text-md rounded-lg focus:border-4 mr-14">Create Ticket</button>
</a>
<a href="@CompanyUri/project/@ProjectUri/manage">
<button class="p-2 pl-5 pr-5 bg-yellow-600 border-yellow-300 text-gray-100 text-md rounded-lg focus:border-4 mr-5">Manage Project</button>
</a>
}
</div>

<div class="flex flex-wrap justify-around justify-items-stretch h-4/5 mb-5" style="min-height: 80vh;">
Expand Down
1 change: 1 addition & 0 deletions WorkFlow/Client/Pages/User/Report.razor
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ else

protected override async Task OnInitializedAsync()
{
if(!_navService.IsAdmin(true)) return;
try
{
UserProductivityDto = await _reportService.UserProductivity(UserId, _navService.GetCurrentCompany().Id);
Expand Down
5 changes: 0 additions & 5 deletions WorkFlow/Client/Pages/User/View.razor

This file was deleted.

4 changes: 4 additions & 0 deletions WorkFlow/Client/Services/INavService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
using WorkFlow.Client.Shared;
using WorkFlow.Shared.Dto;

namespace WorkFlow.Client.Services
{
public interface INavService
{
public event Action OnChange;
public Toast? Toast { get; set; }
public UserDto? CurrentUser { get; }
public bool Fetched { get; }
public Stack<CompanyDto> CompanyList { get; }
public CompanyDto GetCurrentCompany();
public bool IsAdmin(bool redirect = false);
public String TitleCase(string text);
public void SetCurrentCompany(string newCompanyUri, bool reload);
public string CurrentUrl();
Expand All @@ -28,6 +31,7 @@ public struct NavRoutes
public string Href { get; init; }
public string HrefWithCompany { get; set; }
public bool Selected { get; set; }
public bool AdminOnly { get; set; }
}
public class Notification : IEquatable<Notification>
{
Expand Down
18 changes: 18 additions & 0 deletions WorkFlow/Client/Services/NavService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Globalization;
using System.Linq;
using Microsoft.AspNetCore.Components.Authorization;
using WorkFlow.Client.Shared;
using WorkFlow.Shared.Dto;
using WorkFlow.Shared.Entities;
using WorkFlow.Shared.Interfaces;

namespace WorkFlow.Client.Services
Expand All @@ -17,6 +19,7 @@ public class NavService : INavService
private readonly AuthenticationStateProvider _authenticationStateProvider;
private NavigationManager NavigationManager { get; }
public event Action OnChange;
public Toast? Toast { get; set; }
public Stack<CompanyDto> CompanyList { get; init; } = new();
public bool Fetched { get; set; }
private CompanyDto _currentCompany;
Expand Down Expand Up @@ -66,6 +69,21 @@ public CompanyDto GetCurrentCompany()
return _currentCompany;
}

public bool IsAdmin(bool redirect = false)
{
var isAdmin = CurrentUser != null &&
(from userCompany in CurrentUser.UserCompany!
where userCompany.CompanyId == _currentCompany.Id
select userCompany.Role == UserRole.Admin).FirstOrDefault();
if (!isAdmin && redirect)
{
Toast?.AddNotification("Unauthorized", "To access that page Admin role is required", "yellow");
NavigateToHome(false);
}

return isAdmin;
}

public void SetCurrentCompany(string newCompanyUri, bool reload)
{
if (newCompanyUri == "create/company")
Expand Down
36 changes: 18 additions & 18 deletions WorkFlow/Client/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
@inject ICompany _companyService
@implements IDisposable

@if (!_navService.Fetched)
{
<Loader/>
}
else
{
if (_navService.CurrentUser != null)
<Toast>
@if (!_navService.Fetched)
{
<header>
<NavMenu/>
</header>
<div class="container mx-auto px-5">
<Breadcrumbs/>
<Toast>
@Body
</Toast>
</div>
<Loader/>
}
else
{
@Body
if (_navService.CurrentUser != null)
{
<header>
<NavMenu/>
</header>
<div class="container mx-auto px-5">
<Breadcrumbs/>
@Body
</div>
}
else
{
@Body
}
}
}
</Toast>

@code
{
Expand Down
3 changes: 2 additions & 1 deletion WorkFlow/Client/Shared/Navigation/NavOptions.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

@foreach (var option in Options)
{
if (option.AdminOnly && !_navService.IsAdmin()) return;
if (option.Selected)
{
<a class="text-black-900 block px-3 py-2 rounded-md text-base font-medium" href="@option.HrefWithCompany">
Expand All @@ -26,7 +27,7 @@
{
new() {Name = "Home", Href = "/"},
new() {Name = "Projects", Href = "/project"},
new() {Name = "Reports", Href = "/report"},
new() {Name = "Reports", Href = "/report", AdminOnly = true},
};

private void UpdateUi()
Expand Down
2 changes: 2 additions & 0 deletions WorkFlow/Client/Shared/Toast.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@using System.Diagnostics
@inject Blazored.LocalStorage.ILocalStorageService _localStorage
@inject INavService _navService

<CascadingValue Value=this>
<div class="absolute md:top-20 md:right-10">
Expand Down Expand Up @@ -34,6 +35,7 @@

protected override async Task OnInitializedAsync()
{
_navService.Toast = this;
if (!await _localStorage.ContainKeyAsync("notifications"))
{
await _localStorage.SetItemAsync("notifications", _notifications);
Expand Down

0 comments on commit 7a69edc

Please sign in to comment.