示例#1
0
        public async Task <CallingCallResponse> GetCountryCallingCodeAsync(

            [FromServices] IIpToLocation service, CancellationToken token)
        {
            var result = await service.GetAsync(HttpContext.GetIpAddress(), token);

            return(new CallingCallResponse(result?.CallingCode));
        }
示例#2
0
        public async Task <string> GetUserCountryAsync(CancellationToken token)
        {
            var site = _configurationService.GetSiteName();

            if (site == ConfigurationService.Site.Frymo)
            {
                return(Country.India.Name);
            }
            var cookieValue = _httpContext.HttpContext.User.Claims.FirstOrDefault(f =>
                                                                                  string.Equals(f.Type, AppClaimsPrincipalFactory.Country,
                                                                                                StringComparison.OrdinalIgnoreCase))?.Value;

            if (cookieValue != null)
            {
                return(cookieValue);
            }

            cookieValue = _httpContext.HttpContext.Request.Query["country"].FirstOrDefault();
            if (cookieValue != null && !Regex.IsMatch(cookieValue, "[A-Za-z]"))
            {
                cookieValue = null;
            }

            if (cookieValue == null)
            {
                cookieValue = _httpContext.HttpContext.Request.Cookies[CookieName];
                if (cookieValue != null)
                {
                    if (_conCountryProvider.ValidateCountryCode(cookieValue))
                    {
                        return(cookieValue);
                    }

                    cookieValue = null;
                }
            }

            if (cookieValue == null)
            {
                try
                {
                    var result = await _ipToLocation.GetAsync(_httpContext.HttpContext.GetIpAddress(),
                                                              token);

                    cookieValue = result?.CountryCode;
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"on ip location service ip is: {_httpContext.HttpContext.GetIpAddress()}");
                }


                if (cookieValue == null)
                {
                    _logger.LogError("failed to extract country code");
                    return(null);
                }
            }
            _httpContext.HttpContext.Response.Cookies.Append(CookieName, cookieValue);
            return(cookieValue);
        }
示例#3
0
        public async Task <IActionResult> RequestTutorAsync(RequestTutorRequest model,
                                                            [FromServices] IIpToLocation ipLocation,
                                                            [FromServices] TelemetryClient client,
                                                            [FromHeader(Name = "referer")] Uri referer,
                                                            [FromServices] ICountryService countryService,
                                                            CancellationToken token)
        {
            if (!_userManager.TryGetLongUserId(User, out var userId))
            {
                if (model.Email == null)
                {
                    ModelState.AddModelError("error", _stringLocalizer["Need to have email"]);

                    client.TrackTrace("Need to have email 1");
                    return(BadRequest(ModelState));
                }

                if (model.Phone == null)
                {
                    ModelState.AddModelError("error", _stringLocalizer["Need to have phone"]);
                    client.TrackTrace("Need to have phone 2");
                    return(BadRequest(ModelState));
                }
                var location = await ipLocation.GetAsync(HttpContext.GetIpAddress(), token);

                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    if (user.PhoneNumber == null)
                    {
                        var result =
                            await _userManager.SetPhoneNumberAndCountryAsync(user, model.Phone, location?.CallingCode,
                                                                             token);

                        if (result != IdentityResult.Success)
                        {
                            if (string.Equals(result.Errors.First().Code, "Duplicate",
                                              StringComparison.OrdinalIgnoreCase))
                            {
                                client.TrackTrace("Invalid Phone number");
                                ModelState.AddModelError("error", _stringLocalizer["Phone number Already in use"]);
                                return(BadRequest(ModelState));
                            }

                            client.TrackTrace("Invalid Phone number");
                            ModelState.AddModelError("error", _stringLocalizer["Invalid Phone number"]);
                            return(BadRequest(ModelState));
                        }
                    }

                    userId = user.Id;
                }
                else
                {
                    user = await _userManager.FindByPhoneAsync(model.Phone, location?.CallingCode);

                    if (user != null)
                    {
                        userId = user.Id;
                    }
                    else
                    {
                        var country = await countryService.GetUserCountryAsync(token);

                        user = new User(model.Email, model.Name, null, CultureInfo.CurrentCulture, country);

                        var createUserCommand = new CreateUserCommand(user, model.Course);
                        await _commandBus.DispatchAsync(createUserCommand, token);

                        var result =
                            await _userManager.SetPhoneNumberAndCountryAsync(user, model.Phone, location?.CallingCode,
                                                                             token);

                        if (result != IdentityResult.Success)
                        {
                            ModelState.AddModelError("error", _stringLocalizer["Invalid Phone number"]);

                            client.TrackTrace("Invalid Phone number 2");
                            return(BadRequest(ModelState));
                        }

                        userId = user.Id;
                    }
                }
            }

            try
            {
                var queryString = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(referer.Query);
                queryString.TryGetValue("utm_source", out var utmSource);
                var command = new RequestTutorCommand(model.Course,
                                                      _stringLocalizer["RequestTutorChatMessage", model.Course, model.Text ?? string.Empty],
                                                      userId,

                                                      referer.AbsoluteUri,
                                                      model.Text, model.TutorId, utmSource, model.MoreTutors);
                await _commandBus.DispatchAsync(command, token);
            }
            catch (ArgumentException)
            {
                ModelState.AddModelError("error", _stringLocalizer["You cannot request tutor to yourself"]);
                return(BadRequest(ModelState));
            }
            catch (SqlConstraintViolationException)
            {
                client.TrackTrace("Invalid Course");
                ModelState.AddModelError("error", _stringLocalizer["Invalid Course"]);
                return(BadRequest(ModelState));
            }

            if (model.TutorId.HasValue)
            {
                var query = new GetPhoneNumberQuery(model.TutorId.Value);
                var val   = await _queryBus.QueryAsync(query, token);

                return(Ok(new
                {
                    PhoneNumber = val
                }));
            }

            return(Ok());
        }