Skip to content

Commit

Permalink
Add admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed May 19, 2024
1 parent e86b6d8 commit fdfc4ae
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
70 changes: 70 additions & 0 deletions ReplayBrowser/Pages/Account/Admin.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@page "/account/admin"
@using System.Text.Json
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using ReplayBrowser.Data.Models.Account
@using ReplayBrowser.Services
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject AccountService AccountService
@attribute [Authorize]

<h3>Listing all accounts:</h3>
@if (_isNotAdmin)
{
<p>You are not an admin.</p>
} else if (_isLoading)
{
<p>Loading...</p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Username</th>
<th>Guid</th>
<th>IsAdmin</th>
<th>Settings</th>
<th>Logs</th>
</tr>
</thead>
<tbody>
@foreach (var account in _accounts)
{
<tr>
<td>@account.Id</td>
<td>@account.Username</td>
<td>@account.Guid</td>
<td>@account.IsAdmin</td>
<td>@JsonSerializer.Serialize(account.Settings)</td>
<td><a href="/account/[email protected]">Logs</a></td>
</tr>
}
</tbody>
</table>
}

@code
{
private List<Account> _accounts = [];
private bool _isNotAdmin = false;
private bool _isLoading = true;

protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = await AccountService.GetAccount(authState);

if (user == null || !user.IsAdmin)
{
_isNotAdmin = true;
_isLoading = false;
return;
}

_accounts = await AccountService.GetAllAccounts();
_accounts = _accounts.OrderBy(x => x.Id).ToList();
_isLoading = false;
}
}
27 changes: 16 additions & 11 deletions ReplayBrowser/Pages/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,36 @@

@code
{
private System.Security.Claims.ClaimsPrincipal User;
private System.Security.Claims.ClaimsPrincipal _user;

Check warning on line 13 in ReplayBrowser/Pages/Shared/MainLayout.razor

View workflow job for this annotation

GitHub Actions / deploy

Non-nullable field '_user' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
private Account? _account;

protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
User = authState.User;
if (User.Identity.IsAuthenticated)
_user = authState.User;
if (_user.Identity.IsAuthenticated)

Check warning on line 20 in ReplayBrowser/Pages/Shared/MainLayout.razor

View workflow job for this annotation

GitHub Actions / deploy

Dereference of a possibly null reference.
{
var guid = (Guid)AccountHelper.GetAccountGuid(authState)!;
var account = DbContext.Accounts.FirstOrDefault(a => a.Guid == guid);
_account = DbContext.Accounts.FirstOrDefault(a => a.Guid == guid);
var data = await Ss14ApiHelper.FetchPlayerDataFromGuid(guid);

if (account == null)
if (_account == null)
{
account = new Account()
_account = new Account()
{
Guid = guid,
Username = data.Username

Check warning on line 31 in ReplayBrowser/Pages/Shared/MainLayout.razor

View workflow job for this annotation

GitHub Actions / deploy

Dereference of a possibly null reference.
};

DbContext.Accounts.Add(account);
DbContext.Accounts.Add(_account);
await DbContext.SaveChangesAsync();

Log.Information("Created new account for {Guid} with username {Username}", guid, data.Username);
}

if (account.Username != data.Username)
if (_account.Username != data.Username)

Check warning on line 40 in ReplayBrowser/Pages/Shared/MainLayout.razor

View workflow job for this annotation

GitHub Actions / deploy

Dereference of a possibly null reference.
{
account.Username = data.Username;
_account.Username = data.Username;
await DbContext.SaveChangesAsync();
Log.Information("Updated username for account {Guid} to {Username}", guid, data.Username);
}
Expand All @@ -52,12 +53,16 @@

<div class="page">
<main class="container my-5">
@if (User.Identity.IsAuthenticated)
@if (_user.Identity.IsAuthenticated)

Check warning on line 56 in ReplayBrowser/Pages/Shared/MainLayout.razor

View workflow job for this annotation

GitHub Actions / deploy

Dereference of a possibly null reference.
{
<p>Hello <strong>@User.Claims.Single(c => c.Type == "name").Value!</strong></p>
<p>Hello <strong>@_user.Claims.Single(c => c.Type == "name").Value!</strong></p>

<a href="/account/logout" class="btn btn-primary">Logout</a>
<a class="btn btn-primary" onclick="window.location.href = '/account/manage';" style="margin-left: 10px">Manage</a>
@if(_account?.IsAdmin ?? false)
{
<a class="btn btn-primary" onclick="window.location.href = '/account/admin';" style="margin-left: 10px">Admin</a>
}
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions ReplayBrowser/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,17 @@ public async Task AddHistory(Account? callerAccount, HistoryEntry historyEntry)
await UpdateAccount(callerAccount);
}
}

/// <summary>
/// Returns all accounts, their settings and history.
/// </summary>
public async Task<List<Account>> GetAllAccounts()
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayDbContext>();
return await context.Accounts
.Include(a => a.Settings)
.Include(a => a.History)
.ToListAsync();
}
}

0 comments on commit fdfc4ae

Please sign in to comment.