mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-01 09:48:37 +00:00
Add support for creating new users as an admin.
This commit is contained in:
parent
fbcf0eafa7
commit
d52bb6ecc4
2 changed files with 76 additions and 12 deletions
60
ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs
Normal file
60
ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs
Normal file
|
@ -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() => "<OnlineID> <Password>";
|
||||||
|
|
||||||
|
public int RequiredArgs() => 2;
|
||||||
|
}
|
||||||
|
}
|
|
@ -33,6 +33,10 @@
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<_ContentIncludedByDefault Remove="Pages\Admin\Index.cshtml" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
|
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
|
||||||
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 > "$(ProjectDir)/gitVersion.txt"" />
|
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 > "$(ProjectDir)/gitVersion.txt"" />
|
||||||
<Exec Command="git branch --show-current > "$(ProjectDir)/gitBranch.txt"" />
|
<Exec Command="git branch --show-current > "$(ProjectDir)/gitBranch.txt"" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue