| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Backend.DTOs; |
| | 3 | | using Backend.Services; |
| | 4 | |
|
| | 5 | | namespace Backend.Model |
| | 6 | | { |
| | 7 | | public class UserSerivce: IUserService |
| | 8 | | { |
| | 9 | | private readonly PlannerContext _context; |
| | 10 | | private readonly ILogger<UserSerivce> _logger; |
| | 11 | |
|
| 16 | 12 | | public UserSerivce(PlannerContext context, ILogger<UserSerivce> logger) |
| | 13 | | { |
| 16 | 14 | | _context = context; |
| 16 | 15 | | _logger = logger; |
| 16 | 16 | | } |
| | 17 | |
|
| | 18 | | public async Task<User> CreateUserAsync(string email, string password) |
| | 19 | | { |
| | 20 | | // Check if user already exists |
| 8 | 21 | | if (await GetByEmailAsync(email) != null) |
| | 22 | | { |
| 1 | 23 | | _logger.LogInformation("Attempt to create a user that already exists: {Email}", email); |
| 1 | 24 | | throw new InvalidOperationException("User already exists."); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | // Hash password |
| 7 | 28 | | string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password); |
| 7 | 29 | | _logger.LogDebug("Password hashed for user: {Email}; hash: {hash}", email, hashedPassword); |
| | 30 | |
|
| 7 | 31 | | var user = new User |
| 7 | 32 | | { |
| 7 | 33 | | Email = email, |
| 7 | 34 | | PasswordHash = hashedPassword |
| 7 | 35 | | }; |
| | 36 | |
|
| 7 | 37 | | _context.Users.Add(user); |
| 7 | 38 | | await _context.SaveChangesAsync(); |
| | 39 | |
|
| 7 | 40 | | _logger.LogInformation("User created successfully: {user}", user); |
| | 41 | |
|
| 7 | 42 | | return user; |
| 7 | 43 | | } |
| | 44 | |
|
| | 45 | | public async Task<User> GetByEmailAsync(string email) |
| | 46 | | { |
| 10 | 47 | | var user = await _context.Users.FirstOrDefaultAsync(u => u.Email == email) ?? null!; |
| 10 | 48 | | if (user == null) |
| | 49 | | { |
| 8 | 50 | | _logger.LogInformation("User not found with email: {Email}", email); |
| 8 | 51 | | return null!; |
| | 52 | | } |
| | 53 | |
|
| 2 | 54 | | return user; |
| 10 | 55 | | } |
| | 56 | |
|
| | 57 | | public async Task<User> GetByIdAsync(int id) |
| | 58 | | { |
| 9 | 59 | | var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == id) ?? null!; |
| 9 | 60 | | if (user == null) |
| | 61 | | { |
| 3 | 62 | | _logger.LogInformation("User not found with ID: {Id}", id); |
| 3 | 63 | | return null!; |
| | 64 | | } |
| | 65 | |
|
| 6 | 66 | | return user; |
| 9 | 67 | | } |
| | 68 | |
|
| | 69 | | public bool VerifyPasswordHash(string password, User user) |
| | 70 | | { |
| | 71 | | // Verify the password against the stored hash |
| 11 | 72 | | if (user == null || string.IsNullOrEmpty(user.PasswordHash)) |
| | 73 | | { |
| 3 | 74 | | _logger.LogWarning("User or password hash is null or empty."); |
| 3 | 75 | | return false; |
| | 76 | | } |
| | 77 | |
|
| 8 | 78 | | return BCrypt.Net.BCrypt.Verify(password, user.PasswordHash); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public async Task ChangePasswordAsync(string userId, string oldPassword, string newPassword) |
| | 82 | | { |
| 5 | 83 | | if (!int.TryParse(userId, out int id)) |
| | 84 | | { |
| 2 | 85 | | _logger.LogWarning("Invalid user ID format: {UserId}", userId); |
| 2 | 86 | | throw new ArgumentException("Invalid user ID format."); |
| | 87 | | } |
| | 88 | |
|
| 3 | 89 | | var user = await GetByIdAsync(id); |
| 3 | 90 | | if (user == null) |
| | 91 | | { |
| 1 | 92 | | _logger.LogWarning("User not found with ID: {UserId}", userId); |
| 1 | 93 | | throw new InvalidOperationException("User not found."); |
| | 94 | | } |
| | 95 | |
|
| 2 | 96 | | if (!VerifyPasswordHash(oldPassword, user)) |
| | 97 | | { |
| 1 | 98 | | _logger.LogWarning("Old password does not match for user ID: {UserId}", userId); |
| 1 | 99 | | throw new UnauthorizedAccessException("Old password is incorrect."); |
| | 100 | | } |
| | 101 | |
|
| | 102 | | // Hash the new password |
| 1 | 103 | | string newHashedPassword = BCrypt.Net.BCrypt.HashPassword(newPassword); |
| 1 | 104 | | user.PasswordHash = newHashedPassword; |
| | 105 | |
|
| 1 | 106 | | _context.Users.Update(user); |
| 1 | 107 | | await _context.SaveChangesAsync(); |
| | 108 | |
|
| 1 | 109 | | _logger.LogInformation("Password changed successfully for user ID: {UserId}", userId); |
| 1 | 110 | | } |
| | 111 | |
|
| | 112 | | public async Task<bool> UpdatePasswordAsync(int userId, string newPassword) |
| | 113 | | { |
| 2 | 114 | | var user = await GetByIdAsync(userId); |
| 2 | 115 | | if (user == null) |
| | 116 | | { |
| 1 | 117 | | _logger.LogWarning("User not found with ID: {UserId}", userId); |
| 1 | 118 | | return false; |
| | 119 | | } |
| | 120 | |
|
| 1 | 121 | | user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(newPassword); |
| 1 | 122 | | _context.Users.Update(user); |
| 1 | 123 | | await _context.SaveChangesAsync(); |
| | 124 | |
|
| 1 | 125 | | _logger.LogInformation("Password updated successfully for user ID: {UserId}", userId); |
| 1 | 126 | | return true; |
| 2 | 127 | | } |
| | 128 | |
|
| | 129 | | public Task<UserDto> UpdateUserDtoAsync(UserDto userDto) |
| | 130 | | { |
| 0 | 131 | | throw new NotImplementedException(); |
| | 132 | | } |
| | 133 | | } |
| | 134 | | } |