/**
  * @param FormBuilderInterface $builder
  * @param array                $options
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $integrations = '';
     $integrationObjects = $this->helper->getIntegrationObjects(null, 'login_button');
     foreach ($integrationObjects as $integrationObject) {
         if ($integrationObject->getIntegrationSettings()->isPublished()) {
             $model = $this->formModel;
             $integrations .= $integrationObject->getName() . ',';
             $integration = ['integration' => $integrationObject->getName()];
             $builder->add('authUrl_' . $integrationObject->getName(), 'hidden', ['data' => $model->buildUrl('mautic_integration_auth_postauth', $integration, true, [])]);
         }
     }
     $builder->add('integrations', 'hidden', ['data' => $integrations]);
 }
 /**
  * @param TokenInterface        $token
  * @param UserProviderInterface $userProvider
  * @param                       $providerKey
  *
  * @return UsernamePasswordToken
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $authenticated = false;
     $authenticationService = null;
     $response = null;
     $failedAuthMessage = null;
     $user = $token->getUser();
     $authenticatingService = $token instanceof PluginToken ? $token->getAuthenticatingService() : null;
     if (!$user instanceof User) {
         try {
             $user = $userProvider->loadUserByUsername($token->getUsername());
         } catch (UsernameNotFoundException $e) {
         }
         // Will try with the given password unless the plugin explicitly failed authentication
         $tryWithPassword = true;
         // Try authenticating with a plugin first
         if ($this->dispatcher->hasListeners(UserEvents::USER_FORM_AUTHENTICATION)) {
             $integrations = $this->integrationHelper->getIntegrationObjects($authenticatingService, ['sso_form'], false, null, true);
             $authEvent = new AuthenticationEvent($user, $token, $userProvider, $this->request, false, $authenticatingService, $integrations);
             $this->dispatcher->dispatch(UserEvents::USER_FORM_AUTHENTICATION, $authEvent);
             if ($authenticated = $authEvent->isAuthenticated()) {
                 $user = $authEvent->getUser();
                 $authenticatingService = $authEvent->getAuthenticatingService();
             } elseif ($authEvent->isFailed()) {
                 $tryWithPassword = false;
             }
             $response = $authEvent->getResponse();
             $failedAuthMessage = $authEvent->getFailedAuthenticationMessage();
         }
         if (!$authenticated && $tryWithPassword && $user instanceof User) {
             // Try authenticating with local password
             $authenticated = $this->encoder->isPasswordValid($user, $token->getCredentials());
         }
     } else {
         // Assume the user is authenticated although the token will tell for sure
         $authenticated = true;
     }
     if ($authenticated) {
         return new PluginToken($providerKey, $authenticatingService, $user, $user->getPassword(), $user->getRoles(), $response);
     } elseif ($response) {
         return new PluginToken($providerKey, $authenticatingService, $user, '', [], $response);
     }
     if ($failedAuthMessage) {
         throw new AuthenticationException($failedAuthMessage);
     }
     throw new BadCredentialsException();
 }
Exemple #3
0
 /**
  * @param CampaignExecutionEvent $event
  */
 public function onCampaignTriggerAction(CampaignExecutionEvent $event)
 {
     $config = $event->getConfig();
     $lead = $event->getLead();
     $integration = !empty($config['integration']) ? $config['integration'] : null;
     $feature = empty($integration) ? 'push_lead' : null;
     $services = $this->integrationHelper->getIntegrationObjects($integration, $feature);
     $success = false;
     foreach ($services as $name => $s) {
         $settings = $s->getIntegrationSettings();
         if (!$settings->isPublished()) {
             continue;
         }
         if (method_exists($s, 'pushLead')) {
             if ($s->pushLead($lead, $config)) {
                 $success = true;
             }
         }
     }
     return $event->setResult($success);
 }
 /**
  * @param TokenInterface $token
  *
  * @return Response|PluginToken
  */
 public function authenticate(TokenInterface $token)
 {
     if (!$this->supports($token)) {
         return null;
     }
     $user = $token->getUser();
     $authenticatingService = $token->getAuthenticatingService();
     $response = null;
     if (!$user instanceof User) {
         $authenticated = false;
         // Try authenticating with a plugin
         if ($this->dispatcher->hasListeners(UserEvents::USER_PRE_AUTHENTICATION)) {
             $integrations = $this->integrationHelper->getIntegrationObjects($authenticatingService, ['sso_service'], false, null, true);
             $loginCheck = 'mautic_sso_login_check' == $this->request->attributes->get('_route');
             $authEvent = new AuthenticationEvent(null, $token, $this->userProvider, $this->request, $loginCheck, $authenticatingService, $integrations);
             $this->dispatcher->dispatch(UserEvents::USER_PRE_AUTHENTICATION, $authEvent);
             if ($authenticated = $authEvent->isAuthenticated()) {
                 $eventToken = $authEvent->getToken();
                 if ($eventToken !== $token) {
                     // A custom token has been set by the plugin so just return it
                     return $eventToken;
                 }
                 $user = $authEvent->getUser();
                 $authenticatingService = $authEvent->getAuthenticatingService();
             }
             $response = $authEvent->getResponse();
             if (!$authenticated && $loginCheck && !$response) {
                 // Set an empty JSON response
                 $response = new JsonResponse([]);
             }
         }
         if (!$authenticated && empty($response)) {
             throw new AuthenticationException('mautic.user.auth.error.invalidlogin');
         }
     }
     return new PluginToken($this->providerKey, $authenticatingService, $user, $user instanceof User ? $user->getPassword() : '', $user instanceof User ? $user->getRoles() : [], $response);
 }