forked from Sarah/Lieb-Website
Added user rights management
This commit is contained in:
parent
f00418d23c
commit
d472889ae1
9 changed files with 419 additions and 17 deletions
72
Lieb/Pages/User/RoleEdit.razor
Normal file
72
Lieb/Pages/User/RoleEdit.razor
Normal file
|
@ -0,0 +1,72 @@
|
|||
@page "/roleedit"
|
||||
@using Lieb.Data
|
||||
@using Lieb.Models
|
||||
@using Lieb.Models.GuildWars2
|
||||
@inject UserService UserService
|
||||
|
||||
|
||||
<h3>UserOverview</h3>
|
||||
|
||||
|
||||
<AuthorizeView Policy="@Constants.Roles.Admin">
|
||||
<Authorized>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>GW2 Account</th>
|
||||
<th>Banned Until</th>
|
||||
</tr>
|
||||
<h5>Roles</h5>
|
||||
@foreach(LiebRole role in _roles)
|
||||
{
|
||||
<tr>
|
||||
<td>@if(!role.IsSystemRole)
|
||||
{
|
||||
<button type=button @onclick="() => DeleteRoleClicked(role)">Create Role</button>
|
||||
}</td>
|
||||
<td>@role.RoleName</td>
|
||||
</tr>
|
||||
}
|
||||
</table>
|
||||
<p>
|
||||
<label>
|
||||
new Role:
|
||||
<input @bind="_newRoleName" />
|
||||
<button type=button @onclick="() => CreateRoleClicked()">Create Role</button>
|
||||
</label>
|
||||
</p>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
|
||||
|
||||
@code
|
||||
{
|
||||
private List<LiebRole> _roles;
|
||||
|
||||
private string _newRoleName = string.Empty;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_roles = UserService.GetLiebRoles();
|
||||
}
|
||||
|
||||
async Task CreateRoleClicked()
|
||||
{
|
||||
LiebRole role = new LiebRole()
|
||||
{
|
||||
RoleName = _newRoleName,
|
||||
IsSystemRole = false,
|
||||
Level = 0,
|
||||
LevelToAssign = Constants.RoleLevels.RaidLeadLevel
|
||||
};
|
||||
await UserService.AddRole(role);
|
||||
_roles = UserService.GetLiebRoles();
|
||||
}
|
||||
|
||||
async Task DeleteRoleClicked(LiebRole role)
|
||||
{
|
||||
await UserService.DeleteRole(role.LiebRoleId);
|
||||
_roles = UserService.GetLiebRoles();
|
||||
}
|
||||
}
|
118
Lieb/Pages/User/UserEdit.razor
Normal file
118
Lieb/Pages/User/UserEdit.razor
Normal file
|
@ -0,0 +1,118 @@
|
|||
@page "/useredit"
|
||||
@page "/useredit/{userId}"
|
||||
@using Lieb.Data
|
||||
@using Lieb.Models
|
||||
@using Lieb.Models.GuildWars2
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using System.Security.Claims
|
||||
@inject UserService UserService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||
@inject IJSRuntime JsRuntime
|
||||
|
||||
<h3>UserEdit</h3>
|
||||
|
||||
<EditForm Model="@_user" OnValidSubmit="@HandleValidSubmit">
|
||||
<DataAnnotationsValidator />
|
||||
<ValidationSummary />
|
||||
|
||||
|
||||
<AuthorizeView Policy="@Constants.Roles.Admin" Context="authorizationContext">
|
||||
<Authorized>
|
||||
<p>
|
||||
<label>
|
||||
Banned Until:
|
||||
<InputDate @bind-Value="_user.BannedUntil" />
|
||||
</label>
|
||||
</p>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
|
||||
<label>Equipped Builds</label>
|
||||
<table>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>Role Name</th>
|
||||
<th>IsSystemRole</th>
|
||||
</tr>
|
||||
@foreach (LiebRole role in _roles)
|
||||
{
|
||||
<tr>
|
||||
@{
|
||||
bool hasRole = _user.RoleAssignments.Where(a => a.LiebRoleId == role.LiebRoleId).Any();
|
||||
bool disabled = _editingUserRights < role.LevelToAssign;
|
||||
}
|
||||
<td><input type="checkbox" disabled="@disabled" checked="@hasRole" @onchange="args => RoleStatusChanged(role, args)" /></td>
|
||||
<td>@role.RoleName</td>
|
||||
@if(@role.IsSystemRole)
|
||||
{
|
||||
<td>True</td>
|
||||
}
|
||||
</tr>
|
||||
}
|
||||
</table>
|
||||
<br />
|
||||
<button type="submit">Submit</button>
|
||||
|
||||
</EditForm>
|
||||
|
||||
|
||||
@code {
|
||||
|
||||
[Parameter]
|
||||
public string userId { get; set; }
|
||||
|
||||
private LiebUser _user;
|
||||
private int _editingUserRights = 0;
|
||||
private List<LiebRole> _roles;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
|
||||
LiebUser editingUser = UserService.GetLiebUser(discordId);
|
||||
|
||||
foreach(RoleAssignment assignment in editingUser.RoleAssignments)
|
||||
{
|
||||
if(_editingUserRights < assignment.LiebRole.Level)
|
||||
{
|
||||
_editingUserRights = assignment.LiebRole.Level;
|
||||
}
|
||||
}
|
||||
|
||||
if(!string.IsNullOrEmpty(userId) && int.TryParse(userId, out int parsedId))
|
||||
{
|
||||
_user = UserService.GetLiebUser(parsedId);
|
||||
_roles = UserService.GetLiebRoles();
|
||||
}
|
||||
else
|
||||
{
|
||||
NavigationManager.NavigateTo("useroverview");
|
||||
}
|
||||
}
|
||||
|
||||
async Task RoleStatusChanged(LiebRole role, ChangeEventArgs args)
|
||||
{
|
||||
bool isChecked = bool.Parse(args.Value.ToString());
|
||||
RoleAssignment? assignment = _user.RoleAssignments.FirstOrDefault(a => a.LiebRoleId == role.LiebRoleId);
|
||||
if(isChecked && assignment == null)
|
||||
{
|
||||
RoleAssignment roleAssignment = new RoleAssignment()
|
||||
{
|
||||
LiebRoleId = role.LiebRoleId,
|
||||
LiebUserId = _user.LiebUserId
|
||||
};
|
||||
_user.RoleAssignments.Add(roleAssignment);
|
||||
}
|
||||
else if(!isChecked && assignment != null)
|
||||
{
|
||||
_user.RoleAssignments.Remove(assignment);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleValidSubmit()
|
||||
{
|
||||
await UserService.EditUserRoles(_user);
|
||||
NavigationManager.NavigateTo("useroverview");
|
||||
}
|
||||
}
|
87
Lieb/Pages/User/UserOverview.razor
Normal file
87
Lieb/Pages/User/UserOverview.razor
Normal file
|
@ -0,0 +1,87 @@
|
|||
@page "/useroverview"
|
||||
@using Lieb.Data
|
||||
@using Lieb.Models
|
||||
@using Lieb.Models.GuildWars2
|
||||
@inject UserService UserService
|
||||
|
||||
|
||||
<h3>UserOverview</h3>
|
||||
|
||||
|
||||
<AuthorizeView Policy="@Constants.Roles.Admin">
|
||||
<Authorized>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="roleedit">
|
||||
<span class="oi oi-plus" aria-hidden="true"></span> Edit Roles
|
||||
</NavLink>
|
||||
</div>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
|
||||
|
||||
<label>
|
||||
Banned Only:
|
||||
<input type="checkbox" @bind="_bannedOnly" />
|
||||
</label>
|
||||
<label>
|
||||
Search:
|
||||
<input @bind="_searchString" />
|
||||
</label>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>GW2 Account</th>
|
||||
<th>Banned Until</th>
|
||||
</tr>
|
||||
@{
|
||||
HashSet<LiebUser> foundUsers = new HashSet<LiebUser>();
|
||||
if(_bannedOnly)
|
||||
{
|
||||
foreach(LiebUser user in _users.Where(u => u.Name.Contains(_searchString) && u.BannedUntil > DateTime.Now))
|
||||
{
|
||||
foundUsers.Add(user);
|
||||
}
|
||||
foreach(LiebUser user in _users.Where(u => u.GuildWars2Accounts.Where(a => a.AccountName.Contains(_searchString)).Any() && u.BannedUntil > DateTime.Now))
|
||||
{
|
||||
foundUsers.Add(user);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach(LiebUser user in _users.Where(u => u.Name.Contains(_searchString)))
|
||||
{
|
||||
foundUsers.Add(user);
|
||||
}
|
||||
foreach(LiebUser user in _users.Where(u => u.GuildWars2Accounts.Where(a => a.AccountName.Contains(_searchString)).Any()))
|
||||
{
|
||||
foundUsers.Add(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@foreach (LiebUser user in foundUsers)
|
||||
{
|
||||
<tr>
|
||||
<td class="nav-item px-3">
|
||||
@{string navLink = $"useredit/{@user.LiebUserId}";}
|
||||
<NavLink class="nav-link" href="@navLink">@user.Name</NavLink>
|
||||
</td>
|
||||
<td>@foreach(var account in user.GuildWars2Accounts){<div>@account.AccountName </div> }</td>
|
||||
<td>@user.BannedUntil?.ToLongDateString()</td>
|
||||
</tr>
|
||||
}
|
||||
</table>
|
||||
|
||||
@code
|
||||
{
|
||||
private List<LiebUser> _users;
|
||||
|
||||
private bool _bannedOnly;
|
||||
private string _searchString = string.Empty;
|
||||
private string _newRoleName = string.Empty;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_users = UserService.GetLiebUsers();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue