예제 #1
0
        protected virtual async Task CreateUserAsync(CreateOrUpdateUserInput input)
        {
            User tenantId = input.User.MapTo <User>();

            tenantId.TenantId = this.AbpSession.TenantId;
            if (input.User.Password.IsNullOrEmpty())
            {
                input.User.Password = User.CreateRandomPassword();
            }
            else
            {
                IdentityResult identityResult = await this.UserManager.PasswordValidator.ValidateAsync(input.User.Password);

                this.CheckErrors(identityResult);
            }
            tenantId.Password = (new PasswordHasher()).HashPassword(input.User.Password);
            tenantId.ShouldChangePasswordOnNextLogin = input.User.ShouldChangePasswordOnNextLogin;
            tenantId.Roles = new Collection <UserRole>();
            bool flag = false;

            string[] assignedRoleNames = input.AssignedRoleNames;
            for (int i = 0; i < (int)assignedRoleNames.Length; i++)
            {
                string str             = assignedRoleNames[i];
                Role   roleByNameAsync = await this._roleManager.GetRoleByNameAsync(str);

                ICollection <UserRole> roles = tenantId.Roles;
                roles.Add(new UserRole()
                {
                    RoleId = roleByNameAsync.Id
                });
                if (roleByNameAsync.DisplayName == this.L("KeyName_CustomersRole"))
                {
                    flag = true;
                }
            }
            assignedRoleNames = null;
            this.CheckErrors(await this.UserManager.CreateAsync(tenantId));
            await this.CurrentUnitOfWork.SaveChangesAsync();

            if (flag)
            {
                IRepository <Customer, long> repository = this._customerRepository;
                List <Customer> allListAsync            = await repository.GetAllListAsync((Customer m) => (int?)m.TenantId == tenantId.TenantId && m.UserId.HasValue && m.UserId == (long?)tenantId.Id);

                if (!allListAsync.Any <Customer>())
                {
                    Customer customer = new Customer()
                    {
                        AllowBillPay = false,
                        BusinessName = null,
                        FirstName    = tenantId.Name,
                        LastName     = tenantId.Surname,
                        Email        = tenantId.EmailAddress,
                        IsActive     = true,
                        PaymentAssistanceParticipant = false,
                        TitleId  = null,
                        UserId   = new long?(tenantId.Id),
                        TenantId = tenantId.TenantId.Value
                    };
                    await this._customerRepository.InsertAsync(customer);

                    await this.CurrentUnitOfWork.SaveChangesAsync();
                }
            }
            if (input.SendActivationEmail)
            {
                tenantId.SetNewEmailConfirmationCode();
                await this._userEmailer.SendEmailActivationLinkAsync(tenantId, input.User.Password);
            }
        }