Esempio n. 1
0
 /**
  * @param object $entity
  * @return bool
  */
 public function supportOwnerProvider($entity)
 {
     foreach ($this->emailOwnerStorage->getProviders() as $provider) {
         if ($this->activityListChainProvider->isSupportedTargetEntity($entity) && $provider->getEmailOwnerClass() === ClassUtils::getClass($entity)) {
             return true;
         }
     }
     return false;
 }
Esempio n. 2
0
 /**
  * Find an entity object which is an owner of the given email address
  *
  * @param \Doctrine\ORM\EntityManager $em
  * @param string $email
  * @return EmailOwnerInterface
  */
 public function findEmailOwner(EntityManager $em, $email)
 {
     $emailOwner = null;
     foreach ($this->emailOwnerProviderStorage->getProviders() as $provider) {
         $emailOwner = $provider->findEmailOwner($em, $email);
         if ($emailOwner !== null) {
             break;
         }
     }
     return $emailOwner;
 }
 /**
  * @param string[] $emailsToLoad
  *
  * @return array
  *  key   = email address
  *  value = array
  *      known => true/false
  *          false if the address belongs to user only
  *          true if the address belongs to not excluded owners
  *      user  => user id
  */
 protected function getKnownEmailAddresses(array $emailsToLoad)
 {
     $repo = $this->emailAddressManager->getEmailAddressRepository($this->em);
     $qb = $repo->createQueryBuilder('a')->where('a.hasOwner = :hasOwner AND a.email IN (:emails)')->setParameter('hasOwner', true)->setParameter('emails', $emailsToLoad);
     $select = 'a.email';
     $userIdField = null;
     $mailboxIdField = null;
     $ownerIdFields = [];
     foreach ($this->emailOwnerProviderStorage->getProviders() as $provider) {
         $ownerClass = $provider->getEmailOwnerClass();
         $isUser = $ownerClass === 'Oro\\Bundle\\UserBundle\\Entity\\User';
         $isMailbox = $ownerClass === 'Oro\\Bundle\\EmailBundle\\Entity\\Mailbox';
         $field = $this->emailOwnerProviderStorage->getEmailOwnerFieldName($provider);
         if ($isUser) {
             $userIdField = $field;
         }
         if ($isMailbox) {
             $mailboxIdField = $field;
         }
         if (isset($this->exclusions[$ownerClass])) {
             if ($isUser) {
                 $select .= sprintf(',IDENTITY(a.%1$s) AS %1$s', $field);
             } else {
                 $qb->andWhere(sprintf('a.%s IS NULL', $field));
             }
         } else {
             $select .= sprintf(',IDENTITY(a.%1$s) AS %1$s', $field);
             $ownerIdFields[] = $field;
         }
     }
     $qb->select($select);
     $data = $qb->getQuery()->getArrayResult();
     $result = $this->prepareKnownEmailAddressesData($data, $ownerIdFields, $userIdField, $mailboxIdField);
     return $result;
 }
Esempio n. 4
0
 /**
  * Constructor.
  *
  * @param EmailOwnerProviderStorage $emailOwnerProviderStorage
  * @param EmailAddressManager       $emailAddressManager
  */
 public function __construct(EmailOwnerProviderStorage $emailOwnerProviderStorage, EmailAddressManager $emailAddressManager)
 {
     foreach ($emailOwnerProviderStorage->getProviders() as $provider) {
         $fieldName = $emailOwnerProviderStorage->getEmailOwnerFieldName($provider);
         $this->emailOwnerClasses[$fieldName] = $provider->getEmailOwnerClass();
     }
     $this->emailAddressManager = $emailAddressManager;
 }
 /**
  * Constructor.
  *
  * @param EmailOwnerProviderStorage $emailOwnerProviderStorage
  * @param EmailAddressManager $emailAddressManager
  */
 public function __construct(EmailOwnerProviderStorage $emailOwnerProviderStorage, EmailAddressManager $emailAddressManager)
 {
     foreach ($emailOwnerProviderStorage->getProviders() as $provider) {
         $fieldName = sprintf('owner%d', count($this->emailOwnerClasses) + 1);
         $this->emailOwnerClasses[$fieldName] = $provider->getEmailOwnerClass();
     }
     $this->emailAddressManager = $emailAddressManager;
 }
 /**
  * Constructor.
  *
  * @param EmailOwnerProviderStorage $emailOwnerProviderStorage
  * @param string $entityCacheDir
  * @param string $entityCacheNamespace
  * @param string $entityProxyNameTemplate
  */
 public function __construct(EmailOwnerProviderStorage $emailOwnerProviderStorage, $entityCacheDir, $entityCacheNamespace, $entityProxyNameTemplate)
 {
     foreach ($emailOwnerProviderStorage->getProviders() as $provider) {
         $this->emailOwnerClasses[count($this->emailOwnerClasses) + 1] = $provider->getEmailOwnerClass();
     }
     $this->entityCacheDir = $entityCacheDir;
     $this->entityCacheNamespace = $entityCacheNamespace;
     $this->entityProxyNameTemplate = $entityProxyNameTemplate;
 }
Esempio n. 7
0
 /**
  * @param string $emailFromTableAlias EmailAddress table alias of joined Email#fromEmailAddress association
  *
  * @return string
  */
 protected function getFromEmailExpression($emailFromTableAlias)
 {
     $providers = $this->emailOwnerProviderStorage->getProviders();
     if (empty($providers)) {
         return sprintf('%s.email', $emailFromTableAlias);
     }
     $expressionsByOwner = [];
     foreach ($providers as $provider) {
         $relationAlias = $this->emailOwnerProviderStorage->getEmailOwnerFieldName($provider);
         $expressionsByOwner[$relationAlias] = $this->entityNameResolver->getNameDQL($provider->getEmailOwnerClass(), $relationAlias);
     }
     $expression = '';
     foreach ($expressionsByOwner as $alias => $expressionPart) {
         $expression .= sprintf('WHEN %s.%s IS NOT NULL THEN %s ', $emailFromTableAlias, $alias, $expressionPart);
     }
     $expression = sprintf('CASE %sELSE \'\' END', $expression);
     // if has owner then use expression to expose formatted name, use email otherwise
     return sprintf('CONCAT(\'\', CASE WHEN %1$s.hasOwner = true THEN (%2$s) ELSE %1$s.email END) as fromEmailExpression', $emailFromTableAlias, $expression);
 }
 public function testStorage()
 {
     $provider1 = $this->getMock('Oro\\Bundle\\EmailBundle\\Entity\\Provider\\EmailOwnerProviderInterface');
     $provider2 = $this->getMock('Oro\\Bundle\\EmailBundle\\Entity\\Provider\\EmailOwnerProviderInterface');
     $storage = new EmailOwnerProviderStorage();
     $storage->addProvider($provider1);
     $storage->addProvider($provider2);
     $result = $storage->getProviders();
     $this->assertCount(2, $result);
     $this->assertTrue($provider1 === $result[0]);
     $this->assertTrue($provider2 === $result[1]);
 }
 /**
  * @param string[] $emailsToLoad
  *
  * @return array
  *  key   = email address
  *  value = array
  *      known => true/false
  *          false if the address belongs to user only
  *          true if the address belongs to not excluded owners
  *      user  => user id
  */
 protected function getKnownEmailAddresses(array $emailsToLoad)
 {
     $repo = $this->emailAddressManager->getEmailAddressRepository($this->em);
     $qb = $repo->createQueryBuilder('a')->where('a.hasOwner = :hasOwner AND a.email IN (:emails)')->setParameter('hasOwner', true)->setParameter('emails', $emailsToLoad);
     $select = 'a.email';
     $userIdField = null;
     $mailboxIdField = null;
     $ownerIdFields = [];
     foreach ($this->emailOwnerProviderStorage->getProviders() as $provider) {
         $ownerClass = $provider->getEmailOwnerClass();
         $isUser = $ownerClass === 'Oro\\Bundle\\UserBundle\\Entity\\User';
         $isMailbox = $ownerClass === 'Oro\\Bundle\\EmailBundle\\Entity\\Mailbox';
         $field = $this->emailOwnerProviderStorage->getEmailOwnerFieldName($provider);
         if ($isUser) {
             $userIdField = $field;
         }
         if ($isMailbox) {
             $mailboxIdField = $field;
         }
         if (isset($this->exclusions[$ownerClass])) {
             if ($isUser) {
                 $select .= sprintf(',IDENTITY(a.%1$s) AS %1$s', $field);
             } else {
                 $qb->andWhere(sprintf('a.%s IS NULL', $field));
             }
         } else {
             $select .= sprintf(',IDENTITY(a.%1$s) AS %1$s', $field);
             $ownerIdFields[] = $field;
         }
     }
     $qb->select($select);
     $result = [];
     $data = $qb->getQuery()->getArrayResult();
     foreach ($data as $item) {
         $known = false;
         foreach ($ownerIdFields as $field) {
             if ($item[$field] !== null) {
                 $known = true;
                 break;
             }
         }
         $email = strtolower($item['email']);
         $userId = $item[$userIdField];
         $mailboxId = $item[$mailboxIdField];
         $result[$email] = $userId === null ? ['known' => $known] : ['known' => $known, 'user' => (int) $userId];
         if ($mailboxId !== null) {
             $result[$email]['mailbox'] = $mailboxId;
         }
     }
     return $result;
 }
Esempio n. 10
0
 /**
  * Create a proxy class for EmailAddress entity and save it in cache
  *
  * @param Filesystem        $fs
  * @param \Twig_Environment $twig
  */
 protected function processEmailAddressTemplate(Filesystem $fs, \Twig_Environment $twig)
 {
     // Ensure the cache directory exists
     if (!$fs->exists($this->entityCacheDir)) {
         $fs->mkdir($this->entityCacheDir, 0777);
     }
     $args = array();
     $providers = $this->emailOwnerProviderStorage->getProviders();
     foreach ($providers as $provider) {
         $args[] = array('targetEntity' => $provider->getEmailOwnerClass(), 'columnName' => $this->emailOwnerProviderStorage->getEmailOwnerColumnName($provider), 'fieldName' => $this->emailOwnerProviderStorage->getEmailOwnerFieldName($provider));
     }
     $className = sprintf($this->entityProxyNameTemplate, 'EmailAddress');
     $twigParams = array('namespace' => $this->entityCacheNamespace, 'className' => $className, 'owners' => $args);
     // generate a proxy class
     $content = $twig->render('EmailAddress.php.twig', $twigParams);
     $this->writeCacheFile(sprintf('%s%s%s.php', $this->entityCacheDir, DIRECTORY_SEPARATOR, $className), $content);
     // generate ORM mappings for a proxy class
     $content = $twig->render('EmailAddress.orm.yml.twig', $twigParams);
     $this->writeCacheFile(sprintf('%s%s%s.orm.yml', $this->entityCacheDir, DIRECTORY_SEPARATOR, $className), $content);
 }
 /**
  * @param RegistryInterface $registry
  * @param string $className
  * @param EmailOwnerProviderStorage $emailOwnerProviderStorage
  * @param UserConfigManager $userConfigManager
  */
 public function __construct(RegistryInterface $registry, $className, EmailOwnerProviderStorage $emailOwnerProviderStorage, UserConfigManager $userConfigManager)
 {
     $this->registry = $registry;
     $this->className = $className;
     $this->alias = 'o';
     // default
     for ($i = 1; $i <= count($emailOwnerProviderStorage->getProviders()); $i++) {
         $this->emailOwnerFieldNames[] = sprintf('owner%d', $i);
     }
     $firstNames = array();
     $lastNames = array();
     foreach ($this->emailOwnerFieldNames as $fieldName) {
         $firstNames[] = sprintf('%s.firstName', $fieldName);
         $lastNames[] = sprintf('%s.lastName', $fieldName);
     }
     $firstName = sprintf('COALESCE(%s, \'\')', implode(', ', $firstNames));
     $lastName = sprintf('COALESCE(%s, \'\')', implode(', ', $lastNames));
     // TODO: refactor usage of email name formats https://magecore.atlassian.net/browse/BAP-2007
     $nameFormat = '%first% %last%';
     $this->fromEmailExpression = $this->buildFromEmailExpression($nameFormat, $firstName, $lastName) . ' as FromEmailExpression';
 }
Esempio n. 12
0
 /**
  * @return string
  */
 protected function getUserOwnerFieldName()
 {
     return $this->emailOwnerProviderStorage->getEmailOwnerFieldName(ArrayUtil::find(function (EmailOwnerProviderInterface $provider) {
         return $provider->getEmailOwnerClass() === 'Oro\\Bundle\\UserBundle\\Entity\\User';
     }, $this->emailOwnerProviderStorage->getProviders()));
 }