213 lines
9.5 KiB
C#
213 lines
9.5 KiB
C#
using System.Diagnostics;
|
|
using AyCode.Core;
|
|
using AyCode.Database.DbContexts;
|
|
using AyCode.Database.DbContexts.Users;
|
|
using AyCode.Database.DbSets.Users;
|
|
using AyCode.Interfaces.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace AyCode.Database.DataLayers.Users
|
|
{
|
|
public static class AcUserDalExtension
|
|
{
|
|
//public static bool DeleteUser(this IUserDbContextBase ctx, IUserBase user)
|
|
//{
|
|
// if (user == null) return false;
|
|
|
|
// var players = ctx.Players.Where(x => x.OwnerId == user.Id).ToList();
|
|
|
|
// foreach (var player in players)
|
|
// {
|
|
// ctx.Profiles.RemoveRange(ctx.Profiles.Where(x => x.OwnerId == player.PlayerId || x.OwnerId == user.Id));
|
|
// ctx.Wallets.RemoveRange(ctx.Wallets.Where(x => x.OwnerId == player.PlayerId || x.OwnerId == user.Id));
|
|
|
|
// ctx.Sessions.RemoveRange(ctx.Sessions.Where(x => x.PlayerId == player.PlayerId));
|
|
// //ctx.SessionDetails.RemoveRange(ctx.SessionDetails.Where(x => x.PlayerId == player.PlayerId));
|
|
|
|
// ctx.PlayerPermissionTags.RemoveRange(player.PermissionTags);
|
|
// }
|
|
|
|
// ctx.Players.RemoveRange(players);
|
|
// ctx.UserTokens.RemoveRange(ctx.UserTokens.Where(x => x.UserId == user.Id));
|
|
// //ctx.PermissionCategory.RemoveRange(ctx.PermissionCategory.Where(x => x.ForeignKey == user.Id));
|
|
|
|
// ctx.SaveChanges(); //A Player constraint miatt kell, különben nem engedi törölni a User-t! - J.
|
|
// return ctx.Remove(user).State == EntityState.Deleted;
|
|
//}
|
|
|
|
public static TUser? GetUserById<TUser>(this IUserDbSet<TUser> ctx, Guid userId) where TUser : class, IUserBase
|
|
{
|
|
return ctx.Users.FirstOrDefault(u => u.Id == userId);
|
|
}
|
|
|
|
public static TUser? GetUserByEmail<TUser>(this IUserDbSet<TUser> ctx, string email) where TUser : class, IUserBase
|
|
{
|
|
var emailLower = email.ToLower();
|
|
return ctx.Users.FirstOrDefault(u => u.EmailAddress == emailLower);
|
|
}
|
|
|
|
//public static IEnumerable<UserToUserConsent> GetUserConsentsByUserId(this UserDbContextBase ctx, Guid userId)
|
|
// => ctx.UserToUserConsents.Where(u => u.UserId == userId);
|
|
|
|
//public static bool IsUserConsentsAccepted(this UserDbContextBase ctx, Guid userId)
|
|
// => ctx.GetUserConsentsByUserId(userId).Count(x => x.IsAccepted) == AcConst.ConsentCount;
|
|
|
|
//public static bool SetUserConsentsToAccepted(this UserDbContextBase ctx, Guid userId)
|
|
//{
|
|
// var user = ctx.GetUserById(userId);
|
|
// return ctx.SetUserConsentsToAccepted(user);
|
|
//}
|
|
|
|
//public static bool SetUserConsentsToAccepted(this UserDbContextBase ctx, IUserBase? user)
|
|
//{
|
|
// if (!user.EmailConfirmed) return false; //Ez nem kéne ide... - J.
|
|
|
|
// var hashSet = ctx.GetUserConsentsByUserId(user.Id).Select(x => x.UserConsentId).ToHashSet();
|
|
// var userConsents = ctx.UserConsents.Where(x => !hashSet.Contains(x.Id));
|
|
|
|
// ctx.AddRange(userConsents.Select(userConsent => new UserToUserConsent(user.Id, userConsent.Id, true)));
|
|
|
|
// var userToUserConsents = ctx.GetUserConsentsByUserId(user.Id).Where(x => !x.IsAccepted).ToList();
|
|
// foreach (var userToUserConsent in userToUserConsents)
|
|
// {
|
|
// userToUserConsent.IsAccepted = true;
|
|
// }
|
|
|
|
// ctx.UpdateRange(userToUserConsents);
|
|
|
|
// return true;
|
|
//}
|
|
|
|
|
|
public static bool ChangePassword<TUser, TUserToken>(this IUserDbContextBase<TUser, TUserToken> ctx, TUser? user, string passwordHash, string verificationToken)
|
|
where TUser : class, IUserBase
|
|
where TUserToken : class, IUserTokenBase
|
|
{
|
|
if (user == null || !ctx.IsValidToken(user.Id, verificationToken)) return false;
|
|
|
|
user.Password = passwordHash;
|
|
ctx.Users.Update(user);
|
|
|
|
ctx.DeactivateTokens(user.Id);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static TUserToken? UpdateUserTokenSent<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken, DateTime tokenSentUtc) where TUserToken : class, IUserTokenBase
|
|
{
|
|
var userToken = ctx.GetUserToken(userId, verificationToken);
|
|
|
|
if (userToken == null) return null;
|
|
|
|
userToken.TokenSent = tokenSentUtc;
|
|
userToken.TokenExpiration ??= tokenSentUtc.AddDays(1);
|
|
|
|
var utcNow = DateTime.UtcNow;
|
|
userToken.IsActive = tokenSentUtc <= utcNow && userToken.TokenExpiration > utcNow;
|
|
|
|
ctx.UserTokens.Update(userToken);
|
|
return userToken;
|
|
}
|
|
|
|
public static TUserToken CreateUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class, IUserTokenBase
|
|
{
|
|
var userToken = Activator.CreateInstance<TUserToken>();
|
|
userToken.UserId = userId;
|
|
userToken.Token = verificationToken;
|
|
|
|
return ctx.CreateUserToken(userToken);
|
|
}
|
|
|
|
public static TUserToken CreateUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, TUserToken userToken) where TUserToken : class, IUserTokenBase
|
|
{
|
|
ctx.DeactivateTokens(userToken.UserId);
|
|
|
|
userToken.IsActive = true;
|
|
ctx.UserTokens.Add(userToken);
|
|
|
|
return userToken;
|
|
}
|
|
|
|
public static TUserToken? GetActiveUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId) where TUserToken : class, IUserTokenBase
|
|
=> ctx.UserTokens.SingleOrDefault(x => x.UserId == userId && x.IsActive && (x.TokenExpiration == null || x.TokenExpiration > DateTime.UtcNow));
|
|
|
|
public static TUserToken? GetUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class?, IUserTokenBase?
|
|
=> ctx.UserTokens.SingleOrDefault(x => x.UserId == userId && x.Token == verificationToken);
|
|
|
|
public static bool IsValidToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class, IUserTokenBase
|
|
=> ctx.UserTokens.Any(x => x.UserId == userId && x.IsActive && x.Token == verificationToken && x.TokenExpiration > DateTime.UtcNow && x.TokenSent < DateTime.UtcNow);
|
|
|
|
public static void DeactivateTokens<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId) where TUserToken : class, IUserTokenBase
|
|
{
|
|
var activeUserTokens = ctx.UserTokens.Where(x => x.UserId == userId && x.IsActive);
|
|
|
|
foreach (var activeUserToken in activeUserTokens)
|
|
activeUserToken.IsActive = false;
|
|
|
|
ctx.UserTokens.UpdateRange(activeUserTokens);
|
|
}
|
|
|
|
////public static void GetUserDetails(this IUserDal userDal, Guid userId, Action<AspNetUser> callback)
|
|
//// => callback(userDal.Get<AspNetUser>(userId));
|
|
|
|
//public static void UsernameAndEmailAvailable(this IUserDal userDal, string username, string email, Action<bool, AcErrorCode, AspNetUser> callback)
|
|
//{
|
|
// IUserBase notConfirmedUser = null;
|
|
|
|
// var lowerEmail = email.ToLower();
|
|
// var lowerUserName = username.ToLower();
|
|
|
|
// var isAvailable = AcValidate.IsValidEmailFormat(lowerEmail, out var resultErrorCode) && AcValidate.IsValidUserNameFormat(lowerUserName, out resultErrorCode);
|
|
|
|
// if (isAvailable)
|
|
// {
|
|
// //userDal.RowCount<AspNetUser>(ctx => ctx.EmailConfirmed && (ctx.NormalizedUserName == lowerUserName || ctx.NormalizedEmail == lowerEmail)) == 0
|
|
|
|
// var users = userDal.Where<AspNetUser>(ctx => ctx.NormalizedUserName == lowerUserName || ctx.NormalizedEmail == lowerEmail).ToList();
|
|
|
|
// notConfirmedUser = users.FirstOrDefault(x => !x.EmailConfirmed && x.NormalizedEmail == lowerEmail);
|
|
|
|
// //isAvailable = (users.Count == 0 || (users.Count == 1 && notConfirmedUser != null));
|
|
|
|
// if (users.Count > 1) resultErrorCode = AcErrorCode.EmailInUse;
|
|
// else if (users.Count == 1 && notConfirmedUser == null)
|
|
// {
|
|
// resultErrorCode = users.Any(x => x.NormalizedEmail == lowerEmail) ? AcErrorCode.EmailInUse : AcErrorCode.UserNameInUse;
|
|
// }
|
|
|
|
// isAvailable = resultErrorCode == AcErrorCode.Unset;
|
|
// }
|
|
|
|
// callback(isAvailable, resultErrorCode, notConfirmedUser);
|
|
//}
|
|
|
|
|
|
//public static void AddNewUser(this IUserDal userDal, IUserBase newUser, Action<AspNetUser> callback)
|
|
//{
|
|
// userDal.Add(newUser);
|
|
// callback(newUser);
|
|
//}
|
|
|
|
//public static void AddNewUser(this IUserDal userDal, Guid userId, string username, string email, string password, Action callback)
|
|
//{
|
|
// var newUser = new AspNetUser(userId, username, email, password);
|
|
// userDal.Add(newUser);
|
|
|
|
// callback();
|
|
//}
|
|
|
|
////public static void DeleteUser(this IUserDal userDal, string username, Action callback)
|
|
////{
|
|
//// userDal.Session(ctx =>
|
|
//// {
|
|
//// var user = ctx.Users.FirstOrDefault(u => u.NormalizedUserName == username.ToUpperInvariant());
|
|
//// if (user == null) return;
|
|
|
|
//// ctx.Set<AspNetUser>().Delete(user);
|
|
//// ctx.SaveChanges();
|
|
//// };
|
|
////}
|
|
}
|
|
}
|