mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: enhance AuthService to support admin role assignment for the first user
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user