Example #1
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));
            }
        }
Example #2
0
        public async Task <RegisterTenantOutput> RegisterTenant(RegisterTenantInput input)
        {
            if (input.EditionId.HasValue)
            {
                await CheckEditionSubscriptionAsync(input.EditionId.Value, input.SubscriptionStartType, input.Gateway, input.PaymentId);
            }
            else
            {
                await CheckRegistrationWithoutEdition();
            }

            using (CurrentUnitOfWork.SetTenantId(null))
            {
                CheckTenantRegistrationIsEnabled();

                if (UseCaptchaOnRegistration())
                {
                    await _recaptchaValidator.ValidateAsync(input.CaptchaResponse);
                }

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

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

                DateTime?subscriptionEndDate = null;
                var      isInTrialPeriod     = false;

                if (input.EditionId.HasValue)
                {
                    isInTrialPeriod = input.SubscriptionStartType == SubscriptionStartType.Trial;

                    if (isInTrialPeriod)
                    {
                        var edition = (SubscribableEdition)await _editionManager.GetByIdAsync(input.EditionId.Value);

                        subscriptionEndDate = Clock.Now.AddDays(edition.TrialDayCount ?? 0);
                    }
                }

                var tenantId = await _tenantManager.CreateWithAdminUserAsync(
                    input.TenancyName,
                    input.Name,
                    input.AdminPassword,
                    input.AdminEmailAddress,
                    null,
                    isNewRegisteredTenantActiveByDefault,
                    input.EditionId,
                    false,
                    true,
                    subscriptionEndDate,
                    isInTrialPeriod,
                    AppUrlService.CreateEmailActivationUrlFormat(input.TenancyName)
                    );

                Tenant tenant;

                if (input.SubscriptionStartType == SubscriptionStartType.Paid)
                {
                    if (!input.Gateway.HasValue)
                    {
                        throw new Exception("Gateway is missing!");
                    }

                    var payment = await _subscriptionPaymentRepository.GetByGatewayAndPaymentIdAsync(
                        input.Gateway.Value,
                        input.PaymentId
                        );

                    tenant = await _tenantManager.UpdateTenantAsync(
                        tenantId,
                        true,
                        false,
                        payment.PaymentPeriodType,
                        payment.EditionId,
                        EditionPaymentType.NewRegistration);

                    await _subscriptionPaymentRepository.UpdateByGatewayAndPaymentIdAsync(input.Gateway.Value,
                                                                                          input.PaymentId, tenantId, SubscriptionPaymentStatus.Completed);
                }
                else
                {
                    tenant = await TenantManager.GetByIdAsync(tenantId);
                }

                await _appNotifier.NewTenantRegisteredAsync(tenant);

                if (input.EditionId.HasValue && input.Gateway.HasValue && !input.PaymentId.IsNullOrEmpty())
                {
                    _paymentCache.RemoveCacheItem(input.Gateway.Value, input.PaymentId);
                }

                return(new RegisterTenantOutput
                {
                    TenantId = tenant.Id,
                    TenancyName = input.TenancyName,
                    Name = input.Name,
                    UserName = AbpUserBase.AdminUserName,
                    EmailAddress = input.AdminEmailAddress,
                    IsActive = tenant.IsActive,
                    IsEmailConfirmationRequired = isEmailConfirmationRequiredForLogin,
                    IsTenantActive = tenant.IsActive
                });
            }
        }
Example #3
0
        public async Task <RegisterTenantOutput> RegisterTenant(RegisterTenantInput input)
        {
            try
            {
                if (input.EditionId.HasValue)
                {
                    await CheckEditionSubscriptionAsync(input.EditionId.Value, input.SubscriptionStartType, input.Gateway, input.PaymentId);
                }
                else
                {
                    await CheckRegistrationWithoutEdition();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            try
            {
                using (CurrentUnitOfWork.SetTenantId(null))
                {
                    CheckTenantRegistrationIsEnabled();

                    if (UseCaptchaOnRegistration())
                    {
                        await _recaptchaValidator.ValidateAsync(input.CaptchaResponse);
                    }

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

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

                    DateTime?subscriptionEndDate = null;
                    var      isInTrialPeriod     = false;

                    if (input.EditionId.HasValue)
                    {
                        isInTrialPeriod = input.SubscriptionStartType == SubscriptionStartType.Trial;

                        if (isInTrialPeriod)
                        {
                            var edition = (SubscribableEdition)await _editionManager.GetByIdAsync(input.EditionId.Value);

                            subscriptionEndDate = Clock.Now.AddDays(edition.TrialDayCount ?? 0);
                        }
                    }

                    var tenantId = await _tenantManager.CreateWithAdminUserAsync(
                        input.TenancyName,
                        input.Name,
                        input.AdminPassword,
                        input.AdminEmailAddress,
                        null,
                        isNewRegisteredTenantActiveByDefault,
                        input.EditionId,
                        false,
                        true,
                        subscriptionEndDate,
                        isInTrialPeriod,
                        AppUrlService.CreateEmailActivationUrlFormat(input.TenancyName),
                        input.TenantTypeId
                        );

                    Tenant tenant;

                    if (input.SubscriptionStartType == SubscriptionStartType.Paid)
                    {
                        if (!input.Gateway.HasValue)
                        {
                            throw new Exception("Gateway is missing!");
                        }

                        var payment = await _subscriptionPaymentRepository.GetByGatewayAndPaymentIdAsync(
                            input.Gateway.Value,
                            input.PaymentId
                            );

                        tenant = await _tenantManager.UpdateTenantAsync(
                            tenantId,
                            true,
                            false,
                            payment.PaymentPeriodType,
                            payment.EditionId,
                            EditionPaymentType.NewRegistration);

                        await _subscriptionPaymentRepository.UpdateByGatewayAndPaymentIdAsync(input.Gateway.Value,
                                                                                              input.PaymentId, tenantId, SubscriptionPaymentStatus.Completed);
                    }
                    else
                    {
                        tenant = await TenantManager.GetByIdAsync(tenantId);
                    }

                    await _appNotifier.NewTenantRegisteredAsync(tenant);

                    if (input.EditionId.HasValue && input.Gateway.HasValue && !input.PaymentId.IsNullOrEmpty())
                    {
                        _paymentCache.RemoveCacheItem(input.Gateway.Value, input.PaymentId);
                    }



                    CreateMileInputDto mob = new CreateMileInputDto();
                    mob.Code        = "LD";
                    mob.Name        = "Lead";
                    mob.TenantId    = tenantId;
                    mob.Id          = 0;
                    mob.IsQuotation = false;
                    var milestone2 = mob.MapTo <MileStone>();
                    await _MilestoneRepository.InsertAsync(milestone2);

                    CreateMileInputDto mobj = new CreateMileInputDto();
                    mobj.Code        = "QL";
                    mobj.Name        = "Qualified";
                    mobj.TenantId    = tenantId;
                    mobj.Id          = 0;
                    mobj.IsQuotation = false;
                    var milestone = mobj.MapTo <MileStone>();
                    await _MilestoneRepository.InsertAsync(milestone);

                    CreateMileInputDto QT = new CreateMileInputDto();
                    QT.Code        = "QT";
                    QT.Name        = "Qouted";
                    QT.TenantId    = tenantId;
                    QT.Id          = 0;
                    QT.IsQuotation = true;
                    var Qouted = QT.MapTo <MileStone>();
                    await _MilestoneRepository.InsertAsync(Qouted);

                    CreateMileInputDto NG = new CreateMileInputDto();
                    NG.Code        = "NG";
                    NG.Name        = "Negotiation";
                    NG.TenantId    = tenantId;
                    NG.Id          = 0;
                    NG.IsQuotation = true;
                    var Negotiation = NG.MapTo <MileStone>();
                    await _MilestoneRepository.InsertAsync(Negotiation);

                    CreateMileInputDto CL = new CreateMileInputDto();
                    CL.Code        = "CL";
                    CL.Name        = "Closed";
                    CL.TenantId    = tenantId;
                    CL.Id          = 0;
                    CL.IsQuotation = true;
                    var Closed = CL.MapTo <MileStone>();
                    await _MilestoneRepository.InsertAsync(Closed);

                    CreateQuotationStatusInput QS1 = new CreateQuotationStatusInput();
                    QS1.QuotationStatusCode = "NW";
                    QS1.QuotationStatusName = "New";
                    QS1.New      = true;
                    QS1.TenantId = tenantId;
                    QS1.Id       = 0;
                    var QS1s = QS1.MapTo <QuotationStatus>();
                    await _QuotationStatusRepository.InsertAsync(QS1s);

                    CreateQuotationStatusInput QS2 = new CreateQuotationStatusInput();
                    QS2.QuotationStatusCode = "SU";
                    QS2.QuotationStatusName = "Submitted";
                    QS2.Submitted           = true;
                    QS2.TenantId            = tenantId;
                    QS2.Id = 0;
                    var QS2s = QS2.MapTo <QuotationStatus>();
                    await _QuotationStatusRepository.InsertAsync(QS2s);

                    CreateQuotationStatusInput QS3 = new CreateQuotationStatusInput();
                    QS3.QuotationStatusCode = "WO";
                    QS3.QuotationStatusName = "Won";
                    QS3.Won      = true;
                    QS3.TenantId = tenantId;
                    QS3.Id       = 0;
                    var QS3s = QS3.MapTo <QuotationStatus>();
                    await _QuotationStatusRepository.InsertAsync(QS3s);

                    CreateQuotationStatusInput QS4 = new CreateQuotationStatusInput();
                    QS4.QuotationStatusCode = "LO";
                    QS4.QuotationStatusName = "Lost";
                    QS4.Lost     = true;
                    QS4.TenantId = tenantId;
                    QS4.Id       = 0;
                    var QS4s = QS4.MapTo <QuotationStatus>();
                    await _QuotationStatusRepository.InsertAsync(QS4s);

                    CreateQuotationStatusInput QS5 = new CreateQuotationStatusInput();
                    QS5.QuotationStatusCode = "RE";
                    QS5.QuotationStatusName = "Revised";
                    QS5.Revised             = true;
                    QS5.TenantId            = tenantId;
                    QS5.Id = 0;
                    var QS5s = QS5.MapTo <QuotationStatus>();
                    await _QuotationStatusRepository.InsertAsync(QS5s);


                    return(new RegisterTenantOutput
                    {
                        TenantId = tenant.Id,
                        TenancyName = input.TenancyName,
                        Name = input.Name,
                        UserName = AbpUserBase.AdminUserName,
                        EmailAddress = input.AdminEmailAddress,
                        IsActive = tenant.IsActive,
                        IsEmailConfirmationRequired = isEmailConfirmationRequiredForLogin,
                        IsTenantActive = tenant.IsActive
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #4
0
        public async Task <RegisterTenantOutput> RegisterTenant(RegisterTenantInput input)
        {
            if (input.EditionId.HasValue)
            {
                await CheckEditionSubscriptionAsync(input.EditionId.Value, input.SubscriptionStartType);
            }
            else
            {
                await CheckRegistrationWithoutEdition();
            }

            using (CurrentUnitOfWork.SetTenantId(null))
            {
                CheckTenantRegistrationIsEnabled();

                if (UseCaptchaOnRegistration())
                {
                    await _recaptchaValidator.ValidateAsync(input.CaptchaResponse);
                }

                //Getting host-specific settings
                var isActive = await IsNewRegisteredTenantActiveByDefault(input.SubscriptionStartType);

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

                DateTime?subscriptionEndDate = null;
                var      isInTrialPeriod     = false;

                if (input.EditionId.HasValue)
                {
                    isInTrialPeriod = input.SubscriptionStartType == SubscriptionStartType.Trial;

                    if (isInTrialPeriod)
                    {
                        var edition = (SubscribableEdition)await _editionManager.GetByIdAsync(input.EditionId.Value);

                        subscriptionEndDate = Clock.Now.AddDays(edition.TrialDayCount ?? 0);
                    }
                }

                var tenantId = await _tenantManager.CreateWithAdminUserAsync(
                    input.TenancyName,
                    input.Name,
                    input.AdminPassword,
                    input.AdminEmailAddress,
                    null,
                    isActive,
                    input.EditionId,
                    shouldChangePasswordOnNextLogin : false,
                    sendActivationEmail : true,
                    subscriptionEndDate,
                    isInTrialPeriod,
                    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.Name,
                    UserName = AbpUserBase.AdminUserName,
                    EmailAddress = input.AdminEmailAddress,
                    IsActive = tenant.IsActive,
                    IsEmailConfirmationRequired = isEmailConfirmationRequired,
                    IsTenantActive = tenant.IsActive
                });
            }
        }
        public virtual async Task <JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "", string ss = "")
        {
            returnUrl = NormalizeReturnUrl(returnUrl);
            if (!string.IsNullOrWhiteSpace(returnUrlHash))
            {
                returnUrl = returnUrl + returnUrlHash;
            }

            if (UseCaptchaOnLogin())
            {
                await _recaptchaValidator.ValidateAsync(HttpContext.Request.Form[RecaptchaValidator.RecaptchaResponseKey]);
            }

            var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, GetTenancyNameOrNull());

            if (!string.IsNullOrEmpty(ss) && ss.Equals("true", StringComparison.OrdinalIgnoreCase) && loginResult.Result == AbpLoginResultType.Success)
            {
                loginResult.User.SetSignInToken();
                returnUrl = AddSingleSignInParametersToReturnUrl(returnUrl, loginResult.User.SignInToken, loginResult.User.Id, loginResult.User.TenantId);
            }

            if (_settingManager.GetSettingValue <bool>(AppSettings.UserManagement.AllowOneConcurrentLoginPerUser))
            {
                await _userManager.UpdateSecurityStampAsync(loginResult.User);
            }

            if (loginResult.User.ShouldChangePasswordOnNextLogin)
            {
                loginResult.User.SetNewPasswordResetCode();

                return(Json(new AjaxResponse
                {
                    TargetUrl = Url.Action(
                        "ResetPassword",
                        new ResetPasswordViewModel
                    {
                        TenantId = AbpSession.TenantId,
                        UserId = loginResult.User.Id,
                        ResetCode = loginResult.User.PasswordResetCode,
                        ReturnUrl = returnUrl,
                        SingleSignIn = ss
                    })
                }));
            }

            var signInResult = await _signInManager.SignInOrTwoFactorAsync(loginResult, loginModel.RememberMe);

            if (signInResult.RequiresTwoFactor)
            {
                return(Json(new AjaxResponse
                {
                    TargetUrl = Url.Action(
                        "SendSecurityCode",
                        new
                    {
                        returnUrl = returnUrl,
                        rememberMe = loginModel.RememberMe
                    })
                }));
            }

            Debug.Assert(signInResult.Succeeded);

            await UnitOfWorkManager.Current.SaveChangesAsync();

            return(Json(new AjaxResponse {
                TargetUrl = returnUrl
            }));
        }