130 lines
4.5 KiB
C#
130 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using AyCode.Database.DbContexts;
|
|
using AyCode.Database.DbContexts.Users;
|
|
using AyCode.Entities.Users;
|
|
using AyCode.Interfaces.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AyCode.Database.Extensions;
|
|
using AyCode.Core.Consts;
|
|
using AyCode.Database.DbSets.Users;
|
|
using AyCode.Interfaces.Profiles;
|
|
using AyCode.Interfaces.ServiceProviders;
|
|
|
|
namespace AyCode.Database.DataLayers.Users
|
|
{
|
|
public abstract class AcUserDalBase<TDbContext, TUser, TProfile, TUserToken, TServiceProvider> : AcDalBase<TDbContext>
|
|
where TDbContext : AcDbContextBase, IAcUserDbContextBase<TUser, TProfile, TUserToken, TServiceProvider>
|
|
where TUser : class, IAcUser<TProfile, TServiceProvider>
|
|
where TProfile : class, IAcProfile
|
|
where TUserToken : class, IAcUserTokenBase
|
|
where TServiceProvider : class, IAcServiceProvider
|
|
{
|
|
public TUser? GetUserById(Guid userId) => Session(x => x.GetUserById(userId));
|
|
public Task<TUser?> GetUserByIdAsync(Guid userId) => SessionAsync(x => x.GetUserById(userId));
|
|
|
|
public TUser? GetUserByEmail(string email) => Session(x => x.GetUserByEmail(email));
|
|
public Task<TUser?> GetUserByEmailAsync(string email) => SessionAsync(x => x.GetUserByEmail(email));
|
|
|
|
//public UserToken CreateUserToken(Guid userId, string verificationToken)
|
|
//{
|
|
// UserToken userToken = null;
|
|
// Transaction(ctx =>
|
|
// {
|
|
// userToken = ctx.CreateUserToken(userId, verificationToken);
|
|
// return userToken != null;
|
|
// }
|
|
// );
|
|
|
|
// return userToken;
|
|
//}
|
|
|
|
//public UserToken UpdateUserTokenSent(Guid userId, string verificationToken, DateTime tokenSentUtc)
|
|
//{
|
|
// UserToken userToken = null;
|
|
// Transaction(ctx =>
|
|
// {
|
|
// userToken = ctx.UpdateUserTokenSent(userId, verificationToken, tokenSentUtc);
|
|
// return userToken != null;
|
|
// }
|
|
// );
|
|
|
|
// return userToken;
|
|
//}
|
|
|
|
//public UserToken GetActiveUserToken(Guid userId)
|
|
// => Session(ctx => ctx.GetActiveUserToken(userId));
|
|
|
|
//public UserToken GetUserToken(Guid userId, string verificationToken)
|
|
// => Session(ctx => ctx.GetUserToken(userId, verificationToken));
|
|
|
|
//public bool IsValidToken(Guid userId, string verificationToken)
|
|
// => Transaction(ctx => ctx.IsValidToken(userId, verificationToken));
|
|
|
|
//public bool IsValidToken(string email, string verificationToken)
|
|
//{
|
|
// return Transaction(ctx =>
|
|
// {
|
|
// var user = ctx.GetUserByEmail(email);
|
|
|
|
// return user != null && ctx.IsValidToken(user.Id, verificationToken);
|
|
// });
|
|
//}
|
|
|
|
//public void DeactivateTokens(Guid userId)
|
|
//{
|
|
// Transaction(ctx =>
|
|
// {
|
|
// ctx.DeactivateTokens(userId);
|
|
// return true;
|
|
// });
|
|
//}
|
|
//public string GetValidVerificationToken(UserDal userDal, Guid userId, out UserToken userToken)
|
|
//{
|
|
// string verificationToken;
|
|
// userToken = userDal.GetActiveUserToken(userId);
|
|
|
|
// if (userToken == null)
|
|
// {
|
|
// verificationToken = AntGenerator.NewToken();
|
|
// userToken = userDal.CreateUserToken(userId, verificationToken);
|
|
// }
|
|
// else verificationToken = userToken.Token;
|
|
|
|
// return verificationToken;
|
|
//}
|
|
|
|
public AcErrorCode GetUserForEmailValidation(string email, Func<TUser?, AcErrorCode, AcErrorCode> callback)
|
|
{
|
|
TUser? user = null;
|
|
var errorCodes = AcErrorCode.Unset;
|
|
|
|
Transaction(ctx =>
|
|
{
|
|
var lowerEmail= email.ToLower();
|
|
|
|
user = ctx.Users.FirstOrDefault(x => x.EmailAddress == lowerEmail && x.EmailConfirmed);
|
|
|
|
if (user == null)
|
|
{
|
|
errorCodes = AcErrorCode.UserNameNotFound;
|
|
return false;
|
|
}
|
|
|
|
//user.Profile = ctx.Profiles.FirstOrDefault(x => x.OwnerId == user.Id);
|
|
|
|
//if (!ctx.IsUserConsentsAccepted(user.Id)) errorCodes = AcErrorCode.ConsentNotAccepted;
|
|
|
|
return true;
|
|
});
|
|
|
|
//if (user != null)
|
|
|
|
return callback(user, errorCodes);
|
|
}
|
|
}
|
|
}
|