public async Task AddUsersToBlacklist() { var user = fillTestDbHelper.Users.LastOrDefault(opt => !opt.Deleted && opt.Confirmed.GetValueOrDefault()); var expectedUsersIds = fillTestDbHelper.Users.Select(opt => opt.Id).Take(2).ToList(); var usersIds = await updateUsersService.AddUsersToBlackListAsync(expectedUsersIds, user.Id); var actual = await loadUsersService.GetUserInformationAsync(user.Id); Assert.True(actual.BlackList.Intersect(usersIds).OrderBy(id => id).SequenceEqual(usersIds.OrderBy(id => id))); }
public async Task GetUserInformation() { var expectedUser = fillTestDbHelper.Users.FirstOrDefault(); var actualUser = await loadUsersService.GetUserInformationAsync(expectedUser.Id); Assert.True(expectedUser.Id == actualUser.Id && (expectedUser.Phones != null ? expectedUser.Phones.All(phone => actualUser.Phones.Any(p => p.FullNumber == phone.PhoneNumber)) : true) && (expectedUser.Emails != null ? expectedUser.Emails.All(email => actualUser.Emails.Any(p => p == email.EmailAddress)): true)); }
public async Task <IActionResult> Details([FromQuery] long userId) { var user = await _loadUsersService.GetUserInformationAsync(userId).ConfigureAwait(false); if (user != null) { return(PartialView(user)); } return(NotFound()); }
public async Task <Response> CreateResponseAsync() { try { clientConnection.FileAccessToken = RandomExtensions.NextString(64); TokensResponse response = default; if (request.DeviceTokenId != null && !string.IsNullOrWhiteSpace(request.DeviceTokenId)) { await tokensService.SetDeviceTokenIdNullAsync(request.DeviceTokenId).ConfigureAwait(false); } UserVm user = null; switch (request.LoginType) { case LoginType.VerificationCode: { TokenVm token = null; if (request.UidType == UidType.Phone) { token = await tokensService.PhoneVCodeCreateTokenPairAsync(request.Uid, request.VCode.GetValueOrDefault(), request.DeviceTokenId).ConfigureAwait(false); } else if (request.UidType == UidType.Email) { token = await tokensService.EmailVCodeCreateTokenPairAsync(request.Uid, request.VCode.GetValueOrDefault(), request.DeviceTokenId).ConfigureAwait(false); } else if (request.UidType == UidType.UserId) { token = await tokensService.UserIdVCodeCreateTokenPairAsync(Convert.ToInt64(request.Uid), request.VCode.GetValueOrDefault(), request.DeviceTokenId).ConfigureAwait(false); } clientConnection.UserId = token.UserId; token = await tokensService.UpdateTokenDataAsync(request.OSName, request.DeviceName, request.AppName, clientConnection.ClientIP.ToString(), token).ConfigureAwait(false); clientConnection.CurrentToken = token; user = await loadUsersService.GetUserInformationAsync(token.UserId).ConfigureAwait(false); response = new TokensResponse(request.RequestId, token, clientConnection.FileAccessToken, null, user); noticeService.SendNewSessionNoticeAsync(clientConnection); } break; case LoginType.AccessTokenAndUserId: { TokenVm token = new TokenVm { UserId = request.Token.UserId, AccessToken = request.Token.AccessToken, RefreshToken = request.Token.RefreshToken, DeviceTokenId = request.DeviceTokenId }; token = await tokensService.CheckTokenAsync(token, NodeSettings.Configs.Node.Id).ConfigureAwait(false); clientConnection.UserId = token.UserId; token = await tokensService.UpdateTokenDataAsync(request.OSName, request.DeviceName, request.AppName, clientConnection.ClientIP.ToString(), token).ConfigureAwait(false); clientConnection.CurrentToken = token; user = await loadUsersService.GetUserInformationAsync(token.UserId).ConfigureAwait(false); response = new TokensResponse(request.RequestId, token, clientConnection.FileAccessToken, null, user); } break; case LoginType.Password: { ValuePair <TokenVm, string> tokenPasswordPair; if (request.UidType == UidType.Phone) { tokenPasswordPair = await tokensService.PhonePasswordCreateTokenPairAsync(request.Uid, request.Password, request.DeviceTokenId).ConfigureAwait(false); } else if (request.UidType == UidType.Email) { tokenPasswordPair = await tokensService.EmailPasswordCreateTokenPairAsync(request.Uid, request.Password, request.DeviceTokenId).ConfigureAwait(false); } else if (request.UidType == UidType.UserId) { tokenPasswordPair = await tokensService.UserIdPasswordCreateTokenPairAsync(Convert.ToInt64(request.Uid), request.Password, request.DeviceTokenId).ConfigureAwait(false); } else { var errorObject = new { UidType = "Unknown UidType." }; return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError)); } clientConnection.UserId = tokenPasswordPair.FirstValue.UserId; tokenPasswordPair.FirstValue = await tokensService.UpdateTokenDataAsync(request.OSName, request.DeviceName, request.AppName, clientConnection.ClientIP.ToString(), tokenPasswordPair.FirstValue) .ConfigureAwait(false); clientConnection.CurrentToken = tokenPasswordPair.FirstValue; user = await loadUsersService.GetUserInformationAsync(clientConnection.UserId.Value).ConfigureAwait(false); response = new TokensResponse( request.RequestId, tokenPasswordPair.FirstValue, clientConnection.FileAccessToken, tokenPasswordPair.SecondValue, user); noticeService.SendNewSessionNoticeAsync(clientConnection); } break; } clientConnection.Confirmed = user.Confirmed; clientConnection.Banned = user.Banned; connectionsService.AddOrUpdateUserConnection(clientConnection.UserId.Value, clientConnection); SendPendingMessagesAsync(); return(response); } catch (InvalidTokenException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Invalid token.", ErrorCode.InvalidAccessToken)); } catch (WrongVerificationCodeException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Invalid verification code.", ErrorCode.WrongVerificationCode)); } catch (UserNotFoundException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "User not found.", ErrorCode.UserNotFound)); } catch (CreateTokenPairException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Login failed.", ErrorCode.AuthorizationProblem)); } catch (TokensTimeoutException ex) { Logger.WriteLog(ex, request); await noticeService.SendNeedLoginNoticeAsync(clientConnection).ConfigureAwait(false); return(new ResultResponse(request.RequestId, "Refresh token expired.", ErrorCode.RefreshTokenTimeout)); } catch (UserFromAnotherNodeException ex) { await MetricsHelper.Instance.SetCrossNodeApiInvolvedAsync(request.RequestId).ConfigureAwait(false); var userToken = await nodeRequestSender.CheckTokenAsync( request.Token.UserId, request.Token, ex.NodeId.GetValueOrDefault()).ConfigureAwait(false); if (userToken != null) { clientConnection.UserId = userToken.FirstValue.UserId; clientConnection.ProxyNodeWebSocket = connectionsService.GetNodeConnection(ex.NodeId.GetValueOrDefault()).NodeWebSocket; connectionsService.AddOrUpdateUserConnection(clientConnection.UserId.Value, clientConnection); return(new TokensResponse(request.RequestId, userToken.FirstValue, clientConnection.FileAccessToken, null, userToken.SecondValue)); } return(new ResultResponse(request.RequestId, "Login failed.", ErrorCode.AuthorizationProblem)); } catch (Exception ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, null, ErrorCode.UnknownError)); } }
public async Task <Response> CreateResponseAsync() { var user = await loadUsersService.GetUserInformationAsync(clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); return(new UserResponse(request.RequestId, user)); }