create-user lehnt einen bereits existierenden Nutzernamen ab und UserStore hatte keinen Weg, ein Passwort nachträglich zu ändern - einzige Alternative wäre manuelles Editieren der LiteDB-Binärdatei gewesen. Neuer Befehl set-password <benutzername> [--password <pw>] nach demselben Muster wie create-user (Cli.ParsePassword extrahiert, von beiden Befehlen geteilt). docker/README.md ergänzt. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using LiteDB;
|
|
|
|
namespace LehrerApp.Api;
|
|
|
|
public class UserStore(string dataPath) : IDisposable
|
|
{
|
|
private readonly LiteDatabase _db = new(Path.Combine(dataPath, "users.db"));
|
|
|
|
private ILiteCollection<UserEntry> Col
|
|
{
|
|
get
|
|
{
|
|
var col = _db.GetCollection<UserEntry>("users");
|
|
col.EnsureIndex(x => x.Username, unique: true);
|
|
return col;
|
|
}
|
|
}
|
|
|
|
public bool CreateUser(string username, string password)
|
|
{
|
|
if (Col.Exists(x => x.Username == username)) return false;
|
|
Col.Insert(new UserEntry { Username = username, PasswordHash = PasswordHasher.Hash(password) });
|
|
return true;
|
|
}
|
|
|
|
public bool SetPassword(string username, string password)
|
|
{
|
|
var user = Col.FindOne(x => x.Username == username);
|
|
if (user is null) return false;
|
|
user.PasswordHash = PasswordHasher.Hash(password);
|
|
Col.Update(user);
|
|
return true;
|
|
}
|
|
|
|
public bool VerifyPassword(string username, string password)
|
|
{
|
|
var user = Col.FindOne(x => x.Username == username);
|
|
return user is not null && PasswordHasher.Verify(password, user.PasswordHash);
|
|
}
|
|
|
|
public void Dispose() => _db.Dispose();
|
|
}
|
|
|
|
internal class UserEntry
|
|
{
|
|
public ObjectId Id { get; set; } = ObjectId.NewObjectId();
|
|
public string Username { get; set; } = "";
|
|
public string PasswordHash { get; set; } = "";
|
|
}
|