/** * @param UserEmailOrigin $origin */ protected function clearOrigin($origin) { $folders = $origin->getFolders(); $folderRepository = $this->em->getRepository('OroImapBundle:ImapEmailFolder'); foreach ($folders as $folder) { $imapFolder = $folderRepository->findOneBy(['folder' => $folder]); if ($imapFolder && !$origin->isActive()) { $this->clearFolder($imapFolder); $this->em->remove($imapFolder); $this->logger->notice(sprintf('ImapFolder with ID %s removed', $imapFolder->getId())); } elseif ($imapFolder && !$folder->isSyncEnabled()) { $this->clearFolder($imapFolder); $imapFolder->getFolder()->setSynchronizedAt(null); } } foreach ($folders as $folder) { if (!$origin->isActive()) { $this->em->remove($folder); $this->logger->notice(sprintf('Folder with ID %s removed', $folder->getId())); } } if (!$origin->isActive()) { $this->em->remove($origin); $this->logger->notice(sprintf('Origin with ID %s removed', $origin->getId())); } $this->em->flush(); }
/** * @param FormInterface $form * @param UserEmailOrigin $emailOrigin */ protected function updateForm(FormInterface $form, UserEmailOrigin $emailOrigin) { if (!empty($emailOrigin->getAccessToken())) { $form->add('checkFolder', 'button', ['label' => $this->translator->trans('oro.email.retrieve_folders.label'), 'attr' => ['class' => 'btn btn-primary']])->add('folders', 'oro_email_email_folder_tree', ['label' => $this->translator->trans('oro.email.folders.label'), 'attr' => ['class' => 'folder-tree'], 'tooltip' => $this->translator->trans('oro.email.folders.tooltip')]); $form->remove('check'); } }
/** * @param FormInterface $form * @param UserEmailOrigin $emailOrigin */ protected function updateForm(FormInterface $form, UserEmailOrigin $emailOrigin) { //for empty() function must be only variable for compatibility with PHP 5.4 $token = $emailOrigin->getAccessToken(); if (!empty($token)) { $form->add('checkFolder', 'button', ['label' => $this->translator->trans('oro.email.retrieve_folders.label'), 'attr' => ['class' => 'btn btn-primary']])->add('folders', 'oro_email_email_folder_tree', ['label' => $this->translator->trans('oro.email.folders.label'), 'attr' => ['class' => 'folder-tree'], 'tooltip' => $this->translator->trans('oro.email.folders.tooltip')]); $form->remove('check'); } }
public function testExtendForm() { $form = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock(); $form->expects($this->exactly(2))->method('add')->will($this->returnSelf()); $form->expects($this->once())->method('remove')->will($this->returnSelf()); $userEmailOrigin = new UserEmailOrigin(); $userEmailOrigin->setAccessToken('test_string_token'); $formEvent = new FormEvent($form, $userEmailOrigin); $this->listener->extendForm($formEvent); }
/** * Set SmtpTransport instance or create a new if default mailer transport is not smtp * * @param UserEmailOrigin $userEmailOrigin */ public function prepareSmtpTransport($userEmailOrigin) { if (!$this->smtpTransport) { $username = $userEmailOrigin->getUser(); /** @var Mcrypt $encoder */ $encoder = $this->container->get('oro_security.encoder.mcrypt'); $password = $encoder->decryptData($userEmailOrigin->getPassword()); $host = $userEmailOrigin->getSmtpHost(); $port = $userEmailOrigin->getSmtpPort(); $security = $userEmailOrigin->getSmtpEncryption(); $accessToken = $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($userEmailOrigin); $transport = $this->getTransport(); if ($transport instanceof \Swift_SmtpTransport || $transport instanceof \Swift_Transport_EsmtpTransport) { $transport->setHost($host); $transport->setPort($port); $transport->setEncryption($security); } else { $transport = \Swift_SmtpTransport::newInstance($host, $port, $security); } $transport->setUsername($username); if ($accessToken === null) { $transport->setPassword($password); } else { $transport->setAuthMode('XOAUTH2'); $transport->setPassword($accessToken); } $this->smtpTransport = $transport; } }
/** * @param UserEmailOrigin $origin * * @return string */ public function getAccessTokenWithCheckingExpiration(UserEmailOrigin $origin) { $expiresAt = $origin->getAccessTokenExpiresAt(); $utcTimeZone = new \DateTimeZone('UTC'); $now = new \DateTime('now', $utcTimeZone); $token = $origin->getAccessToken(); //if token had been expired, the new one must be generated and saved to DB if ($now > $expiresAt && $this->configManager->get('oro_imap.enable_google_imap')) { $parameters = ['refresh_token' => $origin->getRefreshToken(), 'grant_type' => 'refresh_token']; $attemptNumber = 0; do { $attemptNumber++; $response = $this->doHttpRequest($parameters); if (!empty($response['access_token'])) { $token = $response['access_token']; $origin->setAccessToken($token); $newExpireDate = new \DateTime('+' . $response['expires_in'] . ' seconds', $utcTimeZone); $origin->setAccessTokenExpiresAt($newExpireDate); $this->doctrine->getManager()->persist($origin); $this->doctrine->getManager()->flush(); } } while ($attemptNumber <= self::RETRY_TIMES && empty($response['access_token'])); } return $token; }
/** * @param UserEmailOrigin $origin */ protected function clearOrigin(UserEmailOrigin $origin) { $folders = $origin->getFolders(); $folderRepository = $this->em->getRepository('OroImapBundle:ImapEmailFolder'); foreach ($folders as $folder) { $imapFolder = $folderRepository->findOneBy(['folder' => $folder]); if (!$origin->isActive()) { $this->removeFolder($imapFolder); } elseif (!$folder->isSyncEnabled()) { $this->clearFolder($imapFolder); $imapFolder->getFolder()->setSynchronizedAt(null); } } if (!$origin->isActive()) { $this->em->remove($origin); $this->em->flush(); } }
public function testIsSmtpConfiguredFailure() { $origin = new UserEmailOrigin(); $origin->setSmtpHost(''); $origin->setSmtpPort(''); $origin->setUser(''); $origin->setPassword(''); $origin->setSmtpEncryption(''); $this->assertFalse($origin->isSmtpConfigured()); }
/** * Set SmtpTransport instance or create a new if default mailer transport is not smtp * * @param UserEmailOrigin $userEmailOrigin */ public function prepareSmtpTransport($userEmailOrigin) { if (!$this->smtpTransport) { $username = $userEmailOrigin->getUser(); /** @var Mcrypt $encoder */ $encoder = $this->container->get('oro_security.encoder.mcrypt'); $password = $encoder->decryptData($userEmailOrigin->getPassword()); $host = $userEmailOrigin->getSmtpHost(); $port = $userEmailOrigin->getSmtpPort(); $security = $userEmailOrigin->getSmtpEncryption(); $transport = $this->getTransport(); if ($transport instanceof \Swift_SmtpTransport || $transport instanceof \Swift_Transport_EsmtpTransport) { $transport->setHost($host); $transport->setPort($port); $transport->setEncryption($security); } else { $transport = \Swift_SmtpTransport::newInstance($host, $port, $security); } $transport->setUsername($username); $transport->setPassword($password); $this->smtpTransport = $transport; } }
/** * @param UserEmailOrigin $origin * * @return ImapEmailFolderManager */ protected function createManager(UserEmailOrigin $origin) { $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->mcrypt->decryptData($origin->getPassword())); $connector = $this->connectorFactory->createImapConnector($config); return new ImapEmailFolderManager($connector, $this->doctrine->getManager(), $origin); }
/** * Case when user submit empty form but have configuration * configuration should be not active and relation should be broken */ public function testSubmitEmptyForm() { $type = new ConfigurationType($this->encryptor, $this->securityFacade, $this->translator); $form = $this->factory->create($type); $entity = new UserEmailOrigin(); $this->assertTrue($entity->isActive()); $form->setData($entity); $form->submit(array('imapHost' => '', 'imapPort' => '', 'smtpHost' => '', 'smtpPort' => '', 'imapEncryption' => '', 'smtpEncryption' => '', 'user' => '', 'password' => '')); $this->assertNotSame($entity, $form->getData()); $this->assertNotInstanceOf('Oro\\Bundle\\ImapBundle\\Entity\\UserEmailOrigin', $form->getData()); $this->assertNull($form->getData()); }
/** * @param UserEmailOrigin|null $origin * * @return $this */ public function setOrigin($origin = null) { $currentOrigin = $this->getOrigin(); if ($currentOrigin && ($origin === null || $origin->getUser() === null || $currentOrigin->getId() !== $origin->getId())) { $currentOrigin->setActive(false); $this->origin = null; } if ($origin !== null && $origin->getUser() !== null) { $this->origin = $origin; } return $this; }
/** * @param string $type * @param string|null $accessToken * @param string $formParentName * * @return FormInterface */ public function getImapConnectionForm($type, $accessToken, $formParentName) { $oauthEmailOrigin = new UserEmailOrigin(); $oauthEmailOrigin->setAccessToken($accessToken); if ($type === AccountTypeModel::ACCOUNT_TYPE_GMAIL) { $oauthEmailOrigin->setImapHost(GmailImap::DEFAULT_GMAIL_HOST); $oauthEmailOrigin->setImapPort(GmailImap::DEFAULT_GMAIL_PORT); } $accountTypeModel = $this->createAccountModel($type, $oauthEmailOrigin); return $this->prepareForm($formParentName, $accountTypeModel); }
/** * @param UserEmailOrigin $origin * * @return ImapEmailFolderManager */ protected function createManager(UserEmailOrigin $origin) { $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->mcrypt->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin)); $connector = $this->connectorFactory->createImapConnector($config); return new ImapEmailFolderManager($connector, $this->doctrine->getManager(), $origin); }
/** * Set IMAP configuration * * @param UserEmailOrigin $imapConfiguration * * @return User */ public function setImapConfiguration($imapConfiguration = null) { $currentImapConfiguration = $this->getImapConfiguration(); if ($currentImapConfiguration && (null === $imapConfiguration || $currentImapConfiguration->getId() !== $imapConfiguration->getId())) { // deactivate current IMAP configuration and remove a reference to it $currentImapConfiguration->setActive(false); $this->removeEmailOrigin($currentImapConfiguration); } if (null !== $imapConfiguration && null !== $imapConfiguration->getUser()) { $this->addEmailOrigin($imapConfiguration); } return $this; }
/** * Process send email message. In case exist custom smtp host/port use it * * @param \Swift_Message $message * @param UserEmailOrigin $emailOrigin * @throws \Swift_SwiftException */ public function processSend($message, $emailOrigin) { if ($emailOrigin instanceof UserEmailOrigin) { /* Modify transport smtp settings */ if ($emailOrigin->isSmtpConfigured()) { $this->mailer->prepareSmtpTransport($emailOrigin); } } if (!$this->mailer->send($message)) { throw new \Swift_SwiftException('An email was not delivered.'); } }
/** * Creates a processor is used to synchronize emails * * @param UserEmailOrigin $origin * @return ImapEmailSynchronizationProcessor */ protected function createSynchronizationProcessor($origin) { $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin)); return $this->syncProcessorFactory->create(new ImapEmailManager($this->connectorFactory->createImapConnector($config)), $this->getKnownEmailAddressChecker()); }
public function __construct($id = null, $user = null) { parent::__construct(); $this->id = $id; $this->owner = $user; }
/** * Return UserEmailOrigin entity created with data of $data variable */ protected function getUserEmailOrigin($data) { $userEmailOrigin = new UserEmailOrigin(); $userEmailOrigin->setUser($data['user']); $userEmailOrigin->setAccessTokenExpiresAt($data['accessTokenExpiresAt']); if (isset($data['password'])) { $userEmailOrigin->setPassword($data['password']); } if (isset($data['accessToken'])) { $userEmailOrigin->setAccessToken($data['accessToken']); } if (isset($data['refreshToken'])) { $userEmailOrigin->setRefreshToken($data['refreshToken']); } $organization = $this->getMock('Oro\\Bundle\\OrganizationBundle\\Entity\\Organization'); $userEmailOrigin->setOrganization($organization); return $userEmailOrigin; }
/** * Modify transport smtp settings * * @param UserEmailOrigin $userEmailOrigin */ protected function modifySmtpSettings(UserEmailOrigin $userEmailOrigin) { $transport = $this->mailer->getTransport(); if ($transport instanceof \Swift_Transport_EsmtpTransport) { $transport->setHost($userEmailOrigin->getSmtpHost()); $transport->setPort($userEmailOrigin->getSmtpPort()); $transport->setUsername($userEmailOrigin->getUser()); $transport->setPassword($this->encryptor->decryptData($userEmailOrigin->getPassword())); if ($userEmailOrigin->getSmtpEncryption()) { $transport->setEncryption($userEmailOrigin->getSmtpEncryption()); } } }
/** * Create object of AccountTypeModel using data of form * * @param array $data * * @return AccountTypeModel|null */ protected function createAccountTypeModelFromData($data) { $imapGmailConfiguration = isset($data['userEmailOrigin']) ? $data['userEmailOrigin'] : []; if (empty($imapGmailConfiguration['user'])) { return null; } $accountTypeModel = new AccountTypeModel(); $accountTypeModel->setAccountType($data['accountType']); $userEmailOrigin = new UserEmailOrigin(); $userEmailOrigin->setImapHost($imapGmailConfiguration['imapHost']); $userEmailOrigin->setImapPort($imapGmailConfiguration['imapPort']); $userEmailOrigin->setImapEncryption($imapGmailConfiguration['imapEncryption']); $userEmailOrigin->setUser($imapGmailConfiguration['user']); if (!empty($imapGmailConfiguration['accessTokenExpiresAt'])) { $newExpireDate = $imapGmailConfiguration['accessTokenExpiresAt']; if (!$newExpireDate instanceof \Datetime) { $utcTimeZone = new \DateTimeZone('UTC'); $accessTokenExpiresAt = $imapGmailConfiguration['accessTokenExpiresAt']; $newExpireDate = new \DateTime('+' . $accessTokenExpiresAt . ' seconds', $utcTimeZone); } $userEmailOrigin->setAccessTokenExpiresAt($newExpireDate); } $accountTypeModel->setUserEmailOrigin($userEmailOrigin); return $accountTypeModel; }
/** * @param UserEmailOrigin $origin * * @return string */ public function getAccessTokenWithCheckingExpiration(UserEmailOrigin $origin) { $expiresAt = $origin->getAccessTokenExpiresAt(); $utcTimeZone = new \DateTimeZone('UTC'); $now = new \DateTime('now', $utcTimeZone); $token = $origin->getAccessToken(); //if token had been expired, the new one must be generated and saved to DB if ($now > $expiresAt && $this->configManager->get('oro_imap.enable_google_imap') && $origin->getRefreshToken()) { $token = $this->getAccessToken($origin); } return $token; }