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 const string ClaimType = "Role";
public static readonly int RaidEditPowerLevel = Roles.Moderator.PowerLevel;
public static class Roles
{
public const string User = "User";
public const string RaidLead = "RaidLead";
public const string GuildLead = "GuildLead";
public const string Admin = "Admin";
public static readonly RoleConstant User = new RoleConstant("user", 20);
public static readonly RoleConstant RaidLead = new RoleConstant("RaidLead", 40);
public static readonly RoleConstant Moderator = new RoleConstant("Moderator", 70);
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)
{
//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();
@ -52,10 +52,10 @@ namespace Lieb.Data
context.SaveChanges();
int AdminRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.Admin).LiebRoleId;
int GuildLeadRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.GuildLead).LiebRoleId;
int RaidLeadRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.RaidLead).LiebRoleId;
int UserRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.User).LiebRoleId;
int AdminRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.Admin.Name).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.Name).LiebRoleId;
int UserRoleId = context.LiebRoles.FirstOrDefault(x => x.RoleName == Constants.Roles.User.Name).LiebRoleId;
var assignments = new RoleAssignment[]
{

View file

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

View file

@ -20,26 +20,12 @@ namespace Lieb.Data
if (build.GuildWars2BuildId == 0)
{
context.GuildWars2Builds.Add(build);
await context.SaveChangesAsync();
}
else
{
GuildWars2Build? buildToChange = await context.GuildWars2Builds
.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();
}
context.Update(build);
}
await context.SaveChangesAsync();
}
}

View file

@ -45,7 +45,7 @@ namespace Lieb.Data
.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)
{
@ -57,54 +57,26 @@ namespace Lieb.Data
}
else
{
Raid? raidToChange = await context.Raids
.Include(r => r.Roles)
.Include(r => r.SignUpHistory)
.Include(r => r.Reminders)
.Include(r => r.SignUps)
.FirstOrDefaultAsync(r => r.RaidId == raid.RaidId);
if (raidToChange != null)
context.Update(raid);
context.PlannedRaidRoles.RemoveRange(rolesToDelete);
context.RaidReminders.RemoveRange(remindersToDelete);
//move users back to "Random" role
if (raid.RaidType != RaidType.Planned)
{
raidToChange.Title = raid.Title;
raidToChange.Description = raid.Description;
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)
int randomRoleId = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole).PlannedRaidRoleId;
foreach (RaidSignUp signUp in raid.SignUps)
{
EditRoles(raidToChange, raid, context);
}
else
{
if(!raidToChange.Roles.Where(r => r.IsRandomSignUpRole).Any())
if (randomRoleId == 0)
{
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;
foreach (RaidSignUp signUp in raidToChange.SignUps)
else
{
if (randomRoleId == 0)
{
signUp.PlannedRaidRole = raidToChange.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
}
else
{
signUp.PlannedRaidRoleId = randomRoleId;
}
signUp.PlannedRaidRoleId = randomRoleId;
}
context.PlannedRaidRoles.RemoveRange(raidToChange.Roles.Where(r => !r.IsRandomSignUpRole));
}
EditReminders(raidToChange, raid, context);
context.PlannedRaidRoles.RemoveRange(raid.Roles.Where(r => !r.IsRandomSignUpRole));
}
await context.SaveChangesAsync();
@ -112,65 +84,7 @@ namespace Lieb.Data
}
}
private void EditRoles(Raid raidToEdit, Raid raid, LiebContext context)
{
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)
public async Task DeleteRaid(int raidId)
{
using var context = _contextFactory.CreateDbContext();
Raid raid = GetRaid(raidId);
@ -310,14 +224,14 @@ namespace Lieb.Data
if (raid.RaidType == RaidType.Planned)
{
//if (raid.MoveFlexAllowed)
if (raid.MoveFlexUsers)
{
return IsRoleSignUpAllowed(raid, liebUserId, plannedRoleId, signUpType, moveFlexUser, new List<int>()).Result;
}
//else
//{
// return IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType);
//}
else
{
return IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType);
}
}
else
{

View file

@ -31,7 +31,7 @@ namespace Lieb.Data
.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)
{
@ -39,39 +39,14 @@ namespace Lieb.Data
if (template.RaidTemplateId == 0)
{
context.RaidTemplates.Add(template);
await context.SaveChangesAsync();
}
else
{
RaidTemplate raidToChange = await context.RaidTemplates
.Include(r => r.Roles)
.Include(r => r.Reminders)
.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();
context.Update(template);
context.PlannedRaidRoles.RemoveRange(rolesToDelete);
context.RaidReminders.RemoveRange(remindersToDelete);
}
await context.SaveChangesAsync();
}
}

View file

@ -55,7 +55,7 @@ namespace Lieb.Data
.FirstOrDefault(u => u.LiebUserId == userId);
}
public LiebUser GetLiebUserSmall(ulong discordId)
public LiebUser GetLiebUserGW2AccountOnly(ulong discordId)
{
if (discordId > 0)
{
@ -69,7 +69,7 @@ namespace Lieb.Data
return new LiebUser();
}
public LiebUser GetLiebUserSmall(int userId)
public LiebUser GetLiebGW2AccountOnly(int userId)
{
using var context = _contextFactory.CreateDbContext();
return context.LiebUsers
@ -145,6 +145,36 @@ namespace Lieb.Data
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()
{
using var context = _contextFactory.CreateDbContext();