/// <summary> /// Maps table to entity /// </summary> /// <param name="table">Table entity to map</param> /// <param name="useLazy">Load related data data</param> public static UserCredentialTokenDomain Map(this UserCredentialToken table, bool useLazy) { UserCredentialTokenDomain result = null; if (table != null) { result = new UserCredentialTokenDomain(table.Id) { UserId = table.UserId, CreatedAt = table.CreatedAt, ExpiresOn = table.ExpiresOn, FirstAccess =table.FirstAccess }; if (useLazy) { result.User = table.User?.Map(false); } } return result; }
/// <summary> /// Maps entity to an existing table /// </summary> /// <param name="entity">Domain entity to map</param> /// <param name="table">Instance of existing table entity</param> public static UserCredentialToken Map(this UserCredentialTokenDomain entity, UserCredentialToken table) { if (entity != null && table != null) { table.UserId = entity.UserId.Value; table.CreatedAt = entity.CreatedAt; table.ExpiresOn = entity.ExpiresOn; table.FirstAccess = entity.FirstAccess; } return table; }
/// <summary> /// Maps entity to table /// </summary> /// <param name="entity">Domain entity to map</param> public static UserCredentialToken Map(this UserCredentialTokenDomain entity) { UserCredentialToken result = null; if (entity != null) { result = new UserCredentialToken(entity.Id) { UserId = entity.UserId.Value, CreatedAt = entity.CreatedAt, ExpiresOn = entity.ExpiresOn, FirstAccess = entity.FirstAccess }; } return result; }
/// <summary> /// Create or reset user credentials and password /// </summary> /// <param name="login"></param> /// <param name="firstAccess"></param> /// <param name="sendMailCallBack"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private async Task <SimpleOperationResult> SaveCredentialAsync(Guid tokenId, string login, string password, bool firstAccess, CancellationToken cancellationToken) { bool success = false; IDictionary <string, string> errors = new Dictionary <string, string>(); if (cancellationToken.IsCancellationRequested) { errors.Add("CreateCredential", _localizer["CANCELLATION_REQUESTED"]); } else { string convertedPassword = ConvertPassword(password); if (string.IsNullOrWhiteSpace(convertedPassword)) { errors.Add("Password", _localizer["PASSWORD_REQUIRED"]); } else { UserCredentialToken credentialToken = await _tokenRepository.GetByKeyAsync(tokenId, cancellationToken); if (credentialToken == null) { errors.Add("Token", _localizer["PWD_TOKEN_INVALID"]); } else { if (credentialToken.Expired()) { errors.Add("Token", _localizer["TOKEN_EXPIRED"]); } else { if (credentialToken.FirstAccess != firstAccess) { errors.Add("Token", _localizer["INVALID_TOKEN_OPERATION"]); _tokenRepository.Delete(credentialToken.Id); await _uow.SaveChangesAsync(cancellationToken); } else { User user = await _repository.GetByKeyAsync(credentialToken.User.Id, cancellationToken); if (user.Credential != null && firstAccess) { errors.Add("Credential", _localizer["CREDENTIALS_ALREADY_EXISTS"]); _tokenRepository.Delete(credentialToken.Id); await _uow.SaveChangesAsync(cancellationToken); } else if (user.Credential == null && !firstAccess) { errors.Add("Credential", _localizer["CREDENTIALS_NOT_FOUND"]); _tokenRepository.Delete(credentialToken.Id); await _uow.SaveChangesAsync(cancellationToken); } else { if (firstAccess && (!(await LoginIsAvailableAsync(user.Id, login, cancellationToken)))) { errors.Add("Login", _localizer["LOGIN_ALREADY_EXISTS"]); } else { await _uow.BeginTransactionAsync(cancellationToken); if (firstAccess) { UserCredential credential = new UserCredential() { UserId = user.Id, Login = login, Password = convertedPassword }; await _credentialRepository.AddAsync(credential, cancellationToken); } else { user.Credential.Password = convertedPassword; user.Credential.ChangeCredentials = false; user.Credential.AuthFailsQty = 0; user.Credential.LockoutUntil = null; _credentialRepository.Update(user.Id, user.Credential); } _tokenRepository.Delete(credentialToken.Id); await _uow.SaveChangesAsync(cancellationToken); if (cancellationToken.IsCancellationRequested) { await _uow.RollBackAsync(default);
/// <summary> /// Maps table to entity /// </summary> /// <param name="table">Table entity to map</param> public static UserCredentialTokenDomain Map(this UserCredentialToken table) => Map(table, true);