ChangePassword

This commit is contained in:
Loretta 2024-08-01 15:37:20 +02:00
parent b0eb4dbe32
commit 30c0e69144
6 changed files with 111 additions and 10 deletions

View File

@ -59,6 +59,7 @@ namespace AyCode.Database.DataLayers.Users
public virtual Task<TUser?> UpdateUserAsync(TUser user) => UpdateSafeAsync(user);
public virtual TUser? UpdateUser(TUser user) => UpdateSafe(user);
public Task<bool> AddUserAsync(TUser user)
{

View File

@ -20,4 +20,7 @@ public interface IAcLoginServiceCommon<TUser, TProfile, TCompany, TUserToService
public AcErrorCode Registration(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null);
public Task<AcErrorCode> RegistrationAsync(string email, string password, string? phoneNumber = null, Guid? referralId = null);
public Task<AcErrorCode> RegistrationAsync(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null);
public AcErrorCode ChangePassword(Guid userId, string oldPassword, string newPassword);
public Task<AcErrorCode> ChangePasswordAsync(Guid userId, string oldPassword, string newPassword);
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AyCode.Models.Users
{
public abstract class AcChangePasswordDto
{
public Guid UserId { get; set; }
public string OldPassword { get; set; }
public string NewPassword { get; set; }
protected AcChangePasswordDto() { }
protected AcChangePasswordDto(Guid userId, string oldPassword, string newPassword) : this()
{
UserId = userId;
OldPassword = oldPassword;
NewPassword = newPassword;
}
}
}

View File

@ -12,6 +12,7 @@ using AyCode.Core.Consts;
using AyCode.Core.Extensions;
using AyCode.Services.Server.Logins;
using AyCode.Utils.Extensions;
using AyCode.Core.Helpers;
namespace AyCode.Services.Server.Tests.LoginServices
{
@ -19,7 +20,7 @@ namespace AyCode.Services.Server.Tests.LoginServices
where TDal : AcUserDalBase<TDbContext, TUser, TProfile, TUserToken, TCompany, TUserToServiceProvider, TProfileAddress, TEmailMessage>
where TDbContext : AcDbContextBase, IAcUserDbContextBase<TUser, TProfile, TUserToken, TCompany, TUserToServiceProvider, TProfileAddress, TEmailMessage>
where TLoginServiceServer : class, IAcLoginServiceServer<TResultLoggedInModel, TUser, TUserToken, TProfile, TCompany, TUserToServiceProvider, TProfileAddress>
where TResultLoggedInModel: class, IAcLoggedInModelBase<TUser, TUserToken, TProfile, TCompany, TUserToServiceProvider, TProfileAddress>
where TResultLoggedInModel : class, IAcLoggedInModelBase<TUser, TUserToken, TProfile, TCompany, TUserToServiceProvider, TProfileAddress>
where TUser : class, IAcUser<TProfile, TCompany, TUserToServiceProvider, TProfileAddress>
where TProfile : class, IAcProfile<TProfileAddress>
where TProfileAddress : class, IAcAddress
@ -65,6 +66,7 @@ namespace AyCode.Services.Server.Tests.LoginServices
Assert.IsNotNull(loginService);
#region Valid email+password test
var loggedInModel = loginService.Login(loginEmail, loginPassword);
Assert.IsNotNull(loggedInModel);
@ -74,23 +76,57 @@ namespace AyCode.Services.Server.Tests.LoginServices
Assert.IsTrue(loggedInModel.LoginErrorCode == AcErrorCode.Unset, $"errorCode: {loggedInModel.LoginErrorCode}");
Assert.IsTrue(loggedInModel.IsLoggedIn, $"loggedInModel.IsLoggedIn == false; errorCode: {loggedInModel.LoginErrorCode}");
Assert.IsTrue(string.Equals(loggedInModel.LoggedInUser.EmailAddress, loginEmail, StringComparison.CurrentCultureIgnoreCase));
#endregion Valid email+password test
#region Wrong email test
loggedInModel = loginService.Login("gffsdgdfg@gu.hu", loginPassword);
Assert.IsNotNull(loggedInModel);
Assert.IsFalse(loggedInModel.IsLoggedIn);
Assert.IsTrue(loggedInModel.LoginErrorCode == AcErrorCode.WrongLoginData);
#endregion Wrong email test
#region Wrong password test
loggedInModel = loginService.Login(loginEmail, "fsdgfsdg");
Assert.IsNotNull(loggedInModel);
Assert.IsFalse(loggedInModel.IsLoggedIn);
Assert.IsTrue(loggedInModel.LoginErrorCode == AcErrorCode.WrongLoginData);
#endregion Wrong password test
}
[DataTestMethod]
[DataRow(["", "", "", ""])]
public virtual void AcBase_ChangePassword_ReturnUser_WhenUserLoggedInWithNewPassword(string[] userIdOldPasswordNewPasswordDbBackupHashStrings)
{
var userId = Guid.Parse(userIdOldPasswordNewPasswordDbBackupHashStrings[0]);
var oldPassword = userIdOldPasswordNewPasswordDbBackupHashStrings[1];
var newPassword = userIdOldPasswordNewPasswordDbBackupHashStrings[2];
var oldPasswordBackupHash = userIdOldPasswordNewPasswordDbBackupHashStrings[3];
var user = Dal.GetUserById(userId, false)!;
//Visszaállítjuk az eredeti jelszót... - J.
if (!PasswordHasher.VerifyPassword(oldPassword, user.Password, PasswordHasher.GenerateDynamicSalt(userId)))
{
user.Password = oldPasswordBackupHash;
Dal.UpdateUser(user);
}
var loginService = Activator.CreateInstance(typeof(TLoginServiceServer), Dal, AcEnv.AppConfiguration) as TLoginServiceServer;
Assert.IsNotNull(loginService);
var errorCode = loginService.ChangePassword(userId, oldPassword, newPassword);
Assert.IsTrue(errorCode == AcErrorCode.Unset, $"{errorCode}");
var loggedInModel = loginService.Login(user.EmailAddress, newPassword);
Assert.IsNotNull(loggedInModel);
Assert.IsTrue(loggedInModel.IsLoggedIn);
}
}
}

View File

@ -98,8 +98,8 @@ public class AcLoginServiceServer<TResultLoggedInModel, TDal, TDbContext, TUser,
user.EmailAddress = email;
user.EmailConfirmed = true;
user.Password = PasswordHasher.HashPassword(password, PasswordHasher.GenerateDynamicSalt(userId));
if(referralId != null)
user.RefferalId = referralId;
user.RefferalId = referralId;
var address = Activator.CreateInstance<TProfileAddress>();
address.Id = Guid.NewGuid();
@ -117,6 +117,33 @@ public class AcLoginServiceServer<TResultLoggedInModel, TDal, TDbContext, TUser,
public virtual Task<AcErrorCode> RegistrationAsync(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null)
=> TaskHelper.ToThreadPoolTask(() => Registration(userId, email, password, phoneNumber, referralId));
public virtual AcErrorCode ChangePassword(Guid userId, string oldPassword, string newPassword)
{
try
{
if (userId.IsNullOrEmpty()) return AcErrorCode.IdIsNullOrEmpty;
if (!AcValidate.IsValidPasswordFormat(newPassword, out var errorCode)) return errorCode;
var user = userDal.GetUserById(userId, false); //TODO: csak az EmailConfirmed user password-öket lehessen change-elni! - J.
if (user == null) return AcErrorCode.EntityIsNull;
if (!PasswordHasher.VerifyPassword(oldPassword, user.Password, PasswordHasher.GenerateDynamicSalt(user.Id))) return AcErrorCode.WrongLoginData;
user.Password = PasswordHasher.HashPassword(newPassword, PasswordHasher.GenerateDynamicSalt(userId));
return userDal.UpdateUser(user) == null ? AcErrorCode.DatabaseError : AcErrorCode.Unset;
}
catch (Exception)
{
// ignored
}
return AcErrorCode.UnknownError;
}
public virtual Task<AcErrorCode> ChangePasswordAsync(Guid userId, string oldPassword, string newPassword)
=> TaskHelper.ToThreadPoolTask(() => ChangePassword(userId, oldPassword, newPassword));
public virtual bool SendConfirmationToken(string? email, string confirmationToken)
{
//var sendGrid = SendGrid.SendGridClient();

View File

@ -42,7 +42,7 @@ public class AcLoginServiceClient<TUser, TProfile, TCompany, TUserToServiceProvi
throw new NotImplementedException();
}
public AcErrorCode Registration(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null)
public virtual AcErrorCode Registration(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null)
{
throw new NotImplementedException();
}
@ -52,7 +52,17 @@ public class AcLoginServiceClient<TUser, TProfile, TCompany, TUserToServiceProvi
return TaskHelper.ToThreadPoolTask(() => Registration(email, password, phoneNumber));
}
public Task<AcErrorCode> RegistrationAsync(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null)
public virtual Task<AcErrorCode> RegistrationAsync(Guid userId, string email, string password, string? phoneNumber = null, Guid? referralId = null)
{
throw new NotImplementedException();
}
public virtual AcErrorCode ChangePassword(Guid userId, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}
public virtual Task<AcErrorCode> ChangePasswordAsync(Guid userId, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}