Exemplo n.º 1
0
        public async Task <OrganizationUnitDto> CreateOrganizationUnit(CreateOrganizationUnitInput input)
        {
            var user = await UserManager.GetUserByIdAsync(input.UserId);

            //2017.10.17:修改组织为宿主级
            var organizationUnit = new OrganizationUnit(null, input.DisplayName, input.ParentId);

            await _organizationUnitManager.CreateAsync(organizationUnit);

            await CurrentUnitOfWork.SaveChangesAsync();

            //todo:生产租户信息
            var tenantId = await TenantManager.CreateWithAdminUserAsync(input.DisplayName,
                                                                        input.DisplayName,
                                                                        organizationUnit.Id,
                                                                        null,
                                                                        user.EmailAddress,
                                                                        null,
                                                                        true,
                                                                        input.EditionId,
                                                                        true,
                                                                        false,
                                                                        null,
                                                                        false,
                                                                        _appUrlService.CreateEmailActivationUrlFormat(input.DisplayName)
                                                                        );

            var result = ObjectMapper.Map <OrganizationUnitDto>(organizationUnit);

            result.TenantId = tenantId;
            return(result);
        }
Exemplo n.º 2
0
        protected virtual async Task UpdateUserAsync(CreateOrUpdateUserInput input)
        {
            Debug.Assert(input.User.Id != null, "input.User.Id should be set.");

            var user = await UserManager.FindByIdAsync(input.User.Id.Value.ToString());

            //更新用户信息
            ObjectMapper.Map(input.User, user); //密码不会被映射(请查看映射设置)

            if (input.SetRandomPassword)
            {
                input.User.Password = User.CreateRandomPassword();
            }

            if (!input.User.Password.IsNullOrEmpty())
            {
                await UserManager.InitializeOptionsAsync(AbpSession.TenantId);

                CheckErrors(await UserManager.ChangePasswordAsync(user, input.User.Password));
            }

            CheckErrors(await UserManager.Update4PlatformAsync(user));

            //更新用户角色
            CheckErrors(await UserManager.SetRoles(user, input.AssignedRoleNames));

            //更新用户权限
            var grantedPermissions = PermissionManager.GetPermissionsFromNamesByValidating(input.GrantedPermissionNames);
            await UserManager.SetGrantedPermissionsAsync(user, grantedPermissions);

            //发送激活邮件
            if (!input.SendActivationEmail || user.EmailAddress.IsNullOrWhiteSpace())
            {
                return;
            }
            user.SetNewEmailConfirmationCode();
            await _userEmailer.SendEmailActivationLinkAsync(
                user,
                _appUrlService.CreateEmailActivationUrlFormat(AbpSession.TenantId),
                input.User.Password
                );
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发送激活邮件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SendEmailActivationLink(SendEmailActivationLinkInput input)
        {
            var user = await GetUserByChecking(input.EmailAddress);

            if (user.EmailAddress.IsNullOrWhiteSpace())
            {
                return;
            }

            user.SetNewEmailConfirmationCode();
            await _userEmailer
            .SendEmailActivationLinkAsync(user, _appUrlService.CreateEmailActivationUrlFormat(AbpSession.TenantId));
        }
Exemplo n.º 4
0
 public async Task CreateTenant(CreateTenantInput input)
 {
     await TenantManager.CreateWithAdminUserAsync(input.TenancyName,
                                                  input.Name,
                                                  input.AdminPassword,
                                                  input.AdminEmailAddress,
                                                  input.PhoneNumber,
                                                  input.ConnectionString,
                                                  input.IsActive,
                                                  input.EditionId,
                                                  input.ShouldChangePasswordOnNextLogin,
                                                  input.SendActivationEmail,
                                                  input.SubscriptionEndDateUtc?.ToUniversalTime(),
                                                  input.IsInTrialPeriod,
                                                  _appUrlService.CreateEmailActivationUrlFormat(input.TenancyName)
                                                  );
 }
Exemplo n.º 5
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            try
            {
                if (!model.IsExternalLogin && UseCaptchaOnRegistration())
                {
                    await _recaptchaValidator.ValidateAsync(HttpContext.Request.Form[RecaptchaValidator.RecaptchaResponseKey]);
                }

                ExternalLoginInfo externalLoginInfo = null;
                if (model.IsExternalLogin)
                {
                    externalLoginInfo = await _signInManager.GetExternalLoginInfoAsync();

                    if (externalLoginInfo == null)
                    {
                        throw new Exception("Can not external login!");
                    }

                    using (var providerManager = _externalLoginInfoManagerFactory.GetExternalLoginInfoManager(externalLoginInfo.LoginProvider))
                    {
                        model.UserName = providerManager.Object.GetUserNameFromClaims(externalLoginInfo.Principal.Claims.ToList());
                    }

                    model.Password = await _userManager.CreateRandomPassword();
                }
                else
                {
                    if (model.UserName.IsNullOrEmpty() || model.Password.IsNullOrEmpty())
                    {
                        throw new UserFriendlyException(L("FormIsNotValidMessage"));
                    }
                }

                var user = await _userRegistrationManager.RegisterAsync(
                    model.Name,
                    model.Surname,
                    model.EmailAddress,
                    model.UserName,
                    model.Password,
                    false,
                    _appUrlService.CreateEmailActivationUrlFormat(AbpSession.TenantId)
                    );

                //Getting tenant-specific settings
                var isEmailConfirmationRequiredForLogin = await SettingManager.GetSettingValueAsync <bool>(AbpZeroSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin);

                if (model.IsExternalLogin)
                {
                    Debug.Assert(externalLoginInfo != null);

                    if (string.Equals(externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email), model.EmailAddress, StringComparison.OrdinalIgnoreCase))
                    {
                        user.IsEmailConfirmed = true;
                    }

                    user.Logins = new List <UserLogin>
                    {
                        new UserLogin
                        {
                            LoginProvider = externalLoginInfo.LoginProvider,
                            ProviderKey   = externalLoginInfo.ProviderKey,
                            TenantId      = user.TenantId
                        }
                    };
                }

                await _unitOfWorkManager.Current.SaveChangesAsync();

                Debug.Assert(user.TenantId != null);

                var tenant = await _tenantManager.GetByIdAsync(user.TenantId.Value);

                //Directly login if possible
                if (user.IsActive && (user.IsEmailConfirmed || !isEmailConfirmationRequiredForLogin))
                {
                    AbpLoginResult <Tenant, User> loginResult;
                    if (externalLoginInfo != null)
                    {
                        loginResult = await _logInManager.LoginAsync(externalLoginInfo, tenant.TenancyName);
                    }
                    else
                    {
                        loginResult = await GetLoginResultAsync(user.UserName, model.Password, tenant.TenancyName);
                    }

                    if (loginResult.Result == AbpLoginResultType.Success)
                    {
                        await _signInManager.SignInAsync(loginResult.Identity, false);

                        if (!string.IsNullOrEmpty(model.SingleSignIn) && model.SingleSignIn.Equals("true", StringComparison.OrdinalIgnoreCase) && loginResult.Result == AbpLoginResultType.Success)
                        {
                            var returnUrl = NormalizeReturnUrl(model.ReturnUrl);
                            loginResult.User.SetSignInToken();
                            returnUrl = AddSingleSignInParametersToReturnUrl(returnUrl, loginResult.User.SignInToken, loginResult.User.Id, loginResult.User.TenantId);
                            return(Redirect(returnUrl));
                        }

                        return(Redirect(GetAppHomeUrl()));
                    }

                    Logger.Warn("New registered user could not be login. This should not be normally. login result: " + loginResult.Result);
                }

                return(View("RegisterResult", new RegisterResultViewModel
                {
                    TenancyName = tenant.TenancyName,
                    NameAndSurname = user.Name + " " + user.Surname,
                    UserName = user.UserName,
                    EmailAddress = user.EmailAddress,
                    IsActive = user.IsActive,
                    IsEmailConfirmationRequired = isEmailConfirmationRequiredForLogin
                }));
            }
            catch (UserFriendlyException ex)
            {
                ViewBag.UseCaptcha   = !model.IsExternalLogin && UseCaptchaOnRegistration();
                ViewBag.ErrorMessage = ex.Message;

                model.PasswordComplexitySetting = await _passwordComplexitySettingStore.GetSettingsAsync();

                return(View("Register", model));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 租户注册
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <RegisterTenantOutput> RegisterTenant(RegisterTenantInput input)
        {
            using (CurrentUnitOfWork.SetTenantId(null))
            {
                CheckTenantRegistrationIsEnabled();

                //if (UseCaptchaOnRegistration())//使用手机验证码作为验证
                //{
                //    await _captchaValidator.ValidateAsync(input.CaptchaResponse);
                //}

                switch (input.Type)
                {
                case RegisterType.Telephone:
                {
                    if (input.PhoneNumber.IsNullOrWhiteSpace())
                    {
                        throw new UserFriendlyException(L("Identity.RequiredPhoneNumber"));
                    }

                    await _verificationCodeManager.CheckRegistrationVerificationCode(input.PhoneNumber, input.RegisterCode);

                    break;
                }

                case RegisterType.Email:
                {
                    if (input.EmailAddress.IsNullOrWhiteSpace())
                    {
                        throw new UserFriendlyException(L("Identity.RequiredEmail"));
                    }

                    await _verificationCodeManager.CheckRegistrationVerificationCode(input.EmailAddress, input.RegisterCode);

                    break;
                }

                default:
                    break;
                }

                //Getting host-specific settings
                var isNewRegisteredTenantActiveByDefault = await SettingManager.GetSettingValueForApplicationAsync <bool>(AppSettings.TenantManagement.IsNewRegisteredTenantActiveByDefault);

                var isEmailConfirmationRequiredForLogin = await SettingManager.GetSettingValueForApplicationAsync <bool>(AbpZeroSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin);

                var defaultEditionIdValue = await SettingManager.GetSettingValueForApplicationAsync(AppSettings.TenantManagement.DefaultEdition);

                int?defaultEditionId = null;

                if (!string.IsNullOrEmpty(defaultEditionIdValue) && (await _editionManager.FindByIdAsync(Convert.ToInt32(defaultEditionIdValue)) != null))
                {
                    defaultEditionId = Convert.ToInt32(defaultEditionIdValue);
                }

                var tenantId = await TenantManager.CreateWithAdminUserAsync(
                    input.TenancyName,
                    input.TenancyName,
                    input.Password,
                    input.EmailAddress ?? string.Empty,
                    input.PhoneNumber,
                    null,
                    isNewRegisteredTenantActiveByDefault,
                    defaultEditionId,
                    false,
                    true,
                    null,
                    false,
                    _appUrlService.CreateEmailActivationUrlFormat(input.TenancyName)
                    );

                var tenant = await TenantManager.GetByIdAsync(tenantId);

                await _appNotifier.NewTenantRegisteredAsync(tenant);

                return(new RegisterTenantOutput
                {
                    TenantId = tenant.Id,
                    TenancyName = input.TenancyName,
                    Name = input.TenancyName,
                    UserName = input.TenancyName,
                    IsActive = tenant.IsActive,
                    EmailAddress = input.EmailAddress,
                    IsEmailConfirmationRequired = isEmailConfirmationRequiredForLogin,
                    IsTenantActive = tenant.IsActive
                });
            }
        }