/** * @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(); }
/** * Populates custom field values for updating the lead. Also retrieves social media data * * @param Lead $lead * @param array $data * @param bool|false $overwriteWithBlank * @param bool|true $fetchSocialProfiles * * @return array */ public function setFieldValues(Lead &$lead, array $data, $overwriteWithBlank = false, $fetchSocialProfiles = true) { if ($fetchSocialProfiles) { //@todo - add a catch to NOT do social gleaning if a lead is created via a form, etc as we do not want the user to experience the wait //generate the social cache list($socialCache, $socialFeatureSettings) = $this->integrationHelper->getUserProfiles($lead, $data, true, null, false, true); //set the social cache while we have it if (!empty($socialCache)) { $lead->setSocialCache($socialCache); } } //save the field values $fieldValues = $lead->getFields(); if (empty($fieldValues)) { // Lead is new or they haven't been populated so let's build the fields now static $fields; if (empty($fields)) { $fields = $this->leadFieldModel->getEntities(['filter' => ['isPublished' => true], 'hydration_mode' => 'HYDRATE_ARRAY']); $fields = $this->organizeFieldsByGroup($fields); } $fieldValues = $fields; } //update existing values foreach ($fieldValues as $group => &$groupFields) { foreach ($groupFields as $alias => &$field) { if (!isset($field['value'])) { $field['value'] = null; } // Only update fields that are part of the passed $data array if (array_key_exists($alias, $data)) { $curValue = $field['value']; $newValue = $data[$alias]; if ($curValue !== $newValue && (strlen($newValue) > 0 || strlen($newValue) === 0 && $overwriteWithBlank)) { $field['value'] = $newValue; $lead->addUpdatedField($alias, $newValue, $curValue); } //if empty, check for social media data to plug the hole if (empty($newValue) && !empty($socialCache)) { foreach ($socialCache as $service => $details) { //check to see if a field has been assigned if (!empty($socialFeatureSettings[$service]['leadFields']) && in_array($field['alias'], $socialFeatureSettings[$service]['leadFields'])) { //check to see if the data is available $key = array_search($field['alias'], $socialFeatureSettings[$service]['leadFields']); if (isset($details['profile'][$key])) { //Found!! $field['value'] = $details['profile'][$key]; $lead->addUpdatedField($alias, $details['profile'][$key]); break; } } } } } } } $lead->setFields($fieldValues); }
/** * @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 MauticFactory $factory * @param $lead * @param $event * * @return bool */ public function sendTweetAction($lead, $event) { $tweetSent = false; /** @var \MauticPlugin\MauticSocialBundle\Integration\TwitterIntegration $twitterIntegration */ $twitterIntegration = $this->integrationHelper->getIntegrationObject('Twitter'); // Setup clickthrough for URLs in tweet $this->clickthrough = ['source' => ['campaign', $event['campaign']['id']]]; $leadArray = $lead->getProfileFields(); if (empty($leadArray['twitter'])) { return false; } $tweetText = $event['properties']['tweet_text']; $tweetText = $this->parseTweetText($tweetText, $leadArray); $tweetUrl = $twitterIntegration->getApiUrl('statuses/update'); $status = ['status' => $tweetText]; // fire the tweet $sendTweet = $twitterIntegration->makeRequest($tweetUrl, $status, 'POST', ['append_callback' => false]); // verify the tweet was sent by checking for a tweet id if (is_array($sendTweet) && array_key_exists('id_str', $sendTweet)) { $tweetSent = true; } return $tweetSent ? ['timeline' => $tweetText, 'response' => $sendTweet] : ['failed' => 1, 'response' => $sendTweet]; }
/** * @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); }
/** * Renders the HTML for the social share buttons. * * @return string */ protected function renderSocialShareButtons() { static $content = ''; if (empty($content)) { $shareButtons = $this->integrationHelper->getShareButtons(); $content = "<div class='share-buttons'>\n"; foreach ($shareButtons as $network => $button) { $content .= $button; } $content .= "</div>\n"; //load the css into the header by calling the sharebtn_css view $this->templating->render('MauticPageBundle:SubscribedEvents\\PageToken:sharebtn_css.html.php'); } return $content; }