Ejemplo n.º 1
0
        public async Task <JsonResult> CreateNewUser(PegaUser user)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ResultsItem.Error(ModelState.GetAllErrorsString())));
            }
            if (!Regex.IsMatch(user.Username, @"^[a-zA-Z0-9_\-\.]+$"))
            {
                return(Json(ResultsItem.Error("Username must contain only: Letters(A-Z), Numbers(0-9), _, -, or .")));
            }
            if (!Utilities.IsValidEmailAddress(user.Email))
            {
                return(Json("Please enter a valid email address."));
            }

            var createPair = await AuthorizationLogic.CreateNewUser(user);

            if (createPair.Result.IsSuccess)
            {
                SetSession(Constant.Session.SessionCurrentUser, createPair.Value);
                TempData["UserCreatedTransferMessage"] = createPair.Result.Message;
                return(Json(ResultsItem.Success("Success")));
            }

            return(Json(ResultsItem.Error(createPair.Result.Message)));
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> CurrentUserChangePassword(PegaUser user)
        {
            if (user.NewChangedPassword.Length < 8 || user.NewChangedPassword.Length > 30)
            {
                return(Json(ResultsItem.Error("Password changed failed: Password length must be from 8 - 30 characters.")));
            }
            if (user.NewChangedPassword != user.ConfirmNewChangedPassword)
            {
                return(Json(ResultsItem.Error("Passwords does not match.")));
            }
            if (CurrentUser.Username != user.Username)
            {
                return(Json(ResultsItem.Error("Username does not match.")));
            }

            // do-later: check if current pw is valid first. if (!AuthorizationLogic.AuthorizeUser(CurrentUser.Username, user.Password))

            // Change password
            var pwChangeResult = await AuthorizationLogic.ChangePassword(user.Username, user.NewChangedPassword);

            if (!pwChangeResult.IsSuccess)
            {
                return(Json(pwChangeResult));
            }

            HttpContext.Session.Clear();

            string successMessage = "Your password has been successfully reset. Please login again";

            TempData["message"] = successMessage;
            return(Json(ResultsItem.Success(successMessage)));
        }
Ejemplo n.º 3
0
        public static async Task <ResultsPair <Portfolio> > InsertNewPortfolio(PegaUser user, string portfolioName, Types.PortfolioDisplayType displayType, bool isDefault)
        {
            var validateRequest = IsValidDBRequest(user, 0);

            if (!validateRequest.IsSuccess)
            {
                return(ResultsPair.CreateError <Portfolio>(validateRequest.Message));
            }

            return(await CryptoRepository.InsertNewPortfolio(user.UserId, portfolioName, displayType, isDefault));
        }
Ejemplo n.º 4
0
        public static ResultsItem DeleteAPI(int apiId, PegaUser user)
        {
            var validateRequest = IsValidDBRequest(user, apiId, validateAPI: true);

            if (!validateRequest.IsSuccess)
            {
                return(validateRequest);
            }

            return(CryptoRepository.DeleteAPI(apiId));
        }
Ejemplo n.º 5
0
        public static ResultsItem InsertNewAPI(ExchangeApiInfo exchangeApiInfo, PegaUser user)
        {
            var validateRequest = IsValidDBRequest(user, 0);

            if (!validateRequest.IsSuccess)
            {
                return(validateRequest);
            }

            return(CryptoRepository.InsertNewAPI(exchangeApiInfo, user));
        }
Ejemplo n.º 6
0
        private static string GeneratePassword(Types.EncryptionType encryptionType, PegaUser user)
        {
            switch (encryptionType)
            {
            case Types.EncryptionType.ApiKey_Public: return($"QZ{string.Join("d", user.Username.Reverse())}_V638uhWV");

            case Types.EncryptionType.ApiKey_Private: return($"TZ{user.Username}V638{string.Join("b", user.CreatedDate.ToShortDateString().Replace("/", "").Reverse())}_$WV");
            }

            return(string.Empty);
        }
Ejemplo n.º 7
0
        public static async Task <ResultsItem> UpdateUserCoinAsync(CryptoCoin coin, PegaUser user)
        {
            var validateRequest = IsValidDBRequest(user, coin.PortfolioId, validatePortfolio: true);

            if (!validateRequest.IsSuccess)
            {
                return(validateRequest);
            }

            return(await CryptoRepository.UpdateUserCoinAsync(coin));
        }
Ejemplo n.º 8
0
 public static PegaUser GetDevUser(string username)
 {
     if (AppSettingsProvider.IsDevelopment)
     {
         using (EntityFramework.PegasunDBContext db = new EntityFramework.PegasunDBContext())
         {
             PegaUser user = db.Users.First(x => x.Username == username).Adapt <PegaUser>();
             user.Portfolios = db.Portfolios.Where(x => x.UserId == user.UserId).Adapt <List <Layer.Models.Coins.Portfolio> >();
             user.PTUserInfo = db.PTUserInfo.FirstOrDefault(x => x.UserId == user.UserId).Adapt <PTUserInfo>();
             return(user);
         }
     }
     return(null);
 }
Ejemplo n.º 9
0
        public void CreateUserTest()
        {
            ResultsItem deleteResult = AuthorizationLogic.DeletePegaUser("Test1234").Result;

            PegaUser user = new PegaUser
            {
                Username = "******",
                Password = "******",
                Email    = "*****@*****.**",
                IsSubscribeNewsLetter = true
            };
            ResultsPair <PegaUser> result = AuthorizationLogic.CreateNewUser(user).Result;

            Assert.IsTrue(result.Result.ResultType == Types.ResultsType.Successful);
        }
Ejemplo n.º 10
0
        private static ResultsItem IsValidDBRequest(PegaUser user, int id, bool validateAPI = false, bool validatePortfolio = false, bool validateDemoUser = true)
        {
            if (validateDemoUser && user.Username.ToUpperInvariant() == "DEMOUSER")
            {
                return(ResultsItem.Error("DemoUser is not allowed to perform this action. Please login or create an account for free."));
            }
            if (validatePortfolio && !user.HasPortfolio(id))
            {
                return(ResultsItem.Error(Lang.PortfolioNotFound));
            }
            if (validateAPI && !user.ExchangeApiList.Any(x => x.Id == id))
            {
                return(ResultsItem.Error(Lang.ApiDeleteNotFound));
            }

            return(ResultsItem.Success(string.Empty));
        }
Ejemplo n.º 11
0
        public static async Task <ResultsItem> DeletePortfolio(int portfolioId, PegaUser user)
        {
            var validateRequest = IsValidDBRequest(user, portfolioId, validatePortfolio: true);

            if (!validateRequest.IsSuccess)
            {
                return(validateRequest);
            }

            ResultsItem deleteResult = await CryptoRepository.DeletePortfolio(portfolioId);

            // Since we're deleting the only portfolio we have, create a new default one.
            if (user.Portfolios.Count == 1)
            {
                await InsertNewPortfolio(user, "Default", Types.PortfolioDisplayType.Private, true);
            }

            return(deleteResult);
        }
Ejemplo n.º 12
0
        public async Task <JsonResult> Login(PegaUser user)
        {
            ModelState.Remove("Email");
            if (!ModelState.IsValid)
            {
                return(Json(ResultsItem.Error(ModelState.GetAllErrorsString())));
            }
            if (!Regex.IsMatch(user.Username, @"^[a-zA-Z0-9_\-\.@]+$"))
            {
                return(Json(ResultsItem.Error("Username must contain only: Letters(A-Z), Numbers(0-9), _, -, ., or an email address.")));
            }

            ResultsPair <PegaUser> pair = await AuthorizationLogic.AuthorizeUser(user.Username, user.Password);

            if (pair.Result.IsSuccess)
            {
                SetSession(Constant.Session.SessionCurrentUser, pair.Value);
                return(Json(ResultsItem.Success("Success")));
            }
            return(Json(ResultsItem.Error(pair.Result.Message)));
        }
Ejemplo n.º 13
0
 public static bool IsValidUser(this PegaUser user) => user != null && user.Username != "DemoUser";
Ejemplo n.º 14
0
 public static async Task <ResultsItem> VoteBBComment(int commentId, bool isUpvote, PegaUser user)
 {
     return(await CommunityRepository.VoteBBComment(commentId, isUpvote, user));
 }
Ejemplo n.º 15
0
 public static async Task <ResultsPair <Portfolio> > ResetAllUserTrades(PegaUser user)
 {
     return(await CryptoRepository.ResetAllUserTrades(user));
 }
Ejemplo n.º 16
0
 public static async Task <ResultsItem> DeleteBBComment(int commentId, PegaUser user)
 {
     return(await CommunityRepository.DeleteBBComment(commentId, user));
 }
Ejemplo n.º 17
0
 public static async Task <ResultsItem> VoteBBThread(int threadId, bool isUpvote, PegaUser user)
 {
     return(await CommunityRepository.VoteBBThread(threadId, isUpvote, user));
 }
Ejemplo n.º 18
0
        public static async Task <CryptoCoin> GetSingleCoinByUser(long coinId, PegaUser user)
        {
            CryptoCoin coin = await CryptoRepository.GetSingleCoinByUser(coinId);

            return(!IsValidDBRequest(user, coin.PortfolioId, validatePortfolio: true, validateDemoUser: false).IsSuccess ? null : coin);
        }
Ejemplo n.º 19
0
 public static async Task <ResultsItem> CreateBBComment(BBComment comment, PegaUser user)
 {
     return(await CommunityRepository.CreateBBComment(comment, user));
 }
Ejemplo n.º 20
0
        public static async Task <BBThread> GetThreadWithComments(int threadId, PegaUser user)
        {
            BBThread thread = await CommunityRepository.GetBBThreadWithComments(threadId, user);

            return(thread);
        }
Ejemplo n.º 21
0
 public static async Task <ResultsItem> CreateBBThread(BBThread thread, PegaUser user)
 {
     return(await CommunityRepository.CreateBBThread(thread, user));
 }
Ejemplo n.º 22
0
 public static async Task <List <BBThread> > GetMessageThreads(int take, Types.ConvThreadCategory category, PegaUser user, int officialCoinId = 0)
 {
     return(await GetMessageThreads(take, new List <Types.ConvThreadCategory> {
         category
     }, user, officialCoinId));
 }
Ejemplo n.º 23
0
        public static async Task <List <BBThread> > GetMessageThreads(int take, List <Types.ConvThreadCategory> categories, PegaUser currentUser, int officialCoinId = 0)
        {
            List <BBThread> threads = await CommunityRepository.GetBBThreads(take, categories, currentUser, officialCoinId);

            return(threads);
        }
Ejemplo n.º 24
0
 public static async Task <List <Portfolio> > GetAllUserPortfolioAsync(PegaUser user)
 {
     return(await CryptoRepository.GetAllUserPortfolioAsync(user));
 }
Ejemplo n.º 25
0
 public static async Task <ResultsItem> DeleteUserCoinsAsync(List <CryptoCoin> coins, PegaUser user)
 {
     if (coins.Any(x => !IsValidDBRequest(user, x.PortfolioId, validatePortfolio: true).IsSuccess))
     {
         return(ResultsItem.Error(Lang.PortfolioNotFound));
     }
     return(await CryptoRepository.DeleteUserCoinAsync(coins));
 }
Ejemplo n.º 26
0
 public static ResultsPair <Portfolio> UpdatePortfolio(int portfolioId, string portfolioName, Types.PortfolioDisplayType displayType, bool isDefault, PegaUser user)
 {
     if (!IsValidDBRequest(user, portfolioId, validatePortfolio: true).IsSuccess)
     {
         return(ResultsPair.CreateError <Portfolio>(Lang.PortfolioNotFound));
     }
     return(CryptoRepository.UpdatePortfolio(portfolioId, portfolioName, displayType, isDefault));
 }
Ejemplo n.º 27
0
        public static async Task <ResultsItem> DeleteAllUserCoinByExchangeAsync(int portfolioId, Types.Exchanges exchange, PegaUser user)
        {
            var validateRequest = IsValidDBRequest(user, portfolioId, validatePortfolio: true);

            if (!validateRequest.IsSuccess)
            {
                return(validateRequest);
            }

            return(await CryptoRepository.DeleteAllUserCoinByExchangeAsync(portfolioId, exchange));
        }
Ejemplo n.º 28
0
 public static async Task <ResultsItem> DeleteBBThread(int threadId, PegaUser user)
 {
     return(await CommunityRepository.DeleteBBThread(threadId, user));
 }
Ejemplo n.º 29
0
        public static string Decrypt(string value, Types.EncryptionType encryptionType, PegaUser user)
        {
            SetProperSaltAndVector(encryptionType);

            switch (encryptionType)
            {
            case Types.EncryptionType.ApiKey_Public: return(Decrypt(value, GeneratePassword(encryptionType, user)));

            case Types.EncryptionType.ApiKey_Private: return(Decrypt(value, GeneratePassword(encryptionType, user)));
            }

            return(string.Empty);
        }
Ejemplo n.º 30
0
        public static async Task <ResultsItem> InsertCoinsToUserPortfolioAsync(List <CryptoCoin> coins, PegaUser user, int?portfolioId = null)
        {
            // Validation: Do not allow symbol to end in -USD.
            if (coins.Any(x => x.Symbol.ToUpperInvariant().Contains("-USD")))
            {
                if (coins.Count == 1)
                {
                    return(ResultsItem.Error("The coin symbol must not end in -USD. It should be USD-. For example: USD-ETH, USD-XRP."));
                }
                coins = coins.Where(x => !x.Symbol.ToUpperInvariant().Contains("-USD")).ToList();
            }

            if (portfolioId != null)
            {
                coins.ForEach(x => x.PortfolioId = portfolioId.Value);
            }
            if (coins.Any(x => !IsValidDBRequest(user, x.PortfolioId, validatePortfolio: true).IsSuccess))
            {
                return(ResultsItem.Error(Lang.PortfolioNotFound));
            }

            return(await CryptoRepository.InsertCoinsToUserPortfolioAsync(coins));
        }