feat: enhance AuthService to support admin role assignment for the first user

This commit is contained in:
2026-01-06 15:42:29 -05:00
parent edd94c45e8
commit c934c85b13
2 changed files with 7 additions and 2 deletions
+6 -2
View File
@@ -12,10 +12,11 @@ using Bcrypt = BCrypt.Net.BCrypt;
namespace SharpbinV3.Server.Services namespace SharpbinV3.Server.Services
{ {
public sealed class AuthService(AppDbContext db, IOptions<JWTSettings> options) public sealed class AuthService(AppDbContext db, IOptions<JWTSettings> jwtOptions, IOptions<AuthSettings> authOptions)
{ {
private readonly AppDbContext _db = db; private readonly AppDbContext _db = db;
private readonly JWTSettings _jwtSettings = options.Value; private readonly JWTSettings _jwtSettings = jwtOptions.Value;
private readonly AuthSettings _authSettings = authOptions.Value;
public async Task<User> CreateUser(string username, string password, string? email, string? displayName) public async Task<User> CreateUser(string username, string password, string? email, string? displayName)
{ {
@@ -28,6 +29,9 @@ namespace SharpbinV3.Server.Services
UUID = Guid.CreateVersion7() UUID = Guid.CreateVersion7()
}; };
if (_authSettings.First_User_Admin && !await _db.Users.AnyAsync())
user.Roles = [255];
_db.Users.Add(user); _db.Users.Add(user);
await _db.SaveChangesAsync(); await _db.SaveChangesAsync();
return user; return user;
@@ -5,5 +5,6 @@
public string CF_Turnstile_SiteKey { get; set; } = string.Empty; public string CF_Turnstile_SiteKey { get; set; } = string.Empty;
public string CF_Turnstile_SecretKey { get; set; } = string.Empty; public string CF_Turnstile_SecretKey { get; set; } = string.Empty;
public bool Registration_Enabled { get; set; } = true; public bool Registration_Enabled { get; set; } = true;
public bool First_User_Admin { get; set; } = true;
} }
} }