|
| 1 | +using CodeBeam.UltimateAuth.Core.Abstractions; |
| 2 | +using CodeBeam.UltimateAuth.Core.Domain; |
| 3 | +using CodeBeam.UltimateAuth.Core.Infrastructure; |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | + |
| 7 | +namespace CodeBeam.UltimateAuth.Credentials.EntityFrameworkCore; |
| 8 | + |
| 9 | +internal sealed class EfCoreUserStore<TUser, TUserId> : IUAuthUserStore<TUserId> where TUser : class |
| 10 | +{ |
| 11 | + private readonly DbContext _db; |
| 12 | + private readonly CredentialUserMapping<TUser, TUserId> _map; |
| 13 | + |
| 14 | + public EfCoreUserStore(DbContext db, IOptions<CredentialUserMappingOptions<TUser, TUserId>> options) |
| 15 | + { |
| 16 | + _db = db; |
| 17 | + _map = CredentialUserMappingBuilder.Build(options.Value); |
| 18 | + } |
| 19 | + |
| 20 | + public async Task<IUser<TUserId>?> FindByIdAsync(string? tenantId, TUserId userId, CancellationToken ct = default) |
| 21 | + { |
| 22 | + var user = await _db.Set<TUser>().FirstOrDefaultAsync(u => _map.UserId(u)!.Equals(userId), ct); |
| 23 | + |
| 24 | + if (user is null || !_map.CanAuthenticate(user)) |
| 25 | + return null; |
| 26 | + |
| 27 | + return new EfCoreAuthUser<TUserId>(_map.UserId(user)); |
| 28 | + } |
| 29 | + |
| 30 | + public async Task<UserRecord<TUserId>?> FindByUsernameAsync(string? tenantId, string username, CancellationToken ct = default) |
| 31 | + { |
| 32 | + var user = await _db.Set<TUser>().FirstOrDefaultAsync(u => _map.Username(u) == username, ct); |
| 33 | + |
| 34 | + if (user is null || !_map.CanAuthenticate(user)) |
| 35 | + return null; |
| 36 | + |
| 37 | + return new UserRecord<TUserId> |
| 38 | + { |
| 39 | + Id = _map.UserId(user), |
| 40 | + Username = _map.Username(user), |
| 41 | + PasswordHash = _map.PasswordHash(user), |
| 42 | + IsActive = true, |
| 43 | + CreatedAt = DateTimeOffset.UtcNow, |
| 44 | + IsDeleted = false |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + public async Task<IUser<TUserId>?> FindByLoginAsync(string? tenantId, string login, CancellationToken ct = default) |
| 49 | + { |
| 50 | + var user = await _db.Set<TUser>().FirstOrDefaultAsync(u => _map.Username(u) == login, ct); |
| 51 | + |
| 52 | + if (user is null || !_map.CanAuthenticate(user)) |
| 53 | + return null; |
| 54 | + |
| 55 | + return new EfCoreAuthUser<TUserId>(_map.UserId(user)); |
| 56 | + } |
| 57 | + |
| 58 | + public Task<string?> GetPasswordHashAsync(string? tenantId, TUserId userId, CancellationToken ct = default) |
| 59 | + { |
| 60 | + return _db.Set<TUser>() |
| 61 | + .Where(u => _map.UserId(u)!.Equals(userId)) |
| 62 | + .Select(u => _map.PasswordHash(u)) |
| 63 | + .FirstOrDefaultAsync(ct); |
| 64 | + } |
| 65 | + |
| 66 | + public Task SetPasswordHashAsync(string? tenantId, TUserId userId, string passwordHash, CancellationToken token = default) |
| 67 | + { |
| 68 | + throw new NotSupportedException("Password updates are not supported by EfCoreUserStore. " + |
| 69 | + "Use application-level user management services."); |
| 70 | + } |
| 71 | + |
| 72 | + public async Task<long> GetSecurityVersionAsync(string? tenantId, TUserId userId, CancellationToken ct = default) |
| 73 | + { |
| 74 | + var user = await _db.Set<TUser>().FirstOrDefaultAsync(u => _map.UserId(u)!.Equals(userId), ct); |
| 75 | + return user is null ? 0 : _map.SecurityVersion(user); |
| 76 | + } |
| 77 | + |
| 78 | + public Task IncrementSecurityVersionAsync(string? tenantId, TUserId userId, CancellationToken token = default) |
| 79 | + { |
| 80 | + throw new NotSupportedException("Security version updates must be handled by the application."); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments