184 lines
5.7 KiB
C#
184 lines
5.7 KiB
C#
using AuthHex.Interfaces;
|
|
using AuthHex.Models;
|
|
using AuthHex.Utility.passwordHasher;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
|
|
namespace AuthHex.Repos
|
|
{
|
|
public class UserManageRepository : IUserManageRepository
|
|
{
|
|
private readonly AppDBContext _dbContext;
|
|
|
|
public UserManageRepository(AppDBContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
public Task<User> AddUserAsync(User user, CancellationToken ct = default)
|
|
{
|
|
_dbContext.Users.Add(user);
|
|
return Task.FromResult(user);
|
|
}
|
|
public Task<Token> AddTokenAsync(Token token, CancellationToken ct = default)
|
|
{
|
|
_dbContext.Token.Add(token);
|
|
return Task.FromResult(token);
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<User>> GetUserByIdentifiers(string? email, string? mobileNumber, string? nic, string? username, string? Optional1)
|
|
{
|
|
var query = _dbContext.Users.AsQueryable();
|
|
|
|
query = query.Where(u =>
|
|
(!string.IsNullOrEmpty(email) && u.Email == email) ||
|
|
(!string.IsNullOrEmpty(mobileNumber) && u.MobileNumber == mobileNumber) ||
|
|
(!string.IsNullOrEmpty(nic) && u.Nic == nic) ||
|
|
(!string.IsNullOrEmpty(username) && u.UserName == username) ||
|
|
(!string.IsNullOrEmpty(Optional1) && u.Optional1 == Optional1)
|
|
);
|
|
|
|
return await query.ToListAsync();
|
|
}
|
|
|
|
|
|
public async Task<User?> GetUserByIdentifierAndType(string identifier, Guid? userTypeId)
|
|
{
|
|
|
|
User? user = await _dbContext.Users
|
|
.FirstOrDefaultAsync(u =>
|
|
u.Email == identifier ||
|
|
u.MobileNumber == identifier ||
|
|
u.Nic == identifier
|
|
);
|
|
|
|
//var query = _dbContext.Users.AsQueryable();
|
|
|
|
//query = query.Where(u =>
|
|
// u.Email == identifier ||
|
|
// u.MobileNumber == identifier ||
|
|
// u.Nic == identifier
|
|
//);
|
|
|
|
//if (userTypeId != Guid.Empty)
|
|
// query = query.Where(u => u.UserTypeId == userTypeId);
|
|
|
|
return user;
|
|
}
|
|
|
|
public async Task<User?> GetUserByIdAsync(Guid userId, CancellationToken ct = default)
|
|
{
|
|
return await _dbContext.Users
|
|
.Include(u => u.Role)
|
|
.Include(u => u.UserType)
|
|
.FirstOrDefaultAsync(u => u.Id == userId, ct);
|
|
}
|
|
|
|
public async Task UpdateUserAsync(User user, CancellationToken ct = default)
|
|
{
|
|
_dbContext.Users.Update(user);
|
|
await _dbContext.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task<List<UserSession>> GetUserSessionsAsync(Guid userId, CancellationToken ct = default)
|
|
{
|
|
return await _dbContext.UserSessions
|
|
.Where(s => s.UserId == userId)
|
|
.OrderByDescending(s => s.CreatedAt)
|
|
.ToListAsync(ct);
|
|
}
|
|
|
|
public async Task<UserSession?> GetActiveSessionByRefreshTokenAsync(string refreshToken, CancellationToken ct = default)
|
|
{
|
|
var activeSessions = await _dbContext.UserSessions
|
|
.Include(s => s.User)
|
|
.Where(s => s.RevokedAt == null && s.ExpiresAt > DateTime.UtcNow)
|
|
.OrderByDescending(s => s.CreatedAt)
|
|
.ToListAsync(ct);
|
|
|
|
foreach (var session in activeSessions)
|
|
{
|
|
if (PasswordHasher.Verify(refreshToken, session.RefreshTokenHash))
|
|
return session;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task InvalidateUserSessionsAsync(Guid userId, CancellationToken ct = default)
|
|
{
|
|
var userSessions = await _dbContext.UserSessions
|
|
.Where(s => s.UserId == userId && s.RevokedAt == null)
|
|
.ToListAsync(ct);
|
|
|
|
foreach (var session in userSessions)
|
|
{
|
|
session.RevokedAt = DateTime.UtcNow;
|
|
}
|
|
|
|
await _dbContext.SaveChangesAsync(ct);
|
|
}
|
|
|
|
//public async Task<bool> GetExUser(string identifier)
|
|
//{
|
|
// var userExists = await _dbContext.Users
|
|
// .AnyAsync(u => u.Email == identifier || u.MobileNumber == identifier || u.Nic == identifier);
|
|
// return userExists;
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public async Task<TuteItem?> GetByIdAsync(int id, CancellationToken ct = default)
|
|
//{
|
|
// return await _dbContext.TuteItems
|
|
// .AsNoTracking()
|
|
// .FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
//}
|
|
|
|
//public async Task<List<TuteItem>> ListAsync(CancellationToken ct = default)
|
|
//{
|
|
// return await _dbContext.TuteItems
|
|
// .AsNoTracking()
|
|
// .OrderByDescending(x => x.Id)
|
|
// .ToListAsync(ct);
|
|
//}
|
|
|
|
//public async Task<TuteItem?> UpdateAsync(int id, string title, string? notes, CancellationToken ct = default)
|
|
//{
|
|
// var entity = await _dbContext.TuteItems.FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
// if (entity == null)
|
|
// return null;
|
|
|
|
// entity.Title = title;
|
|
// entity.Notes = notes;
|
|
// return entity;
|
|
//}
|
|
|
|
//public async Task<bool> DeleteAsync(int id, CancellationToken ct = default)
|
|
//{
|
|
// var entity = await _dbContext.TuteItems.FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
// if (entity == null)
|
|
// return false;
|
|
|
|
// _dbContext.TuteItems.Remove(entity);
|
|
// return true;
|
|
//}
|
|
}
|
|
}
|