Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gzozo committed Sep 26, 2024
1 parent aac33d3 commit a3ba9d1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 24 deletions.
25 changes: 24 additions & 1 deletion grade-management-new/GradeManagement.Client/Layout/AppBar.razor
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject SubjectService SubjectService
@inject NavigationManager Navigation

<MudAppBar Elevation="5" Class="pb-1">
<MudIconButton Icon="@(DrawerOpened ? Icons.Material.Filled.Close : Icons.Material.Filled.Menu)" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())"/>
<MudText Typo="Typo.h3" Class="ml-3">AHK Review UI</MudText>
<MudSpacer/>
<SelectSubject @bind-Value="@SubjectService.CurrentSubject" PossibleValues="@SubjectService.Subjects"/>
<MudIconButton Icon="@Icons.Material.Filled.Add" Color="Color.Inherit" Edge="Edge.End" Size="Size.Large" Href="/subjects"/>

<!-- Profile Dropdown Menu -->
<MudMenu>
<ActivatorContent>
<MudAvatar Size="Size.Medium" ImageUrl="@UserProfilePictureUrl"/>
</ActivatorContent>
<ChildContent>
<MudMenuItem>
<MudText Typo="Typo.subtitle1">@UserName</MudText>
</MudMenuItem>
<MudMenuItem OnClick="@SignOut">
<MudText Typo="Typo.subtitle1">Sign Out</MudText>
</MudMenuItem>
</ChildContent>
</MudMenu>
</MudAppBar>

@code{

private string UserProfilePictureUrl { get; set; } = "https://via.placeholder.com/40"; // Default placeholder
private string UserName { get; set; } = "Loading...";
[Parameter] public Action DrawerToggle { get; set; }
[Parameter] public bool DrawerOpened { get; set; }

Expand All @@ -20,4 +38,9 @@
SubjectService.OnChange += StateHasChanged;
}

private void SignOut()
{
Navigation.NavigateToLogout("authentication/logout");
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@using GradeManagement.Shared.Authorization.Policies

<MudNavMenu>
<MudNavLink Href="/" Match="NavLinkMatch.All">Dashboard</MudNavLink>
<AuthorizeView Policy="@TeacherRequirement.PolicyName">
<Authorized>
<MudNavGroup Title="Data" Expanded="true">
<MudNavLink Href="/courses" Match="NavLinkMatch.Prefix">Courses</MudNavLink>
<MudNavLink Href="/semesters" Match="NavLinkMatch.Prefix">Semesters</MudNavLink>
Expand All @@ -13,6 +11,4 @@
<MudNavLink Href="/subjects" Match="NavLinkMatch.Prefix">Subjects</MudNavLink>
<MudNavLink Href="/students" Match="NavLinkMatch.Prefix">Students</MudNavLink>
</MudNavGroup>
</Authorized>
</AuthorizeView>
</MudNavMenu>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using GradeManagement.Client.Services;

using Microsoft.AspNetCore.Components.WebAssembly.Authentication;

namespace GradeManagement.Client.Network;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GradeManagement.Client.Services;

using Microsoft.AspNetCore.Components.WebAssembly.Authentication;

namespace GradeManagement.Client.Network;

public class SubjectHeaderHandler(SubjectService SubjectService) : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
Console.WriteLine("SubjectHeaderHandler");
var subjectId = SubjectService.CurrentSubject?.Id;
if (subjectId is not null)
{
request.Headers.Add("X-Subject-Id-Value", subjectId.ToString());
}

return await base.SendAsync(request, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@page "/subjects"
@using GradeManagement.Client.Components.NewDialogs
@using GradeManagement.Shared.Authorization.Policies
@layout AuthenticatedLayout
@inject IDialogService DialogService
@inject CrudSnackbarService SnackbarService
Expand All @@ -11,8 +10,6 @@

<MudText Typo="Typo.h4" Class="mt-4">Subjects</MudText>
<MudDivider Class="mb-4"/>
<AuthorizeView Policy="@TeacherRequirement.PolicyName">
<Authorized>
<LoadingComponent LongTask="@LoadingAction" @ref="loadingComponentRef">
<MudDataGrid Items="_subjects" T="Subject2" EditMode="DataGridEditMode.Form" ReadOnly="false" CommittedItemChanges="@CommittedItemChanges">
<Columns>
Expand All @@ -25,11 +22,6 @@
</MudDataGrid>
<MudFab Style="position:fixed; bottom:50px;right:50px" Color="@Color.Primary" StartIcon="@Icons.Material.Filled.Add" OnClick="@OpenDialog"/>
</LoadingComponent>
</Authorized>
<NotAuthorized>
<h1>Not authorized</h1>
</NotAuthorized>
</AuthorizeView>

@code {
private List<Subject2> _subjects = new();
Expand Down
27 changes: 18 additions & 9 deletions grade-management-new/GradeManagement.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using GradeManagement.Client.Network;
using GradeManagement.Client.Services;
using GradeManagement.Shared.Authorization.Handlers;
using GradeManagement.Shared.Authorization.Policies;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Web;
Expand All @@ -17,6 +15,7 @@ namespace GradeManagement.Client
public class Program
{
public const string ServerApi = "ServerAPI";
public const string SubjectApi = "SubjectAPI";

public static async Task Main(string[] args)
{
Expand All @@ -36,10 +35,10 @@ public static async Task Main(string[] args)
.Add("api://01834b53-87a0-4236-85d3-a999ecec0115/access_backend");
//Add scope for email
});
builder.Services.AddAuthorizationCore(options =>
options.AddPolicy(TeacherRequirement.PolicyName,
policy => policy.Requirements.Add(new TeacherRequirement())));
builder.Services.AddSingleton<IAuthorizationHandler, TeacherRequirementHandler>();
// builder.Services.AddAuthorizationCore(options =>
// options.AddPolicy(TeacherRequirement.PolicyName,
// policy => policy.Requirements.Add(new TeacherRequirement())));
// builder.Services.AddSingleton<IAuthorizationHandler, TeacherRequirementHandler>();

builder.Services.AddScoped<SubjectService>();
builder.Services.AddScoped<CrudSnackbarService>();
Expand All @@ -50,14 +49,24 @@ public static async Task Main(string[] args)
new ExceptionMessageHandler(sp.GetRequiredService<CrudSnackbarService>()));

// Registering HttpClient that uses our custom handler
builder.Services.AddHttpClient(ServerApi,
builder.Services.AddHttpClient(SubjectApi,
client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<ExceptionMessageHandler>()
.AddHttpMessageHandler<AuthorizationMessageHandler>();


builder.Services.AddScoped<SubjectClient>(sp =>
new SubjectClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi)));
new SubjectClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(SubjectApi)));

builder.Services.AddTransient(sp => new SubjectHeaderHandler(sp.GetRequiredService<SubjectService>()));

// Registering HttpClient that uses our custom handler
builder.Services.AddHttpClient(ServerApi,
client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<ExceptionMessageHandler>()
.AddHttpMessageHandler<AuthorizationMessageHandler>()
.AddHttpMessageHandler<SubjectHeaderHandler>();


builder.Services.AddScoped<CourseClient>(sp =>
new CourseClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi)));
builder.Services.AddScoped<SemesterClient>(sp =>
Expand Down

0 comments on commit a3ba9d1

Please sign in to comment.