Skip to content

Commit

Permalink
Improved Authorization in Admin pages (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
csharpfritz authored Apr 9, 2024
1 parent 8fdc436 commit b0669af
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
21 changes: 2 additions & 19 deletions src/TagzApp.Blazor/Components/Admin/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
@page "/Admin/Index"
@using Microsoft.AspNetCore.Authorization
@* @attribute [Authorize(Roles = "Admin")] *@
@attribute [Authorize(Roles = "Admin")]
@inject NavigationManager NavigationManager
@layout Admin.Shared.AdminLayout

<PageTitle>System Administration</PageTitle>

<h2>Hashtag to watch</h2>

<TagSearch />

@code {

[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

protected override Task OnInitializedAsync()
{
var user = HttpContext.User;
if (!user.IsInRole("Admin"))
{
NavigationManager.NavigateTo("/");
}
return base.OnInitializedAsync();
}

}
<TagSearch />
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/admin/modalcustomization"

@attribute [Authorize(Roles = RolesAndPolicies.Role.Admin)]
@layout AdminLayout
@inject IJSRuntime JSRuntime
@inject ModalConfiguration Config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/admin/uicustomization"
@using PSC.Blazor.Components.MarkdownEditor
@using PSC.Blazor.Components.MarkdownEditor.Enums
@attribute [Authorize(Roles = RolesAndPolicies.Role.Admin)]
@layout AdminLayout
@inject ApplicationConfiguration ApplicationConfiguration
@inject IJSRuntime JSRuntime
Expand Down
8 changes: 2 additions & 6 deletions src/TagzApp.Blazor/Components/Admin/Pages/Users.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
@using Gravatar
@using Microsoft.AspNetCore.Identity
@using Microsoft.EntityFrameworkCore
@attribute [Authorize(Roles = RolesAndPolicies.Role.Admin)]
@layout Admin.Shared.AdminLayout
@inject UserManager<TagzAppUser> UserManager
@inject NavigationManager NavigationManager
@inject ApplicationConfiguration AppConfig
@inject IJSRuntime Js
@rendermode InteractiveServer
@rendermode @(new InteractiveServerRenderMode(false))

<PageTitle>System Administration: User Management</PageTitle>

Expand Down Expand Up @@ -61,11 +62,6 @@
{

if (AppConfig.SingleUserMode) return;
var user = HttpContext.User;
if (!user.IsInRole("Admin"))
{
NavigationManager.NavigateTo("/");
}

UserList = await UserManager.Users.ToListAsync();
}
Expand Down
7 changes: 7 additions & 0 deletions src/TagzApp.Security/SecurityContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand All @@ -25,6 +26,12 @@ protected override void OnModelCreating(ModelBuilder builder)
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);

builder.Entity<IdentityRole>()
.HasData([
new IdentityRole { Id = "1", Name = RolesAndPolicies.Role.Admin, NormalizedName = "ADMIN" },
new IdentityRole { Id = "2", Name = RolesAndPolicies.Role.Moderator, NormalizedName = "MODERATOR" }
]);

builder.Entity<DataProtectionKey>().Property(d => d.Id);

}
Expand Down

0 comments on commit b0669af

Please sign in to comment.