예제 #1
0
        /// <summary>
        /// </summary>
        /// <param name="passwordInput"></param>
        /// <param name="checkLastPassword"></param>
        public ServiceResult ChangePassword(PasswordInput passwordInput, bool checkLastPassword = true)
        {
            var userDetail = Resolve <IUserService>().GetUserDetail(passwordInput.UserId);

            if (userDetail == null)
            {
                return(ServiceResult.Failure("您访问的用户不存在"));
            }

            var result = ServiceResult.Failed;

            if (passwordInput.Password.IsNullOrEmpty())
            {
                return(ServiceResult.Failure("密码不能为空"));
            }

            if (passwordInput.Password.Length < 6)
            {
                return(ServiceResult.Failure("密码长度不能小于6"));
            }

            if (passwordInput.Password != passwordInput.ConfirmPassword)
            {
                return(ServiceResult.Failure("确认密码与确认密码不相同"));
            }

            // 检查老密码
            if (checkLastPassword)
            {
                if (passwordInput.Type == PasswordType.LoginPassword)
                {
                    if (!passwordInput.LastPassword.ToMd5HashString()
                        .Equals(userDetail.Detail.Password, StringComparison.OrdinalIgnoreCase))
                    {
                        return(ServiceResult.Failure("原始登录密码不正确"));
                    }
                }

                if (passwordInput.Type == PasswordType.PayPassword)
                {
                    if (!passwordInput.LastPassword.ToMd5HashString().Equals(userDetail.Detail.PayPassword,
                                                                             StringComparison.OrdinalIgnoreCase))
                    {
                        return(ServiceResult.Failure("原始支付密码不正确"));
                    }
                }
            }

            if (passwordInput.Type == PasswordType.LoginPassword)
            {
                if (_userDetailRepository.ChangePassword(passwordInput.UserId,
                                                         passwordInput.Password.ToMd5HashString()))
                {
                    Resolve <IUserService>().DeleteUserCache(userDetail.Id, userDetail.UserName);
                    return(ServiceResult.Success);
                }
            }

            if (passwordInput.Type == PasswordType.PayPassword)
            {
                if (!RegexHelper.CheckPayPasswrod(passwordInput.Password))
                {
                    return(ServiceResult.Failure("支付密码必须为六位数字"));
                }

                if (_userDetailRepository.ChangePayPassword(passwordInput.UserId,
                                                            passwordInput.Password.ToMd5HashString()))
                {
                    Resolve <IUserService>().DeleteUserCache(userDetail.Id, userDetail.UserName);
                    return(ServiceResult.Success);
                }
            }

            return(result);
        }