diff --git a/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs b/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs new file mode 100644 index 00000000..b3db6c4a --- /dev/null +++ b/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs @@ -0,0 +1,60 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.Logging; +using Microsoft.EntityFrameworkCore; +using LBPUnion.ProjectLighthouse.Types; + +namespace LBPUnion.ProjectLighthouse.Maintenance.Commands +{ + [UsedImplicitly] + public class CreateUserCommand : ICommand + { + private readonly Database _database = new(); + + public async Task Run(string[] args) + { + string onlineId = args[0]; + string password = args[1]; + + password = HashHelper.Sha256Hash(password); + + User? user = await this._database.Users.FirstOrDefaultAsync(u => u.Username == onlineId); + if (user == null) + { + user = await this._database.CreateUser(onlineId, + HashHelper.BCryptHash(password)); + Logger.Log( + $"Created user {user.UserId} with online ID (username) {user.Username} and the specified password.", LoggerLevelLogin.Instance); + + user.PasswordResetRequired = true; + Logger.Log("This user will need to reset their password when they log in.", + LoggerLevelLogin.Instance); + + await this._database.SaveChangesAsync(); + Logger.Log("Database changes saved.", + LoggerLevelDatabase.Instance); + } + else + { + Logger.Log("A user with this username already exists.", + LoggerLevelLogin.Instance); + } + } + + public string Name() => "Create New User"; + + public string[] Aliases() => + new[] + { + "useradd", "adduser", "newuser", "createUser" + }; + + public string Arguments() => " "; + + public int RequiredArgs() => 2; + } +} \ No newline at end of file diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index ffc2f979..6240dece 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -8,34 +8,38 @@ - - - - - - - + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + Always - + Always + + <_ContentIncludedByDefault Remove="Pages\Admin\Index.cshtml" /> + + - - + +