/// <summary> /// Register new user /// </summary> /// <param name="parameters">Authentication parameters received from external authentication method</param> /// <param name="returnUrl">URL to which the user will return after authentication</param> /// <returns>Result of an authentication</returns> protected virtual IActionResult RegisterNewUser(ExternalAuthenticationParameters parameters, string returnUrl) { //check whether the specified email has been already registered if (_customerService.GetCustomerByEmail(parameters.Email) != null) { var alreadyExistsError = string.Format(_localizationService.GetResource("Account.AssociatedExternalAuth.EmailAlreadyExists"), !string.IsNullOrEmpty(parameters.ExternalDisplayIdentifier) ? parameters.ExternalDisplayIdentifier : parameters.ExternalIdentifier); return(ErrorAuthentication(new[] { alreadyExistsError }, returnUrl)); } //registration is approved if validation isn't required var registrationIsApproved = _customerSettings.UserRegistrationType == UserRegistrationType.Standard || (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation && !_externalAuthenticationSettings.RequireEmailValidation); //create registration request var registrationRequest = new CustomerRegistrationRequest(_workContext.CurrentCustomer, parameters.Email, parameters.Email, CommonHelper.GenerateRandomDigitCode(20), PasswordFormat.Hashed, _storeContext.CurrentStore.Id, registrationIsApproved); //whether registration request has been completed successfully var registrationResult = _customerRegistrationService.RegisterCustomer(registrationRequest); if (!registrationResult.Success) { return(ErrorAuthentication(registrationResult.Errors, returnUrl)); } //allow to save other customer values by consuming this event _eventPublisher.Publish(new CustomerAutoRegisteredByExternalMethodEvent(_workContext.CurrentCustomer, parameters)); //raise customer registered event _eventPublisher.Publish(new CustomerRegisteredEvent(_workContext.CurrentCustomer)); //store owner notifications if (_customerSettings.NotifyNewCustomerRegistration) { _workflowMessageService.SendCustomerRegisteredNotificationMessage(_workContext.CurrentCustomer, _localizationSettings.DefaultAdminLanguageId); } //associate external account with registered user AssociateExternalAccountWithUser(_workContext.CurrentCustomer, parameters); //authenticate if (registrationIsApproved) { _authenticationService.SignIn(_workContext.CurrentCustomer, false); _workflowMessageService.SendCustomerWelcomeMessage(_workContext.CurrentCustomer, _workContext.WorkingLanguage.Id); return(new RedirectToRouteResult("RegisterResult", new { resultId = (int)UserRegistrationType.Standard, returnUrl })); } //registration is succeeded but isn't activated if (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation) { //email validation message _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SmiCustomerDefaults.AccountActivationTokenAttribute, Guid.NewGuid().ToString()); _workflowMessageService.SendCustomerEmailValidationMessage(_workContext.CurrentCustomer, _workContext.WorkingLanguage.Id); return(new RedirectToRouteResult("RegisterResult", new { resultId = (int)UserRegistrationType.EmailValidation })); } //registration is succeeded but isn't approved by admin if (_customerSettings.UserRegistrationType == UserRegistrationType.AdminApproval) { return(new RedirectToRouteResult("RegisterResult", new { resultId = (int)UserRegistrationType.AdminApproval })); } return(ErrorAuthentication(new[] { "Error on registration" }, returnUrl)); }
/// <summary> /// Ctor /// </summary> /// <param name="customer">Customer</param> /// <param name="parameters">Parameters</param> public CustomerAutoRegisteredByExternalMethodEvent(Customer customer, ExternalAuthenticationParameters parameters) { Customer = customer; AuthenticationParameters = parameters; }
/// <summary> /// Authenticate current user and associate new external account with user /// </summary> /// <param name="currentLoggedInUser">Current logged-in user</param> /// <param name="parameters">Authentication parameters received from external authentication method</param> /// <param name="returnUrl">URL to which the user will return after authentication</param> /// <returns>Result of an authentication</returns> protected virtual IActionResult AuthenticateNewUser(Customer currentLoggedInUser, ExternalAuthenticationParameters parameters, string returnUrl) { //associate external account with logged-in user if (currentLoggedInUser != null) { AssociateExternalAccountWithUser(currentLoggedInUser, parameters); return(SuccessfulAuthentication(returnUrl)); } //or try to register new user if (_customerSettings.UserRegistrationType != UserRegistrationType.Disabled) { return(RegisterNewUser(parameters, returnUrl)); } //registration is disabled return(ErrorAuthentication(new[] { "Registration is disabled" }, returnUrl)); }