Fix db seeding, migration, repository services
This commit is contained in:
parent
872dc1e263
commit
6b87902ca7
22 changed files with 606 additions and 64 deletions
|
@ -16,18 +16,28 @@ public record Role
|
|||
return new Role(id, name);
|
||||
}
|
||||
|
||||
private static Role? _admin = null;
|
||||
public static Role Admin
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Role(1, "Admin");
|
||||
if (_admin == null)
|
||||
{
|
||||
_admin = new Role(1, "Admin");
|
||||
}
|
||||
return _admin!;
|
||||
}
|
||||
}
|
||||
private static Role? _user = null;
|
||||
public static Role User
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Role(2, "User");
|
||||
if (_user == null)
|
||||
{
|
||||
_user = new Role(2, "User");
|
||||
}
|
||||
return _user!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,26 @@ public class User
|
|||
return new User(id, username, email, passwordHash, roleId, role);
|
||||
}
|
||||
|
||||
public static User CreateNew(string username, string email, string password, IPasswordHasher passwordHasher)
|
||||
{
|
||||
if (!IsValidUsername(username).IsValid)
|
||||
{
|
||||
throw new ArgumentException("Invalid username.");
|
||||
}
|
||||
if (!IsValidEmail(email))
|
||||
{
|
||||
throw new ArgumentException("Invalid email.");
|
||||
}
|
||||
if (!IsValidPassword(password).IsValid)
|
||||
{
|
||||
throw new ArgumentException("Invalid password.");
|
||||
}
|
||||
|
||||
var user = new User(0, username, email, password, DefaultRole.Id, DefaultRole);
|
||||
user.PasswordHash = passwordHasher.HashPassword(user, password);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
public (bool Success, IEnumerable<string>? Errors) UpdatePassword(string password, IPasswordHasher passwordHasher, User requestingUser)
|
||||
{
|
||||
|
@ -44,21 +64,16 @@ public class User
|
|||
|
||||
if (validityCheck.IsValid)
|
||||
{
|
||||
PasswordHash = passwordHasher.HashPassword(password);
|
||||
PasswordHash = passwordHasher.HashPassword(this, password);
|
||||
}
|
||||
|
||||
return validityCheck;
|
||||
}
|
||||
|
||||
|
||||
public bool VerifyPasswordAgainstHash(string providedPassword, IPasswordHasher passwordHasher, User requestingUser)
|
||||
public bool VerifyPasswordAgainstHash(string providedPassword, IPasswordHasher passwordHasher)
|
||||
{
|
||||
if (requestingUser.Id != Id || requestingUser.Role.Name != "Admin")
|
||||
{
|
||||
throw new UnauthorizedAccessException("Only admins or the same user can verify passwords.");
|
||||
}
|
||||
|
||||
return passwordHasher.VerifyHashedPassword(PasswordHash, providedPassword);
|
||||
return passwordHasher.VerifyHashedPassword(this, PasswordHash, providedPassword);
|
||||
}
|
||||
|
||||
|
||||
|
@ -121,7 +136,7 @@ public class User
|
|||
|
||||
public (bool Success, IEnumerable<string>? Errors) UpdateUsername(string username, User requestingUser)
|
||||
{
|
||||
if (requestingUser.Id != Id || requestingUser.Role.Name != "Admin")
|
||||
if (requestingUser.Role.Name != "Admin")
|
||||
{
|
||||
throw new UnauthorizedAccessException("Only admins can update username.");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue