Beispiel #1
0
        public async Task <AjaxResult> Create(RoleInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List <string> names = new List <string>();

            foreach (RoleInputDto dto in dtos)
            {
                Role           role   = dto.MapTo <Role>();
                IdentityResult result = await _roleManager.CreateAsync(role);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult().ToAjaxResult());
                }
                names.Add(role.Name);
            }
            return(new AjaxResult($"角色“{names.ExpandAndToString()}”创建成功"));
        }
Beispiel #2
0
        public async Task <AjaxResult> ChangePassword(ChangePasswordDto dto)
        {
            Check.NotNull(dto, nameof(dto));

            int  userId = User.Identity.GetUserId <int>();
            User user   = await _userManager.FindByIdAsync(userId.ToString());

            if (user == null)
            {
                return(new AjaxResult($"用户不存在", AjaxResultType.Error));
            }

            IdentityResult result = string.IsNullOrEmpty(dto.OldPassword)
                ? await _userManager.AddPasswordAsync(user, dto.NewPassword)
                : await _userManager.ChangePasswordAsync(user, dto.OldPassword, dto.NewPassword);

            return(result.ToOperationResult().ToAjaxResult());
        }
 public async Task<AjaxResult> Create(UserInputDto[] dtos)
 {
     Check.NotNull(dtos, nameof(dtos));
     List<string> names = new List<string>();
     foreach (var dto in dtos)
     {
         User user = dto.MapTo<User>();
         IdentityResult result = dto.Password.IsMissing()
             ? await _userManager.CreateAsync(user)
             : await _userManager.CreateAsync(user, dto.Password);
         if (!result.Succeeded)
         {
             return result.ToOperationResult().ToAjaxResult();
         }
         names.Add(user.UserName);
     }
     return new AjaxResult($"用户“{names.ExpandAndToString()}”创建成功");
 }
Beispiel #4
0
        /// <summary>
        /// 添加用户信息信息
        /// </summary>
        /// <param name="dtos">要添加的用户信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> CreateUsers(params UserInputDto[] dtos)
        {
            UserRepository.UnitOfWork.TransactionEnabled = true;
            List <string> names = new List <string>();

            foreach (UserInputDto dto in dtos)
            {
                User user = dto.MapTo <User>();
                //推荐人
                if (dto.RecommendId > 0)
                {
                    User recommenduser = await UserManager.FindByIdAsync(dto.RecommendId);

                    if (recommenduser == null)
                    {
                        return(new OperationResult(OperationResultType.QueryNull, "推荐人不存在"));
                    }
                    user.Recommend = recommenduser;
                }

                //用户扩展信息
                user.UserExtend = new UserExtend()
                {
                    User     = user,
                    HCoin    = 0,
                    RmbCoin  = 0,
                    Birthday = DateTime.Now
                };


                IdentityResult result = dto.Password.IsMissing()
                    ? await UserManager.CreateAsync(user)
                    : await UserManager.CreateAsync(user, dto.Password);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult());
                }
                names.Add(user.NickName);
            }
            return(await UserRepository.UnitOfWork.SaveChangesAsync() > 0
                ? new OperationResult(OperationResultType.Success, $"用户“{names.ExpandAndToString()}”创建成功")
                : OperationResult.NoChanged);
        }
        public async Task <AjaxResult> Delete(int[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            List <string> names = new List <string>();

            foreach (int id in ids)
            {
                User user = await _userManager.FindByIdAsync(id.ToString());

                IdentityResult result = await _userManager.DeleteAsync(user);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult().ToAjaxResult());
                }
                names.Add(user.UserName);
            }
            return(new AjaxResult($"用户“{names.ExpandAndToString()}”删除成功"));
        }
Beispiel #6
0
        public async Task <IActionResult> Delete(int[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            List <string> names = new List <string>();

            foreach (int id in ids)
            {
                Role role = await _roleManager.FindByIdAsync(id.ToString());

                IdentityResult result = await _roleManager.DeleteAsync(role);

                if (!result.Succeeded)
                {
                    return(Json(result.ToOperationResult().ToAjaxResult()));
                }
                names.Add(role.Name);
            }
            return(Json(new AjaxResult($"角色“{names.ExpandAndToString()}”删除成功", AjaxResultType.Success)));
        }
        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="dto">注册信息</param>
        /// <param name="service"></param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult <User> > Register(RegisterDto dto, IServiceProvider service)
        {
            Check.NotNull(dto, nameof(dto));
            var options = service.GetRequiredService <IOptions <ApplicationOptions> >().Value;
            //var avatarPath = $"{options.FrontendContentPath}{Path.DirectorySeparatorChar}avatars{Path.DirectorySeparatorChar}{options.Avatar.DefaultAvatar}"
            //    .ConvertToFrontendPath();

            //var avatarPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "assets", "avatars", options.Avatar.DefaultAvatar).ConvertToFrontendPath();

            User user = new User()
            {
                UserName = dto.UserName,
                NickName = dto.NickName ?? dto.UserName,
                Email    = dto.Email,
                HeadImg  = options.Avatar.DefaultAvatar
            };
            IdentityResult result = dto.Password == null ? await _userManager.CreateAsync(user) : await _userManager.CreateAsync(user, dto.Password);

            if (!result.Succeeded)
            {
                return(result.ToOperationResult(user));
            }
            UserDetail detail = new UserDetail()
            {
                RegisterIp = dto.RegisterIp,
                UserId     = user.Id,
                Profile    = "{\"personalProfile\": \"\",\"country\": \"\",\"province\": \"\",\"city\": \"\",\"address\": \"\",\"department\": \"\"}"
            };
            int count = await _userDetailRepository.InsertAsync(detail);

            //触发注册成功事件
            if (count > 0)
            {
                RegisterEventData eventData = new RegisterEventData()
                {
                    RegisterDto = dto, User = user
                };
                _eventBus.Publish(eventData);
                return(new OperationResult <User>(OperationResultType.Success, "用户注册成功", user));
            }
            return(new OperationResult <User>(OperationResultType.Error));
        }
        public async Task <AjaxResult> Update(UserInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List <string> names = new List <string>();

            foreach (var dto in dtos)
            {
                User user = await _userManager.FindByIdAsync(dto.Id.ToString());

                user = dto.MapTo(user);
                IdentityResult result = await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult().ToAjaxResult());
                }
                names.Add(user.UserName);
            }
            return(new AjaxResult($"用户“{names.ExpandAndToString()}”更新成功"));
        }
Beispiel #9
0
        public async Task <IActionResult> Update(RoleInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List <string> names = new List <string>();

            foreach (RoleInputDto dto in dtos)
            {
                Role role = await _roleManager.FindByIdAsync(dto.Id.ToString());

                role = dto.MapTo(role);
                IdentityResult result = await _roleManager.UpdateAsync(role);

                if (!result.Succeeded)
                {
                    return(Json(result.ToOperationResult().ToAjaxResult()));
                }
                names.Add(role.Name);
            }
            return(Json(new AjaxResult($"角色“{names.ExpandAndToString()}”更新成功", AjaxResultType.Success)));
        }
Beispiel #10
0
        /// <summary>
        /// 添加角色信息信息
        /// </summary>
        /// <param name="dtos">要添加的角色信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> CreateRoles(params RoleInputDto[] dtos)
        {
            RoleRepository.UnitOfWork.TransactionEnabled = true;
            List <string> names = new List <string>();

            foreach (RoleInputDto dto in dtos)
            {
                Role           role   = dto.MapTo <Role>();
                IdentityResult result = await RoleManager.CreateAsync(role);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult());
                }
                names.Add(role.Name);
            }
            return(await RoleRepository.UnitOfWork.SaveChangesAsync() > 0
                ? new OperationResult(OperationResultType.Success, $"角色“{names.ExpandAndToString()}”创建成功。")
                : OperationResult.NoChanged);
        }
Beispiel #11
0
        public async Task <AjaxResult> ConfirmEmail(ConfirmEmailDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(new AjaxResult("邮箱激活失败:参数不正确", AjaxResultType.Error));
            }
            User user = await _userManager.FindByIdAsync(dto.UserId.ToString());

            if (user == null)
            {
                return(new AjaxResult("注册邮箱激活失败:用户不存在", AjaxResultType.Error));
            }
            if (user.EmailConfirmed)
            {
                return(new AjaxResult("注册邮箱已激活,操作取消", AjaxResultType.Info));
            }
            string         code   = UrlBase64ReplaceChar(dto.Code);
            IdentityResult result = await _userManager.ConfirmEmailAsync(user, code);

            return(result.ToOperationResult().ToAjaxResult());
        }
        /// <summary>
        /// 验证用户注册
        /// </summary>
        /// <param name="dto">用户注册信息</param>
        /// <param name="validateCode">验证码</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> ValidateRegister(UserInfoRegistDto dto, string validateCode)
        {
            dto.CheckNotNull("dto");
            validateCode.CheckNotNullOrEmpty("validateCode");
            //验证码
            var severCode = GetValidateCode(dto.UserName, CodeType.用户注册);

            if (severCode == null || severCode.Code != validateCode)
            {
                return(new OperationResult(OperationResultType.ValidError, "验证码错误", 0));
            }
            if (SysUserRepo.CheckExists(p => p.UserName == dto.UserName))
            {
                return(new OperationResult(OperationResultType.ValidError, "账号已被使用", 0));
            }
            try
            {
                UserInfoRepo.UnitOfWork.TransactionEnabled = true;
                //验证密码格式
                IdentityResult result = await UserManager.PasswordValidator.ValidateAsync(dto.Password);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult());
                }

                SysUser sUser = new SysUser()
                {
                    UserName     = dto.UserName,
                    NickName     = dto.NickName,
                    Email        = dto.Email,
                    PasswordHash = UserManager.PasswordHasher.HashPassword(dto.Password),//密码加密
                    UserType     = UserType.App用户
                };
                if (severCode.ValidateType == ValidateType.手机)
                {
                    sUser.PhoneNumber          = dto.UserName;
                    sUser.PhoneNumberConfirmed = true;
                }
                else
                {
                    sUser.Email          = dto.UserName;
                    sUser.EmailConfirmed = true;
                }
                await UserManager.CreateAsync(sUser);

                var userInfo = Mapper.Map <UserInfo>(dto);
                userInfo.SysUser = sUser;
                await UserInfoRepo.InsertAsync(userInfo);

                await UserInfoRepo.UnitOfWork.SaveChangesAsync();

                var sysUser = await UserManager.FindByNameAsync(sUser.UserName);

                return(new OperationResult(OperationResultType.Success, "注册成功", sysUser));
            }
            catch
            {
                return(new OperationResult(OperationResultType.NoChanged, "注册失败", 0));
            }
        }