Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Authorization in Admin pages #436

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading