Exemplo n.º 1
0
        public static ResultCode Activate(string token)
        {
            try
            {
                Logger.Info("Entering AccountManager.Activate.");
                Logger.Info("Activating new account.");
                Logger.Info($"Token: {token}");

                using (var connection = new MySqlConnection(Config.ConnectionString))
                {
                    var sql = $@"select USERNAME, CREATE_TIME from user_to_activate where TOKEN = '{token}'";

                    var activateTokenModel = connection.QueryFirst<ActivateToken>(sql);

                    if (activateTokenModel == null)
                    {
                        Logger.Info($"Query has no result. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Activate.");

                        return ResultCode.InvalidToken;
                    }

                    if ((DateTime.Now - activateTokenModel.CreateTime).TotalHours >= ExpiredTime)
                    {
                        Logger.Info("Activate failed. Token expired.");
                        Logger.Info("Leaving AccountManager.Activate.");

                        sql = $"delete from user_to_activate where USERNAME = '******'";

                        if (connection.Execute(sql) == 0)
                        {
                            Logger.Info($"Executing sql query failed. Query: {sql}");
                            Logger.Info("Leaving AccountManager.Activate.");

                            return ResultCode.Failed;
                        }

                        return ResultCode.ExpiredToken;
                    }

                    sql = $"update user set IS_ACTIVATED = 1 where USERNAME = '******';";

                    if (connection.Execute(sql) == 0)
                    {
                        Logger.Info($"Executing sql query failed. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Activate.");

                        return ResultCode.Failed;
                    }

                    sql = $"delete from user_to_activate where USERNAME = '******'";

                    if (connection.Execute(sql) == 0)
                    {
                        Logger.Info($"Executing sql query failed. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Activate.");

                        return ResultCode.Failed;
                    }

                    Logger.Info("Activating succeed.");
                    Logger.Info("Leaving AccountManager.Activate.");

                    return ResultCode.Succeed;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Failed in activating. Server error.", ex);
                Logger.Info("Leaving AccountManager.Activate.");

                return ResultCode.ServerError;
            }
        }
Exemplo n.º 2
0
        public static ResultCode Reset(string token, string newPassword)
        {
            try
            {
                Logger.Info("Entering AccountManager.Reset.");
                Logger.Info("Reseting account's password.");
                Logger.Info($"Token: {token}");

                using (var connection = new MySqlConnection(Config.ConnectionString))
                {
                    var sql = $@"select USERNAME, CREATE_TIME from user_to_reset where TOKEN = '{token}'";

                    var resetToken = connection.QueryFirst<ResetToken>(sql);

                    if (resetToken == null)
                    {
                        Logger.Info($"Query has no result. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Reset.");

                        return ResultCode.InvalidToken;
                    }

                    if ((DateTime.Now - resetToken.CreateTime).TotalHours >= ExpiredTime)
                    {
                        Logger.Info("Reset failed. Token expired.");
                        Logger.Info("Leaving AccountManager.Activate.");

                        sql = $"delete from user_to_reset where USERNAME = '******'";

                        if (connection.Execute(sql) == 0)
                        {
                            Logger.Info($"Executing sql query failed. Query: {sql}");
                            Logger.Info("Leaving AccountManager.Reset.");

                            return ResultCode.Failed;
                        }

                        return ResultCode.ExpiredToken;
                    }

                    sql =
                        $"update user set PASSWORD = '******' where USERNAME = '******';";

                    if (connection.Execute(sql) == 0)
                    {
                        Logger.Info($"Executing sql query failed. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Reset.");

                        return ResultCode.Failed;
                    }

                    sql = $"delete from user_to_reset where USERNAME = '******'";

                    if (connection.Execute(sql) == 0)
                    {
                        Logger.Info($"Executing sql query failed. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Reset.");

                        return ResultCode.Failed;
                    }

                    Logger.Info("Resetting succeed.");
                    Logger.Info("Leaving AccountManager.Reset.");

                    return ResultCode.Succeed;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Failed in resetting. Server error.", ex);
                Logger.Info("Leaving AccountManager.Reset.");

                return ResultCode.ServerError;
            }
        }
Exemplo n.º 3
0
        public static ResultCode TryReset(string username, string newPassword, string email)
        {
            try
            {
                Logger.Info("Entering AccountManager.TryReset.");
                Logger.Info("Trying to reset account's password.");
                Logger.Info($"Username: {username}");
                Logger.Info($"Email: {email}");

                var sql = $"select * from user where USERNAME = '******' and EMAIL = '{email}';";

                using (var connection = new MySqlConnection(Config.ConnectionString))
                {
                    var userModel = connection.QueryFirst<User>(sql);

                    if (userModel == null)
                    {
                        Logger.Info($"Query shouldn't has result. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Register.");

                        return ResultCode.UnmatchedUsernameAndEmail;
                    }

                    var token = EncryptHelper.Encrypt(Guid.NewGuid() + username, 1);

                    sql =
                        $@"insert into user_to_reset (USERNAME, TOKEN, CREATE_TIME) values('{username}','{token}','{CurrentTime}');";

                    if (connection.Execute(sql) == 0)
                    {
                        Logger.Info($"Executing sql query failed. Query: {sql}");
                        Logger.Info("Leaving AccountManager.Register.");

                        return ResultCode.Failed;
                    }

                    MailHelper.SendMail(
                        "noreply",
                        new[] { email },
                        subject: ResetMailSubject,
                        content: string.Format(ResetMailContent, token));

                    Logger.Info("Trying to reset succeed.");
                    Logger.Info("Leaving AccountManager.TryReset.");

                    return ResultCode.Succeed;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Failed in trying to reset. Server error.", ex);
                Logger.Info("Leaving AccountManager.Register.");

                return ResultCode.ServerError;
            }
        }