Raids and RaidTemplates are now only editable by their owner or moderators

reworked user rights
This commit is contained in:
t.ruspekhofer 2022-03-21 01:12:35 +01:00
parent cb683723b7
commit 2bf630f3a1
25 changed files with 258 additions and 270 deletions

View file

@ -2,28 +2,28 @@
{ {
public static class Constants public static class Constants
{ {
public const string ClaimType = "Role"; public const string ClaimType = "Role";
public static readonly int RaidEditPowerLevel = Roles.Moderator.PowerLevel;
public static class Roles public static class Roles
{ {
public const string User = "User"; public static readonly RoleConstant User = new RoleConstant("user", 20);
public const string RaidLead = "RaidLead"; public static readonly RoleConstant RaidLead = new RoleConstant("RaidLead", 40);
public const string GuildLead = "GuildLead"; public static readonly RoleConstant Moderator = new RoleConstant("Moderator", 70);
public const string Admin = "Admin"; public static readonly RoleConstant Admin = new RoleConstant("Admin", 100);
}
public static List<string> GetAllRoles() public class RoleConstant
{
public readonly string Name;
public readonly int PowerLevel;
public RoleConstant(string name, int powerLevel)
{ {
return typeof(Roles).GetFields().Select(f => f.GetValue(f)).Cast<string>().ToList(); Name = name;
PowerLevel = powerLevel;
} }
} }
public static class RoleLevels
{
public const int UserLevel = 20;
public const int RaidLeadLevel = 55;
public const int GuildLeadLevel = 65;
public const int AdminLevel = 80;
}
} }
} }

View file

@ -10,21 +10,21 @@ namespace Lieb.Data
public static void Initialize(LiebContext context) public static void Initialize(LiebContext context)
{ {
//add special Roles //add special Roles
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.Admin) == null) if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.Admin.Name) == null)
{ {
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.Admin, IsSystemRole = true, Level = Constants.RoleLevels.AdminLevel, LevelToAssign = Constants.RoleLevels.AdminLevel }); context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.Admin.Name, Type = RoleType.SystemRole, Level = Constants.Roles.Admin.PowerLevel, LevelToAssign = Constants.Roles.Admin.PowerLevel });
} }
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.GuildLead) == null) if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.Moderator.Name) == null)
{ {
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.GuildLead, IsSystemRole = true, Level = Constants.RoleLevels.GuildLeadLevel, LevelToAssign = Constants.RoleLevels.AdminLevel }); context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.Moderator.Name, Type = RoleType.SystemRole, Level = Constants.Roles.Moderator.PowerLevel, LevelToAssign = Constants.Roles.Admin.PowerLevel });
} }
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.RaidLead) == null) if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.RaidLead.Name) == null)
{ {
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.RaidLead, IsSystemRole = true, Level = Constants.RoleLevels.RaidLeadLevel, LevelToAssign = Constants.RoleLevels.GuildLeadLevel }); context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.RaidLead.Name, Type = RoleType.SystemRole, Level = Constants.Roles.RaidLead.PowerLevel, LevelToAssign = Constants.Roles.Moderator.PowerLevel });
} }
if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.User) == null) if (context.LiebRoles.FirstOrDefault(r => r.RoleName == Constants.Roles.User.Name) == null)
{ {
context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.User, IsSystemRole = true, Level = Constants.RoleLevels.UserLevel, LevelToAssign = Constants.RoleLevels.AdminLevel + 1 }); context.LiebRoles.Add(new LiebRole() { RoleName = Constants.Roles.User.Name, Type = RoleType.SystemRole, Level = Constants.Roles.User.PowerLevel, LevelToAssign = Constants.Roles.Admin.PowerLevel + 1 });
} }
context.SaveChanges(); context.SaveChanges();
@ -52,10 +52,10 @@ namespace Lieb.Data
context.SaveChanges(); context.SaveChanges();
int AdminRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.Admin).LiebRoleId; int AdminRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.Admin.Name).LiebRoleId;
int GuildLeadRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.GuildLead).LiebRoleId; int GuildLeadRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.Moderator.Name).LiebRoleId;
int RaidLeadRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.RaidLead).LiebRoleId; int RaidLeadRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.RaidLead.Name).LiebRoleId;
int UserRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.User).LiebRoleId; int UserRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.User.Name).LiebRoleId;
var assignments = new RoleAssignment[] var assignments = new RoleAssignment[]
{ {

View file

@ -34,22 +34,12 @@ namespace Lieb.Data
{ {
user.GuildWars2Accounts.Add(account); user.GuildWars2Accounts.Add(account);
} }
await context.SaveChangesAsync();
} }
else else
{ {
GuildWars2Account? accountToChange = context.GuildWars2Accounts context.Update(account);
.Include(a => a.EquippedBuilds)
.FirstOrDefault(a => a.GuildWars2AccountId == account.GuildWars2AccountId);
if (accountToChange != null)
{
accountToChange.AccountName = account.AccountName;
accountToChange.ApiKey = account.ApiKey;
await context.SaveChangesAsync();
}
} }
await context.SaveChangesAsync();
} }
} }

View file

@ -20,26 +20,12 @@ namespace Lieb.Data
if (build.GuildWars2BuildId == 0) if (build.GuildWars2BuildId == 0)
{ {
context.GuildWars2Builds.Add(build); context.GuildWars2Builds.Add(build);
await context.SaveChangesAsync();
} }
else else
{ {
GuildWars2Build? buildToChange = await context.GuildWars2Builds context.Update(build);
.FirstOrDefaultAsync(r => r.GuildWars2BuildId == build.GuildWars2BuildId);
if (buildToChange != null)
{
buildToChange.BuildName = build.BuildName;
buildToChange.Might = build.Might;
buildToChange.Quickness = build.Quickness;
buildToChange.Alacrity = build.Alacrity;
buildToChange.Heal = build.Heal;
buildToChange.Class = build.Class;
buildToChange.EliteSpecialization = build.EliteSpecialization;
await context.SaveChangesAsync();
}
} }
await context.SaveChangesAsync();
} }
} }

View file

@ -45,7 +45,7 @@ namespace Lieb.Data
.FirstOrDefault(r => r.RaidId == raidId); .FirstOrDefault(r => r.RaidId == raidId);
} }
public async Task AddOrEditRaid(Raid raid) public async Task AddOrEditRaid(Raid raid, List<PlannedRaidRole> rolesToDelete, List<RaidReminder> remindersToDelete)
{ {
if (raid != null) if (raid != null)
{ {
@ -57,54 +57,26 @@ namespace Lieb.Data
} }
else else
{ {
Raid? raidToChange = await context.Raids context.Update(raid);
.Include(r => r.Roles) context.PlannedRaidRoles.RemoveRange(rolesToDelete);
.Include(r => r.SignUpHistory) context.RaidReminders.RemoveRange(remindersToDelete);
.Include(r => r.Reminders)
.Include(r => r.SignUps) //move users back to "Random" role
.FirstOrDefaultAsync(r => r.RaidId == raid.RaidId); if (raid.RaidType != RaidType.Planned)
if (raidToChange != null)
{ {
raidToChange.Title = raid.Title; int randomRoleId = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole).PlannedRaidRoleId;
raidToChange.Description = raid.Description; foreach (RaidSignUp signUp in raid.SignUps)
raidToChange.StartTimeUTC = raid.StartTimeUTC;
raidToChange.EndTimeUTC = raid.EndTimeUTC;
raidToChange.Organizer = raid.Organizer;
raidToChange.Guild = raid.Guild;
raidToChange.VoiceChat = raid.VoiceChat;
raidToChange.RaidType = raid.RaidType;
raidToChange.RequiredRole = raid.RequiredRole;
raidToChange.FreeForAllTimeUTC = raid.FreeForAllTimeUTC;
raidToChange.DiscordMessageId = raid.DiscordMessageId;
raidToChange.DiscordChannelId = raid.DiscordChannelId;
raidToChange.DiscordGuildId = raid.DiscordGuildId;
if (raidToChange.RaidType == RaidType.Planned)
{ {
EditRoles(raidToChange, raid, context); if (randomRoleId == 0)
}
else
{
if(!raidToChange.Roles.Where(r => r.IsRandomSignUpRole).Any())
{ {
raidToChange.Roles.Add(raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole)); signUp.PlannedRaidRole = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
} }
int randomRoleId = raidToChange.Roles.FirstOrDefault(r => r.IsRandomSignUpRole).PlannedRaidRoleId; else
foreach (RaidSignUp signUp in raidToChange.SignUps)
{ {
if (randomRoleId == 0) signUp.PlannedRaidRoleId = randomRoleId;
{
signUp.PlannedRaidRole = raidToChange.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
}
else
{
signUp.PlannedRaidRoleId = randomRoleId;
}
} }
context.PlannedRaidRoles.RemoveRange(raidToChange.Roles.Where(r => !r.IsRandomSignUpRole));
} }
context.PlannedRaidRoles.RemoveRange(raid.Roles.Where(r => !r.IsRandomSignUpRole));
EditReminders(raidToChange, raid, context);
} }
await context.SaveChangesAsync(); await context.SaveChangesAsync();
@ -112,65 +84,7 @@ namespace Lieb.Data
} }
} }
private void EditRoles(Raid raidToEdit, Raid raid, LiebContext context) public async Task DeleteRaid(int raidId)
{
List<PlannedRaidRole> rolesToRemove = new List<PlannedRaidRole>();
foreach (PlannedRaidRole role in raidToEdit.Roles)
{
PlannedRaidRole? newRole = raid.Roles.FirstOrDefault(r => r.PlannedRaidRoleId == role.PlannedRaidRoleId);
if (newRole != null)
{
role.Spots = newRole.Spots;
role.Name = newRole.Name;
role.Description = newRole.Description;
}
else
{
rolesToRemove.Add(role);
}
}
foreach (PlannedRaidRole role in rolesToRemove)
{
raidToEdit.Roles.Remove(role);
context.PlannedRaidRoles.Remove(role);
}
foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0))
{
raidToEdit.Roles.Add(role);
}
}
private void EditReminders(Raid raidToEdit, Raid raid, LiebContext context)
{
List<RaidReminder> reminderToRemove = new List<RaidReminder>();
foreach (RaidReminder reminder in raidToEdit.Reminders)
{
RaidReminder? newReminder = raid.Reminders.FirstOrDefault(r => r.RaidReminderId == reminder.RaidReminderId);
if (newReminder != null)
{
reminder.Type = newReminder.Type;
reminder.Message = newReminder.Message;
reminder.HoursBeforeRaid = newReminder.HoursBeforeRaid;
reminder.ChannelId = newReminder.ChannelId;
reminder.Sent = newReminder.Sent;
}
else
{
reminderToRemove.Add(reminder);
}
}
foreach (RaidReminder reminder in reminderToRemove)
{
raidToEdit.Reminders.Remove(reminder);
context.RaidReminders.Remove(reminder);
}
foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0))
{
raidToEdit.Roles.Add(role);
}
}
public async Task DeleteRaid(int raidId)
{ {
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
Raid raid = GetRaid(raidId); Raid raid = GetRaid(raidId);
@ -310,14 +224,14 @@ namespace Lieb.Data
if (raid.RaidType == RaidType.Planned) if (raid.RaidType == RaidType.Planned)
{ {
//if (raid.MoveFlexAllowed) if (raid.MoveFlexUsers)
{ {
return IsRoleSignUpAllowed(raid, liebUserId, plannedRoleId, signUpType, moveFlexUser, new List<int>()).Result; return IsRoleSignUpAllowed(raid, liebUserId, plannedRoleId, signUpType, moveFlexUser, new List<int>()).Result;
} }
//else else
//{ {
// return IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType); return IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType);
//} }
} }
else else
{ {

View file

@ -31,7 +31,7 @@ namespace Lieb.Data
.FirstOrDefault(t => t.RaidTemplateId == raidTemplateId); .FirstOrDefault(t => t.RaidTemplateId == raidTemplateId);
} }
public async Task AddOrEditTemplate(RaidTemplate template) public async Task AddOrEditTemplate(RaidTemplate template, List<PlannedRaidRole> rolesToDelete, List<RaidReminder> remindersToDelete)
{ {
if (template != null) if (template != null)
{ {
@ -39,39 +39,14 @@ namespace Lieb.Data
if (template.RaidTemplateId == 0) if (template.RaidTemplateId == 0)
{ {
context.RaidTemplates.Add(template); context.RaidTemplates.Add(template);
await context.SaveChangesAsync();
} }
else else
{ {
RaidTemplate raidToChange = await context.RaidTemplates context.Update(template);
.Include(r => r.Roles) context.PlannedRaidRoles.RemoveRange(rolesToDelete);
.Include(r => r.Reminders) context.RaidReminders.RemoveRange(remindersToDelete);
.FirstOrDefaultAsync(r => r.RaidTemplateId == template.RaidTemplateId);
raidToChange.Title = template.Title;
raidToChange.Description = template.Description;
raidToChange.Organizer = template.Organizer;
raidToChange.Guild = template.Guild;
raidToChange.VoiceChat = template.VoiceChat;
raidToChange.RaidType = template.RaidType;
raidToChange.RequiredRole = template.RequiredRole;
raidToChange.DiscordChannelId = template.DiscordChannelId;
raidToChange.DiscordGuildId = template.DiscordGuildId;
raidToChange.StartTime = template.StartTime;
raidToChange.EndTime = template.EndTime;
raidToChange.FreeForAllTime = template.FreeForAllTime;
raidToChange.TimeZone = template.TimeZone;
raidToChange.Interval = template.Interval;
raidToChange.CreateDaysBefore = template.CreateDaysBefore;
context.PlannedRaidRoles.RemoveRange(raidToChange.Roles);
context.RaidReminders.RemoveRange(raidToChange.Reminders);
raidToChange.Roles.Clear();
raidToChange.Reminders.Clear();
raidToChange.Roles = template.Roles;
raidToChange.Reminders = template.Reminders;
await context.SaveChangesAsync();
} }
await context.SaveChangesAsync();
} }
} }

View file

@ -55,7 +55,7 @@ namespace Lieb.Data
.FirstOrDefault(u => u.LiebUserId == userId); .FirstOrDefault(u => u.LiebUserId == userId);
} }
public LiebUser GetLiebUserSmall(ulong discordId) public LiebUser GetLiebUserGW2AccountOnly(ulong discordId)
{ {
if (discordId > 0) if (discordId > 0)
{ {
@ -69,7 +69,7 @@ namespace Lieb.Data
return new LiebUser(); return new LiebUser();
} }
public LiebUser GetLiebUserSmall(int userId) public LiebUser GetLiebGW2AccountOnly(int userId)
{ {
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
return context.LiebUsers return context.LiebUsers
@ -145,6 +145,36 @@ namespace Lieb.Data
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
public int GetPowerLevel(int userId)
{
using var context = _contextFactory.CreateDbContext();
LiebUser? user = context.LiebUsers
.Include(u => u.RoleAssignments)
.ThenInclude(r => r.LiebRole)
.AsNoTracking()
.FirstOrDefault(u => u.LiebUserId == userId);
if (user != null)
{
return user.RoleAssignments.Max(a => a.LiebRole.Level);
}
return 0;
}
public int GetPowerLevel(ulong discordId)
{
using var context = _contextFactory.CreateDbContext();
LiebUser? user = context.LiebUsers
.Include(u => u.RoleAssignments)
.ThenInclude(r => r.LiebRole)
.AsNoTracking()
.FirstOrDefault(u => u.DiscordUserId == discordId);
if (user != null)
{
return user.RoleAssignments.Max(a => a.LiebRole.Level);
}
return 0;
}
public List<LiebRole> GetLiebRoles() public List<LiebRole> GetLiebRoles()
{ {
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();

View file

@ -61,7 +61,7 @@ namespace Discord.OAuth2
} }
else else
{ {
LiebRole standardRole = await _LiebDbcontext.LiebRoles.FirstOrDefaultAsync(m => m.RoleName == Constants.Roles.User); LiebRole standardRole = await _LiebDbcontext.LiebRoles.FirstOrDefaultAsync(m => m.RoleName == Constants.Roles.User.Name);
string userName = context.Identity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; string userName = context.Identity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
LiebUser newUser = new LiebUser() LiebUser newUser = new LiebUser()
{ {
@ -78,7 +78,7 @@ namespace Discord.OAuth2
_LiebDbcontext.RoleAssignments.Add(roleAssignment); _LiebDbcontext.RoleAssignments.Add(roleAssignment);
await _LiebDbcontext.SaveChangesAsync(); await _LiebDbcontext.SaveChangesAsync();
context.Identity.AddClaim(new Claim(Constants.ClaimType, Constants.Roles.User)); context.Identity.AddClaim(new Claim(Constants.ClaimType, Constants.Roles.User.Name));
} }
return context; return context;
} }

View file

@ -29,6 +29,10 @@ namespace Lieb.Models.GuildWars2.Raid
public string RequiredRole { get; set; } = String.Empty; public string RequiredRole { get; set; } = String.Empty;
public bool MoveFlexUsers { get; set; } = true;
public int RaidOwnerId { get; set; }
//role name, number of spots //role name, number of spots
public ICollection<PlannedRaidRole> Roles { get; set; } = new HashSet<PlannedRaidRole>(); public ICollection<PlannedRaidRole> Roles { get; set; } = new HashSet<PlannedRaidRole>();

View file

@ -2,6 +2,13 @@
namespace Lieb.Models namespace Lieb.Models
{ {
public enum RoleType
{
GuildRole = 1,
SystemRole = 2,
UserDefinedRole = 3
}
public class LiebRole public class LiebRole
{ {
public int LiebRoleId { get; set; } public int LiebRoleId { get; set; }
@ -10,7 +17,7 @@ namespace Lieb.Models
[StringLength(40, ErrorMessage = "RoleName too long (40 character limit).")] [StringLength(40, ErrorMessage = "RoleName too long (40 character limit).")]
public string RoleName { get; set; } = string.Empty; public string RoleName { get; set; } = string.Empty;
public bool IsSystemRole { get; set; } = false; public RoleType Type { get; set; }
public int Level { get; set; } = 20; public int Level { get; set; } = 20;

View file

@ -10,7 +10,7 @@
<h3>BuildEdit</h3> <h3>BuildEdit</h3>
<AuthorizeView Policy="@Constants.Roles.Admin" Context="authorizationContext"> <AuthorizeView Policy="@Constants.Roles.Admin.Name" Context="authorizationContext">
<EditForm Model="@_build" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@_build" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator /> <DataAnnotationsValidator />

View file

@ -7,7 +7,7 @@
<h3>BuildOverview</h3> <h3>BuildOverview</h3>
<AuthorizeView Policy="@Constants.Roles.Admin"> <AuthorizeView Policy="@Constants.Roles.Admin.Name">
<Authorized> <Authorized>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="buildedit"> <NavLink class="nav-link" href="buildedit">

View file

@ -75,12 +75,14 @@
<div> <div>
<AuthorizeView> <AuthorizeView>
<button class="controlButton raidButton" @onclick="() => SignOffClicked()">Sign Off</button> <button class="controlButton raidButton" @onclick="() => SignOffClicked()">Sign Off</button>
</AuthorizeView>
<AuthorizeView Policy="@Constants.Roles.RaidLead"> @if (_raid.RaidOwnerId == _user.LiebUserId || _user.RoleAssignments.Max(a => a.LiebRole.Level) >= Constants.RaidEditPowerLevel)
<button class="controlButton raidButton" @onclick="() => EditClicked()">Edit</button>
@if (_raid.RaidType != RaidType.Planned)
{ {
<button class="controlButton raidButton" type=button @onclick="() => RandomizeClicked()">Randomize</button> <button class="controlButton raidButton" @onclick="() => EditClicked()">Edit</button>
@if (_raid.RaidType != RaidType.Planned)
{
<button class="controlButton raidButton" type=button @onclick="() => RandomizeClicked()">Randomize</button>
}
} }
</AuthorizeView> </AuthorizeView>
</div> </div>

View file

@ -4,16 +4,18 @@
@using Lieb.Models @using Lieb.Models
@using Lieb.Models.GuildWars2.Raid @using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using System.Security.Claims
@inject RaidService RaidService @inject RaidService RaidService
@inject UserService UserService @inject UserService UserService
@inject TimeZoneService TimeZoneService @inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IJSRuntime JsRuntime @inject IJSRuntime JsRuntime
<h3>CreateRaid</h3> <h3>CreateRaid</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead" Context="authorizationContext"> <AuthorizeView Policy="@Constants.Roles.RaidLead.Name" Context="authorizationContext">
<EditForm Model="@_raid" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@_raid" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
@{ @{
@ -70,7 +72,7 @@
<option value="">Not Locked</option> <option value="">Not Locked</option>
@foreach(LiebRole role in UserService.GetLiebRoles()) @foreach(LiebRole role in UserService.GetLiebRoles())
{ {
if (!role.IsSystemRole) if (role.Type != RoleType.SystemRole)
{ {
<option value="@role.RoleName">@role.RoleName</option> <option value="@role.RoleName">@role.RoleName</option>
} }
@ -151,11 +153,11 @@
</AuthorizeView> </AuthorizeView>
@code { @code {
[Parameter] [Parameter]
public string raidId { get; set; } public string raidId { get; set; }
public Raid _raid; public Raid _raid;
private LiebUser _user;
private string _errorMessage = string.Empty; private string _errorMessage = string.Empty;
@ -165,18 +167,40 @@
private DateTimeOffset _freeForAllDate = DateTime.Now.Date; private DateTimeOffset _freeForAllDate = DateTime.Now.Date;
private DateTimeOffset _freeForAllTime; private DateTimeOffset _freeForAllTime;
private List<PlannedRaidRole> _rolesToDelete = new List<PlannedRaidRole>();
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState != null)
{
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUser(discordId);
}
if(_user == null)
{
NavigationManager.NavigateTo("");
}
if(!string.IsNullOrEmpty(raidId) && int.TryParse(raidId, out int parsedId)) if(!string.IsNullOrEmpty(raidId) && int.TryParse(raidId, out int parsedId))
{ {
_raid = RaidService.GetRaid(parsedId); _raid = RaidService.GetRaid(parsedId);
_startTime = await TimeZoneService.GetLocalDateTime(_raid.StartTimeUTC);
_endTime = await TimeZoneService.GetLocalDateTime(_raid.EndTimeUTC); if (_raid != null && (_raid.RaidOwnerId == _user.LiebUserId
_raidDate = _startTime.Date; || _user.RoleAssignments.Max(a => a.LiebRole.Level) >= Constants.RaidEditPowerLevel))
_freeForAllTime = await TimeZoneService.GetLocalDateTime(_raid.FreeForAllTimeUTC); {
_freeForAllDate = _freeForAllTime.Date; _startTime = await TimeZoneService.GetLocalDateTime(_raid.StartTimeUTC);
_endTime = await TimeZoneService.GetLocalDateTime(_raid.EndTimeUTC);
_raidDate = _startTime.Date;
_freeForAllTime = await TimeZoneService.GetLocalDateTime(_raid.FreeForAllTimeUTC);
_freeForAllDate = _freeForAllTime.Date;
}
else
{
_raid = new Raid();
}
} }
else else
{ {
@ -192,6 +216,10 @@
async Task DeleteRoleClicked(PlannedRaidRole role) async Task DeleteRoleClicked(PlannedRaidRole role)
{ {
if(role.PlannedRaidRoleId != 0)
{
_rolesToDelete.Add(role);
}
_raid.Roles.Remove(role); _raid.Roles.Remove(role);
} }
@ -209,7 +237,7 @@
{ {
if(_raid.RaidType != RaidType.Planned) if(_raid.RaidType != RaidType.Planned)
{ {
PlannedRaidRole role = _raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole); PlannedRaidRole? role = _raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
int randomRoleId = role != null ? role.PlannedRaidRoleId : 0; int randomRoleId = role != null ? role.PlannedRaidRoleId : 0;
_raid.Roles.Clear(); _raid.Roles.Clear();
_raid.Roles.Add(new PlannedRaidRole() _raid.Roles.Add(new PlannedRaidRole()
@ -228,8 +256,6 @@
return; return;
} }
//_raid.TimeZone = await TimeZoneService.GetUserTimeZone();
_raid.StartTimeUTC = await TimeZoneService.GetUTCDateTime(_raidDate.Date + _startTime.TimeOfDay); _raid.StartTimeUTC = await TimeZoneService.GetUTCDateTime(_raidDate.Date + _startTime.TimeOfDay);
if(_startTime.TimeOfDay > _endTime.TimeOfDay) if(_startTime.TimeOfDay > _endTime.TimeOfDay)
{ {
@ -241,7 +267,12 @@
} }
_raid.FreeForAllTimeUTC = await TimeZoneService.GetUTCDateTime(_freeForAllDate.Date + _freeForAllTime.TimeOfDay); _raid.FreeForAllTimeUTC = await TimeZoneService.GetUTCDateTime(_freeForAllDate.Date + _freeForAllTime.TimeOfDay);
await RaidService.AddOrEditRaid(_raid); if (_raid.RaidOwnerId == 0)
{
_raid.RaidOwnerId = _user.LiebUserId;
}
await RaidService.AddOrEditRaid(_raid, _rolesToDelete, new List<RaidReminder>());
NavigationManager.NavigateTo("raidoverview"); NavigationManager.NavigateTo("raidoverview");
} }
} }

View file

@ -11,7 +11,7 @@
<h3>RaidOverview</h3> <h3>RaidOverview</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead"> <AuthorizeView Policy="@Constants.Roles.RaidLead.Name">
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="raidedit"> <NavLink class="nav-link" href="raidedit">
<span class="oi oi-plus" aria-hidden="true"></span> Add Raid <span class="oi oi-plus" aria-hidden="true"></span> Add Raid
@ -31,7 +31,7 @@
<option value="No Group">No Group</option> <option value="No Group">No Group</option>
@foreach(LiebRole role in UserService.GetLiebRoles()) @foreach(LiebRole role in UserService.GetLiebRoles())
{ {
if (!role.IsSystemRole) if (role.Type != RoleType.SystemRole)
{ {
<option value="@role.RoleName">@role.RoleName</option> <option value="@role.RoleName">@role.RoleName</option>
} }
@ -64,7 +64,7 @@
if (authState.User.Identity.IsAuthenticated) if (authState.User.Identity.IsAuthenticated)
{ {
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUserSmall(discordId); _user = UserService.GetLiebUser(discordId);
} }
_raids = RaidService.GetRaids(); _raids = RaidService.GetRaids();

View file

@ -5,6 +5,7 @@
@inject UserService UserService @inject UserService UserService
@inject RaidService RaidService @inject RaidService RaidService
@inject TimeZoneService TimeZoneService @inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager
@inject RaidRandomizerService RaidRandomizerService @inject RaidRandomizerService RaidRandomizerService
<body> <body>
@ -58,13 +59,11 @@
</div> </div>
<AuthorizeView Policy="@Constants.Roles.RaidLead"> <AuthorizeView>
<div class="nav-item px-3"> @if (_template.RaidOwnerId == _user.LiebUserId || _user.RoleAssignments.Max(a => a.LiebRole.Level) >= Constants.RaidEditPowerLevel)
@{string navLink = $"raidtemplateedit/{_template.RaidTemplateId}";} {
<NavLink class="nav-link" href="@navLink"> <button class="controlButton raidButton" @onclick="() => EditClicked()">Edit</button>
<span class="oi oi-plus" aria-hidden="true"></span> Edit }
</NavLink>
</div>
</AuthorizeView> </AuthorizeView>
</body> </body>
@ -72,7 +71,15 @@
[Parameter] [Parameter]
public RaidTemplate _template { get; set; } public RaidTemplate _template { get; set; }
[Parameter]
public LiebUser? _user { get; set; }
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
} }
async Task EditClicked()
{
NavigationManager.NavigateTo($"raidtemplateedit/{_template.RaidTemplateId}");
}
} }

View file

@ -4,16 +4,18 @@
@using Lieb.Models @using Lieb.Models
@using Lieb.Models.GuildWars2.Raid @using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using System.Security.Claims
@inject RaidTemplateService RaidTemplateService @inject RaidTemplateService RaidTemplateService
@inject UserService UserService @inject UserService UserService
@inject TimeZoneService TimeZoneService @inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IJSRuntime JsRuntime @inject IJSRuntime JsRuntime
<h3>CreateRaid</h3> <h3>CreateRaid</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead" Context="authorizationContext"> <AuthorizeView Policy="@Constants.Roles.RaidLead.Name" Context="authorizationContext">
<EditForm Model="@_template" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@_template" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
@{ @{
@ -83,7 +85,7 @@
<option value="">Not Locked</option> <option value="">Not Locked</option>
@foreach(LiebRole role in UserService.GetLiebRoles()) @foreach(LiebRole role in UserService.GetLiebRoles())
{ {
if (!role.IsSystemRole) if (role.Type != RoleType.SystemRole)
{ {
<option value="@role.RoleName">@role.RoleName</option> <option value="@role.RoleName">@role.RoleName</option>
} }
@ -177,6 +179,7 @@
public string raidId { get; set; } public string raidId { get; set; }
public RaidTemplate _template; public RaidTemplate _template;
private LiebUser _user;
private string _errorMessage = string.Empty; private string _errorMessage = string.Empty;
@ -187,18 +190,39 @@
private DateTimeOffset _freeForAllTime; private DateTimeOffset _freeForAllTime;
private string _userTimeZone = string.Empty; private string _userTimeZone = string.Empty;
private List<PlannedRaidRole> _rolesToDelete = new List<PlannedRaidRole>();
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState != null)
{
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUser(discordId);
}
if(_user == null)
{
NavigationManager.NavigateTo("");
}
if(!string.IsNullOrEmpty(raidId) && int.TryParse(raidId, out int parsedId)) if(!string.IsNullOrEmpty(raidId) && int.TryParse(raidId, out int parsedId))
{ {
_template = RaidTemplateService.GetTemplate(parsedId); _template = RaidTemplateService.GetTemplate(parsedId);
_startTime = _template.StartTime;
_endTime = _template.EndTime; if (_template != null && (_template.RaidOwnerId == _user.LiebUserId
_raidDate = _startTime.Date; || _user.RoleAssignments.Max(a => a.LiebRole.Level) >= Constants.RaidEditPowerLevel))
_freeForAllTime = _template.FreeForAllTime; {
_freeForAllDate = _freeForAllTime.Date; _startTime = _template.StartTime;
_endTime = _template.EndTime;
_raidDate = _startTime.Date;
_freeForAllTime = _template.FreeForAllTime;
_freeForAllDate = _freeForAllTime.Date;
}
else
{
_template = new RaidTemplate();
}
} }
else else
{ {
@ -215,6 +239,10 @@
async Task DeleteRoleClicked(PlannedRaidRole role) async Task DeleteRoleClicked(PlannedRaidRole role)
{ {
if(role.PlannedRaidRoleId != 0)
{
_rolesToDelete.Add(role);
}
_template.Roles.Remove(role); _template.Roles.Remove(role);
} }
@ -260,7 +288,12 @@
} }
_template.FreeForAllTime = _freeForAllDate.Date + _freeForAllTime.TimeOfDay; _template.FreeForAllTime = _freeForAllDate.Date + _freeForAllTime.TimeOfDay;
await RaidTemplateService.AddOrEditTemplate(_template); if (_template.RaidOwnerId == 0)
{
_template.RaidOwnerId = _user.LiebUserId;
}
await RaidTemplateService.AddOrEditTemplate(_template, _rolesToDelete, new List<RaidReminder>());
NavigationManager.NavigateTo("raidtemplateoverview"); NavigationManager.NavigateTo("raidtemplateoverview");
} }
} }

View file

@ -11,7 +11,7 @@
<h3>RaidTemplateOverview</h3> <h3>RaidTemplateOverview</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead"> <AuthorizeView Policy="@Constants.Roles.RaidLead.Name">
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="raidtemplateedit"> <NavLink class="nav-link" href="raidtemplateedit">
<span class="oi oi-plus" aria-hidden="true"></span> Add Raid <span class="oi oi-plus" aria-hidden="true"></span> Add Raid
@ -22,17 +22,23 @@
@foreach (var raid in _templates.OrderBy(r => r.StartTime)) @foreach (var raid in _templates.OrderBy(r => r.StartTime))
{ {
<br /> <br />
<RaidTemplateDetails _template=@raid/> <RaidTemplateDetails _template=@raid _user=@_user/>
} }
@code @code
{ {
private List<RaidTemplate> _templates; private List<RaidTemplate> _templates;
private LiebUser? _user;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
_templates = RaidTemplateService.GetTemplates(); _templates = RaidTemplateService.GetTemplates();
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated)
{
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUser(discordId);
}
} }
} }

View file

@ -70,7 +70,7 @@
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUserSmall(discordId); _user = UserService.GetLiebUserGW2AccountOnly(discordId);
} }

View file

@ -115,7 +115,7 @@
{ {
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
_user = UserService.GetLiebUserSmall(discordId); _user = UserService.GetLiebUserGW2AccountOnly(discordId);
if(!string.IsNullOrEmpty(gw2Id) && int.TryParse(gw2Id, out int parsedId) && _user.GuildWars2Accounts.Where(a => a.GuildWars2AccountId == parsedId).Any()) if(!string.IsNullOrEmpty(gw2Id) && int.TryParse(gw2Id, out int parsedId) && _user.GuildWars2Accounts.Where(a => a.GuildWars2AccountId == parsedId).Any())
{ {

View file

@ -8,20 +8,19 @@
<h3>Role Edit</h3> <h3>Role Edit</h3>
<AuthorizeView Policy="@Constants.Roles.Admin"> <AuthorizeView Policy="@Constants.Roles.Admin.Name">
<Authorized> <Authorized>
<table>
<table> @foreach(LiebRole role in _roles)
@foreach(LiebRole role in _roles) {
{ <tr>
<tr> <td>@role.RoleName</td>
<td>@if(!role.IsSystemRole) <td>@if(role.Type == RoleType.UserDefinedRole)
{ {
<button type=button @onclick="() => DeleteRoleClicked(role)">Delete Role</button> <button type=button @onclick="() => DeleteRoleClicked(role)">Delete Role</button>
}</td> }</td>
<td>@role.RoleName</td> </tr>
</tr> }
}
</table> </table>
<p> <p>
<label> <label>
@ -50,9 +49,9 @@
LiebRole role = new LiebRole() LiebRole role = new LiebRole()
{ {
RoleName = _newRoleName, RoleName = _newRoleName,
IsSystemRole = false, Type = RoleType.UserDefinedRole,
Level = 0, Level = 0,
LevelToAssign = Constants.RoleLevels.RaidLeadLevel LevelToAssign = Constants.Roles.RaidLead.PowerLevel
}; };
await UserService.AddRole(role); await UserService.AddRole(role);
_roles = UserService.GetLiebRoles(); _roles = UserService.GetLiebRoles();

View file

@ -17,7 +17,7 @@
<ValidationSummary /> <ValidationSummary />
<AuthorizeView Policy="@Constants.Roles.Admin" Context="authorizationContext"> <AuthorizeView Policy="@Constants.Roles.Admin.Name" Context="authorizationContext">
<Authorized> <Authorized>
<p>@_submitMessage</p> <p>@_submitMessage</p>
<p> <p>
@ -44,21 +44,18 @@
<tr> <tr>
<th></th> <th></th>
<th>Role</th> <th>Role</th>
<th>IsSystemRole</th> <th>Type</th>
</tr> </tr>
@foreach (LiebRole role in _roles) @foreach (LiebRole role in _roles)
{ {
<tr> <tr>
@{ @{
bool hasRole = _user.RoleAssignments.Where(a => a.LiebRoleId == role.LiebRoleId).Any(); bool hasRole = _user.RoleAssignments.Where(a => a.LiebRoleId == role.LiebRoleId).Any();
bool disabled = _editingUserRights < role.LevelToAssign; bool disabled = _editingUserRights < role.LevelToAssign || role.Type == RoleType.GuildRole;
} }
<td><input type="checkbox" disabled="@disabled" checked="@hasRole" @onchange="args => RoleStatusChanged(role, args)" /></td> <td><input type="checkbox" disabled="@disabled" checked="@hasRole" @onchange="args => RoleStatusChanged(role, args)" /></td>
<td>@role.RoleName</td> <td>@role.RoleName</td>
@if(@role.IsSystemRole) <td>@role.Type.ToString()</td>
{
<td>True</td>
}
</tr> </tr>
} }
</table> </table>

View file

@ -8,7 +8,7 @@
<h3>UserOverview</h3> <h3>UserOverview</h3>
<AuthorizeView Policy="@Constants.Roles.Admin"> <AuthorizeView Policy="@Constants.Roles.Admin.Name">
<Authorized> <Authorized>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="roleedit"> <NavLink class="nav-link" href="roleedit">

View file

@ -47,10 +47,17 @@ builder.Services.AddAuthentication(opt =>
builder.Services.AddAuthorization(options => builder.Services.AddAuthorization(options =>
{ {
foreach(string role in Constants.Roles.GetAllRoles()) options.AddPolicy(Constants.Roles.User.Name, policy => policy.RequireClaim(Constants.ClaimType, new List<string>(){
{ Constants.Roles.User.Name, Constants.Roles.RaidLead.Name, Constants.Roles.Moderator.Name, Constants.Roles.Admin.Name}));
options.AddPolicy(role, policy => policy.RequireClaim(Constants.ClaimType, role));
} options.AddPolicy(Constants.Roles.RaidLead.Name, policy => policy.RequireClaim(Constants.ClaimType, new List<string>() {
Constants.Roles.RaidLead.Name, Constants.Roles.Moderator.Name, Constants.Roles.Admin.Name }));
options.AddPolicy(Constants.Roles.Moderator.Name, policy => policy.RequireClaim(Constants.ClaimType, new List<string>() {
Constants.Roles.Moderator.Name, Constants.Roles.Admin.Name }));
options.AddPolicy(Constants.Roles.Admin.Name, policy => policy.RequireClaim(Constants.ClaimType, new List<string>() {
Constants.Roles.Admin.Name }));
}); });

View file

@ -20,7 +20,7 @@
<span class="oi oi-plus" aria-hidden="true"></span> Raid Overview <span class="oi oi-plus" aria-hidden="true"></span> Raid Overview
</NavLink> </NavLink>
</div> </div>
<AuthorizeView Policy="@Constants.Roles.RaidLead"> <AuthorizeView Policy="@Constants.Roles.RaidLead.Name">
<Authorized> <Authorized>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="raidtemplateoverview"> <NavLink class="nav-link" href="raidtemplateoverview">
@ -38,7 +38,7 @@
</div> </div>
</Authorized> </Authorized>
</AuthorizeView> </AuthorizeView>
<AuthorizeView Policy="@Constants.Roles.Admin"> <AuthorizeView Policy="@Constants.Roles.Admin.Name">
<Authorized> <Authorized>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="buildoverview"> <NavLink class="nav-link" href="buildoverview">
@ -56,7 +56,7 @@
</div> </div>
</Authorized> </Authorized>
</AuthorizeView> </AuthorizeView>
<AuthorizeView Policy="@Constants.Roles.Admin"> <AuthorizeView Policy="@Constants.Roles.Admin.Name">
<Authorized> <Authorized>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="useroverview"> <NavLink class="nav-link" href="useroverview">