/// <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.Clear, _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 vustomer 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 })); } //registration is succeeded but isn't activated if (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation) { //email validation message _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, NopCustomerDefaults.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> /// 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)); }
/// <summary> /// Ctor /// </summary> /// <param name="user">User</param> /// <param name="parameters">Parameters</param> public UserAutoRegisteredByExternalMethodEvent(User user, ExternalAuthenticationParameters parameters) { this.User = user; this.AuthenticationParameters = parameters; }
/// <summary> /// Get the particular user with specified parameters /// </summary> /// <param name="parameters">External authentication parameters</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the customer /// </returns> public virtual async Task <Customer> GetUserByExternalAuthenticationParametersAsync(ExternalAuthenticationParameters parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } var associationRecord = _externalAuthenticationRecordRepository.Table.FirstOrDefault(record => record.ExternalIdentifier.Equals(parameters.ExternalIdentifier) && record.ProviderSystemName.Equals(parameters.ProviderSystemName)); if (associationRecord == null) { return(null); } return(await _customerService.GetCustomerByIdAsync(associationRecord.CustomerId)); }
/// <summary> /// Associate external account with customer /// </summary> /// <param name="customer">Customer</param> /// <param name="parameters">External authentication parameters</param> /// <returns>A task that represents the asynchronous operation</returns> public virtual async Task AssociateExternalAccountWithUserAsync(Customer customer, ExternalAuthenticationParameters parameters) { if (customer == null) { throw new ArgumentNullException(nameof(customer)); } var externalAuthenticationRecord = new ExternalAuthenticationRecord { CustomerId = customer.Id, Email = parameters.Email, ExternalIdentifier = parameters.ExternalIdentifier, ExternalDisplayIdentifier = parameters.ExternalDisplayIdentifier, OAuthAccessToken = parameters.AccessToken, ProviderSystemName = parameters.ProviderSystemName }; await _externalAuthenticationRecordRepository.InsertAsync(externalAuthenticationRecord, false); }
public CustomerAutoRegisteredByExternalMethodEvent(Customer customer, ExternalAuthenticationParameters parameters) { this.Customer = customer; this.AuthenticationParameters = parameters; }