mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-05 23:34:09 +02:00
Refactor user management: Extract IUserService and UserService, consolidate user registration, authentication, and profile management. Fix memory leaks in ApiClient, enhance connection management, and improve error handling in AuthController and UsersController. Update IRC command handling to utilize IUserService for user operations.
This commit is contained in:
@@ -304,73 +304,9 @@ public class ChatService : IChatService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<UserProfileDto?> GetUserProfileAsync(string username)
|
||||
{
|
||||
username = username.ToLowerInvariant();
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<EchoHubDbContext>();
|
||||
|
||||
var user = await db.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
if (user is null) return null;
|
||||
|
||||
return new UserProfileDto(
|
||||
user.Id, user.Username, user.DisplayName, user.Bio,
|
||||
user.NicknameColor, user.AvatarAscii, user.Status,
|
||||
user.StatusMessage, user.Role, user.CreatedAt, user.LastSeenAt);
|
||||
}
|
||||
|
||||
public Task<List<string>> GetChannelsForUserAsync(string username)
|
||||
=> Task.FromResult(_presenceTracker.GetChannelsForUser(username));
|
||||
|
||||
public async Task<(Guid UserId, string Username)?> AuthenticateUserAsync(string username, string password)
|
||||
{
|
||||
username = username.ToLowerInvariant();
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<EchoHubDbContext>();
|
||||
|
||||
var user = await db.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
if (user is null) return null;
|
||||
|
||||
if (!BCrypt.Net.BCrypt.Verify(password, user.PasswordHash))
|
||||
return null;
|
||||
|
||||
return (user.Id, user.Username);
|
||||
}
|
||||
|
||||
public async Task<(Guid UserId, string Username)?> RegisterUserAsync(string username, string password)
|
||||
{
|
||||
username = username.ToLowerInvariant().Trim();
|
||||
|
||||
if (!ValidationConstants.UsernameRegex().IsMatch(username))
|
||||
return null;
|
||||
|
||||
if (password.Length < 6 || password.Length > ValidationConstants.MaxPasswordLength)
|
||||
return null;
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<EchoHubDbContext>();
|
||||
|
||||
if (await db.Users.AnyAsync(u => u.Username == username))
|
||||
return null;
|
||||
|
||||
var isFirstUser = !await db.Users.AnyAsync();
|
||||
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = username,
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword(password),
|
||||
Role = isFirstUser ? ServerRole.Owner : ServerRole.Member,
|
||||
};
|
||||
|
||||
db.Users.Add(user);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return (user.Id, user.Username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collapse consecutive newlines and cap total line count to prevent newline spam.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user