Ejemplo n.º 1
0
        public static RequestResponse Login(string login, string password)
        {
            int tokensLimit = 5;

            login    = ValidationManager.OnlyStringsLettersDigitsSpaces(login);
            password = ValidationManager.OnlyStringsLettersDigitsSpaces(password);

            User user = User.FindByLogin(login);

            if (user == null)
            {
                return(new RequestResponse(RequestTypes.Login, ReturnCodes.FailedUserNotExist));
            }

            if (user.password != HashManager.Encrypt(password))
            {
                return(new RequestResponse(RequestTypes.Login, ReturnCodes.FailedInvalidLoginData));
            }

            int    tokensAmount = AccessToken.UserTokensCount(user.id);
            string resultToken  = HashManager.GenerateToken();

            if (tokensAmount >= tokensLimit)
            {
                var userTokens   = AccessToken.GetListByUserId(user.id);
                var updatedToken = userTokens[new Random().Next(userTokens.Count)];
                updatedToken.token = resultToken;
                updatedToken.Save();
            }
            else
            {
                var newToken = new AccessToken();
                newToken.user_id = user.id;
                newToken.token   = resultToken;
                AccessToken.Create(newToken);
            }

            var data = new JObject();

            data["token"] = resultToken;

            return(new RequestResponse(RequestTypes.Login, ReturnCodes.Success, data));
        }