Example #1
0
        public string UpdatePreferences([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_PREFERENCES_CURRENCY))
                {
                    var currencyKey = value[ParseDataKeyApi.KEY_USER_UPDATE_PREFERENCES_CURRENCY].ToString();
                    if (!string.IsNullOrEmpty(currencyKey) &&
                        PaymentCurrency.LIST_CURRENCY.ContainsKey(currencyKey))
                    {
                        userModel.CurrencyKey = currencyKey;
                    }
                    else
                    {
                        return(CreateDataError("Currency Key is not exist"));
                    }
                }

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_PREFERENCES_TIMEZONE))
                {
                    var timezoneKey = value[ParseDataKeyApi.KEY_USER_UPDATE_PREFERENCES_TIMEZONE].ToString();
                    if (!string.IsNullOrEmpty(timezoneKey) && Timezone.LIST_TIME_ZONE.ContainsKey(timezoneKey))
                    {
                        userModel.TimezoneKey = timezoneKey;
                    }
                    else
                    {
                        return(CreateDataError("Timezone Key is not exist"));
                    }
                }

                _userBusiness.UpdateProfile(userModel);

                //save action log
                return(_userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                                  ActionLog.UPDATE_PREFERENCES,
                                                  HelpersApi.GetIp(Request)).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.USER_UPDATE_PREFERENCES + e);
                return(CreateDataError(e.Message));
            }
        }
Example #2
0
        public string UpdateUserProfile([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_ADDRESS_1))
                {
                    userModel.StreetAddress1 = value[ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_ADDRESS_1].ToString();
                }

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_ADDRESS_2))
                {
                    userModel.StreetAddress2 = value[ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_ADDRESS_2].ToString();
                }

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_CITY))
                {
                    userModel.City = value[ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_CITY].ToString();
                }

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_POSTAL_CODE))
                {
                    userModel.PostalCode = value[ParseDataKeyApi.KEY_USER_UPDATE_PROFILE_POSTAL_CODE].ToString();
                }

                _userBusiness.UpdateProfile(userModel);

                //save action log
                return(_userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                                  ActionLog.UPDATE_PROFILE,
                                                  HelpersApi.GetIp(Request)).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.USER_UPDATE + e);
                return(CreateDataError(e.Message));
            }
        }
Example #3
0
        public string UpdateNotifications([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (value.ContainsKey(ParseDataKeyApi.KEY_USER_UPDATE_NOTIFICATION))
                {
                    userModel.Notifications = value[ParseDataKeyApi.KEY_USER_UPDATE_NOTIFICATION].ToString();
                }

                _userBusiness.UpdateProfile(userModel);

                return(_userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                                  ActionLog.UPDATE_NOTIFICATION,
                                                  HelpersApi.GetIp(Request)).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.USER_UPDATE_NOTIFICATION + e);
                return(CreateDataError(e.Message));
            }
        }
Example #4
0
        public async Task <string> UploadFile()
        {
            try
            {
                var file = Request.Form.Files[0];


                var userCheck = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (file.Length > 2097152)
                {
                    return(CreateDataError("File max size 2Mb"));
                }

                if (file.Length <= 0)
                {
                    return(CreateDataError("Can't update image"));
                }


                using (var w = new WebClient())
                {
                    w.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                    try
                    {
                        using (var m = new MemoryStream())
                        {
                            file.CopyTo(m);
                            m.Close();
                            // Convert byte[] to Base64 String
                            var base64String = Convert.ToBase64String(m.GetBuffer());

                            var values = new NameValueCollection
                            {
                                { ParseDataKeyApi.KEY_USER_UPDATE_AVATAR, base64String }
                            };

                            w.Headers.Add("Authorization", "Client-ID " + AppSettingHelper.GetImgurApiKey());

                            byte[] response = await w.UploadValuesTaskAsync(AppSettingHelper.GetImgurUrl(), values);

                            var result = JsonHelper.DeserializeObject <JObject>(Encoding.UTF8.GetString(response));

                            if (!(bool)result["success"])
                            {
                                return(CreateDataError("Save image fail"));
                            }
                            userCheck.Avatar = result["data"]["link"].ToString();
                            var updateUser = _userBusiness.UpdateProfile(userCheck);

                            if (updateUser.Status != Status.STATUS_SUCCESS)
                            {
                                return(CreateDataError("Can't update image"));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }

                    //save action log
                    _userBusiness.AddActionLog(userCheck.Email, userCheck.Id,
                                               ActionLog.AVATAR,
                                               HelpersApi.GetIp(Request));

                    return(new ReturnObject
                    {
                        Status = Status.STATUS_SUCCESS,
                        Message = "Upload avatar success ",
                        Data = userCheck.Avatar
                    }.ToJson());
                }
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.USER_AVATAR + e);
                return(CreateDataError(e.Message));
            }
        }
Example #5
0
        public async Task <string> GetCurrentUser()
        {
            try
            {
                var email = User.Claims.Where(c => c.Type == ClaimTypes.Email).Select(c => c.Value)
                            .SingleOrDefault();

                if (string.IsNullOrEmpty(email))
                {
                    return(CreateDataError("Can't not email"));
                }

                var query = new Dictionary <string, string> {
                    { "Email", email }
                };


                var userModel = _userBusiness.GetUserInfo(query);

                var ip = HelpersApi.GetIp(Request);
                IpGeographicalLocation location = null;
                if (!string.IsNullOrEmpty(ip))
                {
                    //get location for ip
                    location = await IpGeographicalLocation.QueryGeographicalLocationAsync(ip);
                }

                if (userModel == null)
                {
                    var jsonUser = User.Claims.Where(c => c.Type == "userInfo").Select(c => c.Value)
                                   .SingleOrDefault();

                    var userClaims = Vakapay.Models.Entities.User.FromJson(jsonUser);
                    userClaims.Notifications = "1,2,3";
                    if (location != null)
                    {
                        if (location.Currency?.Code != null)
                        {
                            userClaims.CurrencyKey = location.Currency.Code;
                        }

                        if (location.TimeZone?.Id != null)
                        {
                            userClaims.TimezoneKey = location.TimeZone.Id;
                        }
                    }

                    var resultData = _userBusiness.Login(userClaims);

                    if (resultData.Status == Status.STATUS_ERROR)
                    {
                        return(CreateDataError(resultData.Message));
                    }


                    userModel = Vakapay.Models.Entities.User.FromJson(resultData.Data);


                    return(_walletBusiness.MakeAllWalletForNewUser(userModel).ToJson());
                }

                if (string.IsNullOrEmpty(ip))
                {
                    return new ReturnObject
                           {
                               Status = Status.STATUS_SUCCESS,
                               Data   = Vakapay.Models.Entities.User.ToJson(userModel)
                           }
                }
                .ToJson();

                UpdateCurrencyAndTimeZone(userModel, location);

                var browser = HelpersApi.GetBrowser(Request);

                var confirmedDevices = new ConfirmedDevices
                {
                    Browser  = browser,
                    Ip       = ip,
                    Location = location != null && !string.IsNullOrEmpty(location.CountryName)
                        ? location.City + "," + location.CountryName
                        : "localhost",
                    UserId = userModel.Id
                };

                var search = new Dictionary <string, string> {
                    { "Ip", ip }, { "Browser", browser }
                };


                //save devices
                var checkConfirmedDevices = _userBusiness.GetConfirmedDevices(search);
                if (checkConfirmedDevices == null)
                {
                    _userBusiness.SaveConfirmedDevices(confirmedDevices);
                }


                userModel.SecretAuthToken = null;
                userModel.TwoFactorSecret = null;
                userModel.SecondPassword  = null;
                userModel.Id          = null;
                userModel.PhoneNumber = !string.IsNullOrEmpty(userModel.PhoneNumber)
                    ? "*********" + userModel.PhoneNumber.Remove(0, 9)
                    : null;
                if (userModel.Birthday.Contains("1900-01-01"))
                {
                    userModel.Birthday = null;
                }

                return(new ReturnObject
                {
                    Status = Status.STATUS_SUCCESS,
                    Data = userModel.ToJson()
                }.ToJson());
            }
        public string GetConfirmedDevices()
        {
            try
            {
                var queryStringValue = Request.Query;

                if (!queryStringValue.ContainsKey("offset") || !queryStringValue.ContainsKey("limit"))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                StringValues sort;
                StringValues filter;
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_OFFSET, out var offset);
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_LIMIT, out var limit);
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER, out filter);
                }
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT, out sort);
                }

                sort = ConvertSortDevice(sort);

                var ip = HelpersApi.GetIp(Request);

                var checkConfirmedDevices = new ConfirmedDevices();

                if (!string.IsNullOrEmpty(ip))
                {
                    var browser = HelpersApi.GetBrowser(Request);

                    var search = new Dictionary <string, string> {
                        { "Ip", ip }, { "Browser", browser }
                    };

                    //save web session
                    checkConfirmedDevices = _userBusiness.GetConfirmedDevices(search);
                }

                var userModel = (User)RouteData.Values["UserModel"];

                int numberData;
                var resultDevice = _userBusiness.GetListConfirmedDevices(out numberData, userModel.Id,
                                                                         checkConfirmedDevices,
                                                                         Convert.ToInt32(offset),
                                                                         Convert.ToInt32(limit), sort, filter);

                if (resultDevice.Status != Status.STATUS_SUCCESS)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.DATA_NOT_FOUND));
                }

                var listDevice = JsonHelper.DeserializeObject <List <ConfirmedDevices> >(resultDevice.Data);
                return(new ReturnObject
                {
                    Status = Status.STATUS_SUCCESS,
                    Data = new ResultList <ConfirmedDevices>
                    {
                        List = listDevice,
                        Total = numberData
                    }.ToJson()
                }.ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.DEVICE_LIST + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Example #7
0
        public string UpdateOption([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION_CODE].ToString();
                }


                bool isVerify = false;

                switch (userModel.IsTwoFactor)
                {
                case 1:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    break;

                case 2:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    var secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);
                    if (string.IsNullOrEmpty(secretAuthToken.UpdateOptionVerification))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                    }

                    isVerify = HelpersApi.CheckCodeSms(secretAuthToken.UpdateOptionVerification, code, userModel);
                    break;

                case 0:
                    isVerify = true;
                    break;
                }


                if (!isVerify)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                }

                var option = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION];

                userModel.Verification = (int)option;

                _userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                           ActionLog.UPDATE_OPTION_VETIFY,
                                           HelpersApi.GetIp(Request));

                return(_userBusiness.UpdateProfile(userModel).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_OPTION_UPDATE + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Example #8
0
        public string CustomTwo([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];
                if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_STATUS))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var status = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_STATUS];


                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_PASS_DATA_GET_CODE].ToString();
                }
                var token = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_GOOGLE_TOKEN))
                {
                    token = value[ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_GOOGLE_TOKEN].ToString();
                }


                bool       isVerify = false;
                ActionCode secretAuthToken;

                switch (userModel.IsTwoFactor)
                {
                case 1:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }


                    if ((int)status == 2)
                    {
                        secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);

                        if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                        }


                        isVerify = HelpersApi.CheckCodeSms(secretAuthToken.CustomTwofa, code, userModel);
                    }
                    else if ((int)status == 0)
                    {
                        isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    }

                    break;

                case 2:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    if ((int)status == 0)
                    {
                        secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);

                        if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                        }

                        isVerify = HelpersApi.CheckCodeSms(secretAuthToken.CustomTwofa, code, userModel);
                    }
                    else if ((int)status == 1)
                    {
                        isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    }

                    break;

                case 0:
                    if ((int)status == 1)
                    {
                        if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                        }
                        Console.WriteLine(userModel.TwoFactorSecret);

                        isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    }
                    else if ((int)status == 2)
                    {
                        if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                        }

                        secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);
                        if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                        }

                        isVerify = HelpersApi.CheckCodeSms(secretAuthToken.CustomTwofa, code, userModel);
                    }

                    break;
                }

                Console.WriteLine(isVerify);

                if (!isVerify)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                }

                userModel.IsTwoFactor = (int)status;

                _userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                           ActionLog.TWOFA_ENABLE,
                                           HelpersApi.GetIp(Request));

                return(_userBusiness.UpdateProfile(userModel).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_ENABLE_UPDATE + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }