Esempio n. 1
0
        protected virtual async Task CreateUserAsync(CreateOrUpdateUserInput input)
        {
            //if (AbpSession.TenantId.HasValue)
            //{
            //    await _userPolicy.CheckMaxUserCountAsync(AbpSession.GetTenantId());
            //}

            var user = input.User.MapTo <User>(); //Passwords is not mapped (see mapping configuration)

            user.TenantId = AbpSession.TenantId;

            //if (!input.User.Password.IsNullOrEmpty())
            {
                CheckErrors(await UserManager.PasswordValidator.ValidateAsync(input.User.Password));
            }
            //else
            //{
            //    input.User.Password = User.CreateRandomPassword();
            //}

            user.Password = new PasswordHasher().HashPassword(input.User.Password);
            user.ShouldChangePasswordOnNextLogin = input.User.ShouldChangePasswordOnNextLogin;

            //Assign roles
            user.Roles = new Collection <UserRole>();
            foreach (var roleName in input.AssignedRoleNames)
            {
                var role = await _roleManager.GetRoleByNameAsync(roleName);

                user.Roles.Add(new UserRole(AbpSession.TenantId, user.Id, role.Id));
            }
            if (!string.IsNullOrWhiteSpace(user.PhoneNumber) && (await UserManager.FindUserByPhoneNumberAsync(user.PhoneNumber)) != null)
            {
                throw new UserFriendlyException("当前手机号已被注册,创建用户失败");
            }
            CheckErrors(await UserManager.CreateAsync(user));
            await CurrentUnitOfWork.SaveChangesAsync(); //To get new user's Id.

            await SetOrganization(user, input.Organizations);

            //Notifications
            await _notificationSubscriptionManager.SubscribeToAllAvailableNotificationsAsync(user.ToUserIdentifier());

            await _appNotifier.WelcomeToTheApplicationAsync(user);

            try
            {
                if (input.SendActivationEmail && !string.IsNullOrWhiteSpace(input.User.EmailAddress))
                {
                    var body = $"您好,系统已为您创建账号,用户名:{input.User.UserName}, 验证码:{input.User.Password},感谢您使用本系统";
                    if (!input.User.IsActive)
                    {
                        body = $"您好,系统已为您创建账号,用户名:{input.User.UserName}, 验证码:{input.User.Password}," +
                               "首次登陆需要激活帐号,感谢您使用本系统。";
                    }
                    var subject = "账号创建通知";
                    await _emailSender.SendAsync(input.User.EmailAddress, subject, body);
                }
                if (input.SendActivationMessage && !string.IsNullOrWhiteSpace(input.User.PhoneNumber))
                {
                    var body = $"您好,系统已为您创建账号,用户名:{input.User.UserName}, 密码:{input.User.Password},感谢您使用本系统";
                    if (!input.User.IsActive)
                    {
                        body = $"您好,系统已为您创建账号,用户名:{input.User.UserName}, 密码:{input.User.Password},首次登陆需要激活帐号,感谢您使用本系统。";
                    }
                    await _smsSender.Sender(input.User.PhoneNumber, body);
                }
            }
            catch (Exception ex)
            {
                //ignore
            }
        }
Esempio n. 2
0
        public override async Task <UserDto> CreateAsync(CreateUserDto input)
        {
            CheckCreatePermission();

            var user = ObjectMapper.Map <User>(input);

            if (input.RoleTypeList != null && input.RoleTypeList.Count > 0)
            {
                user.InitRoleType = input.RoleTypeList[0];  //初始角色类型
            }

            user.TenantId         = AbpSession.TenantId;
            user.IsEmailConfirmed = true;

            await _userManager.InitializeOptionsAsync(AbpSession.TenantId);

            CheckErrors(await _userManager.CreateAsync(user, input.Password));

            if (input.RoleNames != null)
            {
                CheckErrors(await _userManager.SetRolesAsync(user, input.RoleNames));
            }

            CurrentUnitOfWork.SaveChanges();


            await SetOrganization(user, input.Organizations);

            //Notifications
            await _notificationSubscriptionManager.SubscribeToAllAvailableNotificationsAsync(user.ToUserIdentifier());

            await _appNotifier.WelcomeToTheApplicationAsync(user);

            try
            {
                if (input.SendActivationEmail && !string.IsNullOrWhiteSpace(input.EmailAddress))
                {
                    var body = $"您好,系统已为您创建账号,用户名:{input.UserName}, 验证码:{input.Password},感谢您使用本系统";
                    if (!input.IsActive)
                    {
                        body = $"您好,系统已为您创建账号,用户名:{input.UserName}, 验证码:{input.Password}," +
                               "首次登陆需要激活帐号,感谢您使用本系统。";
                    }
                    var subject = "账号创建通知";
                    await _emailSender.SendAsync(input.EmailAddress, subject, body);
                }
                if (input.SendActivationMessage && !string.IsNullOrWhiteSpace(input.PhoneNumber))
                {
                    var body = $"您好,系统已为您创建账号,用户名:{input.UserName}, 密码:{input.Password},感谢您使用本系统";
                    if (!input.IsActive)
                    {
                        body = $"您好,系统已为您创建账号,用户名:{input.UserName}, 密码:{input.Password},首次登陆需要激活帐号,感谢您使用本系统。";
                    }
                    await _smsSender.Sender(input.PhoneNumber, body);
                }
            }
            catch (Exception)
            {
                //ignore
            }
            return(MapToEntityDto(user));
        }