Authinator/Pages/Admin.cshtml
2023-06-01 06:14:24 +02:00

180 lines
5.1 KiB
Plaintext

@page
@using Authinator.Backend.Utils
@using Authinator.Backend.Database
@using Authinator.Backend.Database.Tables
@using System.Web
@using Microsoft.EntityFrameworkCore
@model AdminModel
@{
ViewData["Title"] = "Admin";
var db = new DatabaseContext();
async void Td(string? value, string placeholder = "unset") {
if (string.IsNullOrWhiteSpace(value)) {
<td>
<i>@placeholder</i>
</td>
}
else {
<td>@value</td>
}
}
}
<h1 class="display-6">Users</h1>
<table class="table table-striped table-hover table-bordered align-middle">
<thead>
<th>ID</th>
<th>Reference</th>
<th>Username</th>
<th>Email</th>
<th>Groups</th>
<th>Actions</th>
</thead>
<tbody>
@foreach (var user in db.Users.Include(p => p.Groups)) {
<tr>
<td>@user.Id</td>
@{
Td(user.Reference);
Td(user.Username);
Td(user.Email);
Td(user.Groups.Select(p => p.Name).DefaultIfEmpty().Aggregate((s, s1) => $"{s}, {s1}"), "none");
}
<td>
<div class="dropdown">
@if (user.IsSignupComplete) {
<button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Actions
</button>
}
else {
<button class="btn btn-sm btn-outline-success dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Actions
</button>
}
<ul class="dropdown-menu">
@if (!user.IsSignupComplete) {
<li>
<button class="dropdown-item" onclick="copyToClipboard(location.protocol + '//' + location.host + '/FinishRegistration/' + '@HttpUtility.UrlEncode(user.GetSignupToken())')"><i class="bi bi-check2-circle"></i>&nbsp; Copy signup link</button>
</li>
<li>
<a href="/Admin/User/@user.Id/Edit" class="dropdown-item"><i class="bi bi-pencil-fill"></i>&nbsp; Edit</a>
</li>
<li>
<button class="dropdown-item text-danger"><i class="bi bi-trash-fill"></i>&nbsp; Delete</button>
</li>
}
else {
<li>
<button class="dropdown-item" onclick="copyToClipboard(location.protocol + '//' + location.host + '/User/@user.Id/Reset/' + '@HttpUtility.UrlEncode(user.GetPwResetToken())')"><i class="bi bi-person-fill-lock"></i>&nbsp; Reset password</button>
</li>
<li>
<a href="/Admin/User/@user.Id/Edit" class="dropdown-item"><i class="bi bi-pencil-fill"></i>&nbsp; Edit</a>
</li>
<li>
<button class="dropdown-item text-warning"><i class="bi bi-exclamation-triangle-fill"></i>&nbsp; Disable</button>
</li>
<li>
<button class="dropdown-item text-danger"><i class="bi bi-trash-fill"></i>&nbsp; Delete</button>
</li>
}
</ul>
</div>
</td>
</tr>
}
</tbody>
</table>
<b>Add user</b><br/>
<form method="POST">
<input type="text" name="reference" placeholder="Reference" required/>
<button type="submit" name="action" value="add_user">Submit</button>
</form>
<br/>
<br/>
<h1 class="display-6">Groups</h1>
<table class="table table-striped table-hover table-bordered align-middle">
<thead>
<th>ID</th>
<th>Name</th>
<th>Actions</th>
</thead>
<tbody>
@foreach (var group in db.Groups) {
<tr>
<td>@group.Id</td>
<td>@group.Name</td>
<td>
<div class="dropdown">
<button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Actions
</button>
<ul class="dropdown-menu">
<li>
<a href="/Admin/Group/@group.Id/Edit" class="dropdown-item"><i class="bi bi-pencil-fill"></i>&nbsp; Edit</a>
</li>
<li>
<button class="dropdown-item text-danger"><i class="bi bi-trash-fill"></i>&nbsp; Delete</button>
</li>
</ul>
</div>
</td>
</tr>
}
</tbody>
</table>
<b>Add group</b><br/>
<form method="POST">
<input type="text" name="name" placeholder="Group name" required/>
<button type="submit" name="action" value="add_group">Submit</button>
</form>
<br/>
<br/>
<h1 class="display-6">ACLs</h1>
<table class="table table-striped table-hover table-bordered align-middle">
<thead>
<th>ID</th>
<th>Name</th>
<th>Target</th>
<th>Actions</th>
</thead>
<tbody>
@foreach (var acl in db.ACLs) {
<tr>
<td>@acl.Id</td>
<td>@acl.Name</td>
<td><span class="px-2 py-1 rounded bg-body-tertiary font-monospace">@acl.Target</span></td>
<td>
<div class="dropdown">
<button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Actions
</button>
<ul class="dropdown-menu">
<li>
<a href="/Admin/ACL/@acl.Id/Edit" class="dropdown-item"><i class="bi bi-pencil-fill"></i>&nbsp; Edit</a>
</li>
<li>
<button class="dropdown-item text-danger"><i class="bi bi-trash-fill"></i>&nbsp; Delete</button>
</li>
</ul>
</div>
</td>
</tr>
}
</tbody>
</table>
<b>Add ACL</b><br/>
<form method="POST">
<input type="text" name="name" placeholder="ACL name" required/>
<input type="text" name="target" placeholder="Target" required/>
<button type="submit" name="action" value="add_acl">Submit</button>
</form>