mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-09-22 17:29:10 +00:00
Fix build errors, add commands for setting various permissions levels
This commit is contained in:
parent
af15dd6a0c
commit
ff12f5f7d5
3 changed files with 86 additions and 44 deletions
|
@ -1,43 +0,0 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Administration.Maintenance.Commands;
|
||||
|
||||
public class MakeUserAdminCommand : ICommand
|
||||
{
|
||||
private readonly Database database = new();
|
||||
|
||||
public string Name() => "Make User Admin";
|
||||
public string[] Aliases()
|
||||
=> new[]
|
||||
{
|
||||
"makeAdmin",
|
||||
};
|
||||
public string Arguments() => "<username/userId>";
|
||||
public int RequiredArgs() => 1;
|
||||
|
||||
public async Task Run(string[] args, Logger logger)
|
||||
{
|
||||
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.Username == args[0]);
|
||||
if (user == null)
|
||||
try
|
||||
{
|
||||
user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == Convert.ToInt32(args[0]));
|
||||
if (user == null) throw new Exception();
|
||||
}
|
||||
catch
|
||||
{
|
||||
logger.LogError($"Could not find user by parameter '{args[0]}'", LogArea.Command);
|
||||
return;
|
||||
}
|
||||
|
||||
user.PermissionLevel = PermissionLevel.Administrator;
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
logger.LogSuccess($"The user {user.Username} (id: {user.UserId}) is now an admin.", LogArea.Command);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Administration.Maintenance;
|
||||
|
||||
#region Base Type
|
||||
public abstract class SetUserPermissionLevelCommand : ICommand
|
||||
{
|
||||
private readonly PermissionLevel permissionLevel;
|
||||
|
||||
protected SetUserPermissionLevelCommand(PermissionLevel permissionLevel)
|
||||
{
|
||||
this.permissionLevel = permissionLevel;
|
||||
}
|
||||
|
||||
private readonly Database database = new();
|
||||
public abstract string Name();
|
||||
public abstract string[] Aliases();
|
||||
|
||||
public string Arguments() => "<username/userId>";
|
||||
public int RequiredArgs() => 1;
|
||||
|
||||
public async Task Run(string[] args, Logger logger)
|
||||
{
|
||||
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.Username == args[0]);
|
||||
if (user == null)
|
||||
try
|
||||
{
|
||||
user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == Convert.ToInt32(args[0]));
|
||||
if (user == null) throw new Exception();
|
||||
}
|
||||
catch
|
||||
{
|
||||
logger.LogError($"Could not find user by parameter '{args[0]}'",
|
||||
LogArea.Command);
|
||||
return;
|
||||
}
|
||||
|
||||
user.PermissionLevel = this.permissionLevel;
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
logger.LogSuccess($"The user {user.Username} (id: {user.UserId}) is now {this.permissionLevel}.",
|
||||
LogArea.Command);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Implementations
|
||||
|
||||
public class SetUserAdminCommand : SetUserPermissionLevelCommand
|
||||
{
|
||||
public SetUserAdminCommand() : base(PermissionLevel.Administrator)
|
||||
{}
|
||||
public override string Name() => "Make User Admin";
|
||||
public override string[] Aliases() => new []{"make-admin",};
|
||||
}
|
||||
|
||||
public class SetUserModeratorCommand : SetUserPermissionLevelCommand
|
||||
{
|
||||
public SetUserModeratorCommand() : base(PermissionLevel.Moderator)
|
||||
{}
|
||||
public override string Name() => "Make User Moderator";
|
||||
public override string[] Aliases() => new[] { "make-moderator", };
|
||||
}
|
||||
|
||||
public class BanUserCommand : SetUserPermissionLevelCommand
|
||||
{
|
||||
public BanUserCommand() : base(PermissionLevel.Banned)
|
||||
{}
|
||||
public override string Name() => "Ban User";
|
||||
public override string[] Aliases() => new[] { "ban", };
|
||||
}
|
||||
|
||||
public class DemoteUserCommand : SetUserPermissionLevelCommand
|
||||
{
|
||||
public DemoteUserCommand() : base(PermissionLevel.Default)
|
||||
{}
|
||||
public override string Name() => "Demote User";
|
||||
public override string[] Aliases() => new[] { "demote", };
|
||||
}
|
||||
#endregion
|
|
@ -103,7 +103,7 @@ public static class StartupTasks
|
|||
string password = CryptoHelper.BCryptHash(CryptoHelper.Sha256Hash(passwordClear));
|
||||
|
||||
User admin = database.CreateUser("admin", password).Result;
|
||||
admin.IsAdmin = true;
|
||||
admin.PermissionLevel = PermissionLevel.Administrator;
|
||||
admin.PasswordResetRequired = true;
|
||||
|
||||
database.SaveChanges();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue