Added user rights management
This commit is contained in:
parent
f00418d23c
commit
d472889ae1
9 changed files with 419 additions and 17 deletions
|
@ -17,5 +17,13 @@
|
|||
return typeof(Roles).GetFields().Select(f => f.GetValue(f)).Cast<string>().ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public static class RoleLevels
|
||||
{
|
||||
public const int UserLevel = 20;
|
||||
public const int RaidLeadLevel = 55;
|
||||
public const int GuildLeadLevel = 65;
|
||||
public const int AdminLevel = 80;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,22 +9,25 @@ namespace Lieb.Data
|
|||
{
|
||||
public static void Initialize(LiebContext context)
|
||||
{
|
||||
//add new Roles
|
||||
List<LiebRole> roles = new List<LiebRole>();
|
||||
foreach (string roleName in Constants.Roles.GetAllRoles())
|
||||
//add special Roles
|
||||
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.Admin) == null)
|
||||
{
|
||||
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == roleName) == null)
|
||||
{
|
||||
roles.Add(new LiebRole()
|
||||
{
|
||||
RoleName = roleName
|
||||
});
|
||||
}
|
||||
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.Admin, IsSystemRole = true, Level = Constants.RoleLevels.AdminLevel, LevelToAssign = Constants.RoleLevels.AdminLevel });
|
||||
}
|
||||
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.GuildLead) == null)
|
||||
{
|
||||
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.GuildLead, IsSystemRole = true, Level = Constants.RoleLevels.GuildLeadLevel, LevelToAssign = Constants.RoleLevels.AdminLevel });
|
||||
}
|
||||
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.RaidLead) == null)
|
||||
{
|
||||
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.RaidLead, IsSystemRole = true, Level = Constants.RoleLevels.RaidLeadLevel, LevelToAssign = Constants.RoleLevels.GuildLeadLevel });
|
||||
}
|
||||
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.User) == null)
|
||||
{
|
||||
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.User, IsSystemRole = true, Level = Constants.RoleLevels.UserLevel, LevelToAssign = Constants.RoleLevels.AdminLevel + 1 });
|
||||
}
|
||||
context.LiebRoles.AddRange(roles);
|
||||
context.SaveChanges();
|
||||
|
||||
|
||||
// Look for any LiebUsers.
|
||||
if (context.LiebUsers.Any())
|
||||
{
|
||||
|
|
|
@ -13,23 +13,48 @@ namespace Lieb.Data
|
|||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task<LiebUser> GetLiebUser(ulong discordId)
|
||||
public List<LiebUser> GetLiebUsers()
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.LiebUsers
|
||||
.Include(u => u.GuildWars2Accounts)
|
||||
.ThenInclude(a => a.EquippedBuilds)
|
||||
.ThenInclude(b => b.GuildWars2Build)
|
||||
.Include(u => u.RoleAssignments)
|
||||
.ThenInclude(r => r.LiebRole)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public LiebUser GetLiebUser(ulong discordId)
|
||||
{
|
||||
if (discordId > 0)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return await context.LiebUsers
|
||||
return context.LiebUsers
|
||||
.Include(u => u.GuildWars2Accounts)
|
||||
.ThenInclude(a => a.EquippedBuilds)
|
||||
.ThenInclude(b => b.GuildWars2Build)
|
||||
.Include(u => u.RoleAssignments)
|
||||
.ThenInclude(r => r.LiebRole)
|
||||
.FirstOrDefaultAsync(u => u.DiscordUserId == discordId);
|
||||
.FirstOrDefault(u => u.DiscordUserId == discordId);
|
||||
}
|
||||
else
|
||||
return new LiebUser();
|
||||
}
|
||||
|
||||
public LiebUser GetLiebUser(int userId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.LiebUsers
|
||||
.Include(u => u.GuildWars2Accounts)
|
||||
.ThenInclude(a => a.EquippedBuilds)
|
||||
.ThenInclude(b => b.GuildWars2Build)
|
||||
.Include(u => u.RoleAssignments)
|
||||
.ThenInclude(r => r.LiebRole)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(u => u.LiebUserId == userId);
|
||||
}
|
||||
|
||||
public LiebUser GetLiebUserSmall(ulong discordId)
|
||||
{
|
||||
if (discordId > 0)
|
||||
|
@ -43,6 +68,14 @@ namespace Lieb.Data
|
|||
return new LiebUser();
|
||||
}
|
||||
|
||||
public LiebUser GetLiebUserSmall(int userId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.LiebUsers
|
||||
.Include(u => u.GuildWars2Accounts)
|
||||
.FirstOrDefault(u => u.LiebUserId == userId);
|
||||
}
|
||||
|
||||
public async Task<int> GetLiebUserId(ulong discordId)
|
||||
{
|
||||
if (discordId > 0)
|
||||
|
@ -69,5 +102,73 @@ namespace Lieb.Data
|
|||
}
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task EditUserRoles(LiebUser user)
|
||||
{
|
||||
if (user != null)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
LiebUser? userToChange = await context.LiebUsers
|
||||
.Include(u => u.RoleAssignments)
|
||||
.FirstOrDefaultAsync(u => u.LiebUserId == user.LiebUserId);
|
||||
|
||||
if (userToChange == null)
|
||||
return;
|
||||
|
||||
userToChange.BannedUntil = user.BannedUntil;
|
||||
|
||||
List<RoleAssignment> toDelete = new List<RoleAssignment>();
|
||||
foreach (RoleAssignment assignment in userToChange.RoleAssignments)
|
||||
{
|
||||
RoleAssignment? newAssignment = user.RoleAssignments.FirstOrDefault(r => r.RoleAssignmentId == assignment.RoleAssignmentId);
|
||||
if (newAssignment == null)
|
||||
{
|
||||
toDelete.Add(assignment);
|
||||
}
|
||||
}
|
||||
foreach (RoleAssignment assignment in toDelete)
|
||||
{
|
||||
userToChange.RoleAssignments.Remove(assignment);
|
||||
context.RoleAssignments.Remove(assignment);
|
||||
}
|
||||
foreach (RoleAssignment assignment in user.RoleAssignments.Where(r => r.RoleAssignmentId == 0))
|
||||
{
|
||||
userToChange.RoleAssignments.Add(assignment);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<LiebRole> GetLiebRoles()
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.LiebRoles
|
||||
.Include(u => u.RoleAssignments)
|
||||
.ThenInclude(r => r.LiebUser)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task AddRole(LiebRole role)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == role.RoleName) == null)
|
||||
{
|
||||
context.LiebRoles.Add(role);
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteRole(int roleId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
LiebRole role = context.LiebRoles.FirstOrDefault(r => r.LiebRoleId == roleId);
|
||||
if (role != null)
|
||||
{
|
||||
context.LiebRoles.Remove(role);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,12 @@ namespace Lieb.Models
|
|||
[StringLength(40, ErrorMessage = "RoleName too long (40 character limit).")]
|
||||
public string RoleName { get; set; } = string.Empty;
|
||||
|
||||
public bool IsSystemRole { get; set; } = false;
|
||||
|
||||
public int Level { get; set; } = 20;
|
||||
|
||||
public int LevelToAssign { get; set; } = 30;
|
||||
|
||||
public ICollection<RoleAssignment> RoleAssignments { get; set; } = new List<RoleAssignment>();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,9 @@ namespace Lieb.Models
|
|||
public int LiebUserId { get; set; }
|
||||
public ulong DiscordUserId { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(40, ErrorMessage = "Name too long (40 character limit).")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[StringLength(60, ErrorMessage = "Pronouns too long (60 character limit).")]
|
||||
public string Pronouns { get; set; } = string.Empty;
|
||||
|
||||
|
|
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();
|
||||
}
|
||||
}
|
|
@ -38,6 +38,15 @@
|
|||
</div>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
<AuthorizeView Policy="@Constants.Roles.Admin">
|
||||
<Authorized>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="useroverview">
|
||||
<span class="oi oi-list-rich" aria-hidden="true"></span> User Overview
|
||||
</NavLink>
|
||||
</div>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue