From c934c85b130d2c0086146110f3211f612387b8d3 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Tue, 6 Jan 2026 15:42:29 -0500 Subject: [PATCH] feat: enhance AuthService to support admin role assignment for the first user --- SharpbinV3.Server/Services/AuthService.cs | 8 ++++++-- SharpbinV3.Server/Settings/AuthSettings.cs | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/SharpbinV3.Server/Services/AuthService.cs b/SharpbinV3.Server/Services/AuthService.cs index 1d37015..1c33b56 100644 --- a/SharpbinV3.Server/Services/AuthService.cs +++ b/SharpbinV3.Server/Services/AuthService.cs @@ -12,10 +12,11 @@ using Bcrypt = BCrypt.Net.BCrypt; namespace SharpbinV3.Server.Services { - public sealed class AuthService(AppDbContext db, IOptions options) + public sealed class AuthService(AppDbContext db, IOptions jwtOptions, IOptions authOptions) { 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 CreateUser(string username, string password, string? email, string? displayName) { @@ -28,6 +29,9 @@ namespace SharpbinV3.Server.Services UUID = Guid.CreateVersion7() }; + if (_authSettings.First_User_Admin && !await _db.Users.AnyAsync()) + user.Roles = [255]; + _db.Users.Add(user); await _db.SaveChangesAsync(); return user; diff --git a/SharpbinV3.Server/Settings/AuthSettings.cs b/SharpbinV3.Server/Settings/AuthSettings.cs index 7b2f2c6..f5fbe2b 100644 --- a/SharpbinV3.Server/Settings/AuthSettings.cs +++ b/SharpbinV3.Server/Settings/AuthSettings.cs @@ -5,5 +5,6 @@ public string CF_Turnstile_SiteKey { get; set; } = string.Empty; public string CF_Turnstile_SecretKey { get; set; } = string.Empty; public bool Registration_Enabled { get; set; } = true; + public bool First_User_Admin { get; set; } = true; } }