vendor/sulu/community-bundle/Controller/AbstractController.php line 212

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\CommunityBundle\Controller;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
  13. use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface;
  14. use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerRegistryInterface;
  15. use Sulu\Bundle\ContactBundle\Entity\Address;
  16. use Sulu\Bundle\ContactBundle\Entity\AddressType;
  17. use Sulu\Bundle\ContactBundle\Entity\ContactAddress;
  18. use Sulu\Bundle\SecurityBundle\Entity\User;
  19. use Sulu\Bundle\WebsiteBundle\Resolver\TemplateAttributeResolverInterface;
  20. use Sulu\Component\Security\Authentication\SaltGenerator;
  21. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SymfonyAbstractController;
  23. use Symfony\Component\Form\FormInterface;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpKernel\Exception\HttpException;
  26. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  27. /**
  28.  * Contains helper function for all controllers.
  29.  */
  30. abstract class AbstractController extends SymfonyAbstractController
  31. {
  32.     /**
  33.      * @var string|null
  34.      */
  35.     private $webspaceKey;
  36.     /**
  37.      * Returns current or specific communityManager.
  38.      */
  39.     protected function getCommunityManager(string $webspaceKey): CommunityManagerInterface
  40.     {
  41.         return $this->getCommunityManagerRegistry()->get($webspaceKey);
  42.     }
  43.     /**
  44.      * Returns current webspace key.
  45.      */
  46.     protected function getWebspaceKey(): string
  47.     {
  48.         if (null === $this->webspaceKey) {
  49.             $this->webspaceKey $this->getRequestAnalyzer()->getWebspace()->getKey();
  50.         }
  51.         return $this->webspaceKey;
  52.     }
  53.     /**
  54.      * Set Password and Salt by a Symfony Form.
  55.      */
  56.     protected function setUserPasswordAndSalt(User $userFormInterface $form): User
  57.     {
  58.         /** @var string|null $plainPassword */
  59.         $plainPassword $form->get('plainPassword')->getData();
  60.         if (null === $plainPassword) {
  61.             return $user;
  62.         }
  63.         $salt $user->getSalt();
  64.         if (!$salt) {
  65.             $salt $this->getSaltGenerator()->getRandomSalt();
  66.         }
  67.         $user->setSalt($salt);
  68.         $password $this->encodePassword($user$plainPassword);
  69.         $user->setPassword($password);
  70.         return $user;
  71.     }
  72.     protected function encodePassword(User $userstring $plainPassword): string
  73.     {
  74.         if ($this->container->has('?security.password_hasher')) {
  75.             /** @var UserPasswordHasherInterface $hasher */
  76.             $hasher $this->container->get('?security.password_hasher');
  77.             return $hasher->hashPassword($user$plainPassword);
  78.         } else {
  79.             /** @var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface $encoder */
  80.             $encoder $this->container->get('?security.password_encoder');
  81.             return $encoder->encodePassword($user$plainPassword);
  82.         }
  83.     }
  84.     /**
  85.      * Check if user should be logged in.
  86.      */
  87.     protected function checkAutoLogin(string $type): bool
  88.     {
  89.         /** @var bool */
  90.         return $this->getCommunityManager($this->getWebspaceKey())->getConfigTypeProperty(
  91.             $type,
  92.             Configuration::AUTO_LOGIN
  93.         );
  94.     }
  95.     /**
  96.      * Render a specific type template.
  97.      *
  98.      * @param mixed[] $data
  99.      */
  100.     protected function renderTemplate(string $type, array $data = []): Response
  101.     {
  102.         /** @var string $template */
  103.         $template $this->getCommunityManager($this->getWebspaceKey())->getConfigTypeProperty(
  104.             $type,
  105.             Configuration::TEMPLATE
  106.         );
  107.         return $this->render(
  108.             $template,
  109.             $data
  110.         );
  111.     }
  112.     /**
  113.      * Save all persisted entities.
  114.      */
  115.     protected function saveEntities(): void
  116.     {
  117.         $this->getEntityManager()->flush();
  118.     }
  119.     /**
  120.      * Set Sulu template attributes.
  121.      *
  122.      * @param mixed[] $custom
  123.      *
  124.      * @return mixed[]
  125.      */
  126.     private function getTemplateAttributes(array $custom = []): array
  127.     {
  128.         return $this->getTemplateAttributeResolver()->resolve($custom);
  129.     }
  130.     /**
  131.      * @return User
  132.      */
  133.     public function getUser(): ?User
  134.     {
  135.         $user parent::getUser();
  136.         if (!$user instanceof User) {
  137.             throw new HttpException(403);
  138.         }
  139.         if (null === $user->getContact()->getMainAddress()) {
  140.             // TODO this should be done by the form type not by the controller
  141.             $this->addAddress($user);
  142.         }
  143.         return $user;
  144.     }
  145.     /**
  146.      * Add address to user.
  147.      */
  148.     private function addAddress(User $user): void
  149.     {
  150.         $contact $user->getContact();
  151.         $address = new Address();
  152.         $address->setPrimaryAddress(true);
  153.         $address->setNote('');
  154.         /** @var AddressType $addressType */
  155.         $addressType $this->getEntityManager()->getReference(AddressType::class, 1);
  156.         $address->setAddressType($addressType);
  157.         $contactAddress = new ContactAddress();
  158.         $contactAddress->setMain(true);
  159.         $contactAddress->setAddress($address);
  160.         $contactAddress->setContact($contact);
  161.         $contact->addContactAddress($contactAddress);
  162.     }
  163.     /**
  164.      * @param mixed[] $parameters
  165.      */
  166.     public function render(string $view, array $parameters = [], Response $response null): Response
  167.     {
  168.         return parent::render(
  169.             $view,
  170.             $this->getTemplateAttributes($parameters),
  171.             $response
  172.         );
  173.     }
  174.     /**
  175.      * @param mixed[] $parameters
  176.      */
  177.     public function renderView(string $view, array $parameters = []): string
  178.     {
  179.         return parent::renderView($view$this->getTemplateAttributes($parameters));
  180.     }
  181.     protected function getCommunityManagerRegistry(): CommunityManagerRegistryInterface
  182.     {
  183.         return $this->container->get('sulu_community.community_manager.registry');
  184.     }
  185.     protected function getRequestAnalyzer(): RequestAnalyzerInterface
  186.     {
  187.         return $this->container->get('sulu_core.webspace.request_analyzer');
  188.     }
  189.     protected function getSaltGenerator(): SaltGenerator
  190.     {
  191.         return $this->container->get('sulu_security.salt_generator');
  192.     }
  193.     protected function getTemplateAttributeResolver(): TemplateAttributeResolverInterface
  194.     {
  195.         return $this->container->get('sulu_website.resolver.template_attribute');
  196.     }
  197.     protected function getEntityManager(): EntityManagerInterface
  198.     {
  199.         return $this->container->get('doctrine.orm.entity_manager');
  200.     }
  201.     /**
  202.      * @return array<string|int, string>
  203.      */
  204.     public static function getSubscribedServices(): array
  205.     {
  206.         $subscribedServices parent::getSubscribedServices();
  207.         $subscribedServices['sulu_community.community_manager.registry'] = CommunityManagerRegistryInterface::class;
  208.         $subscribedServices['sulu_core.webspace.request_analyzer'] = RequestAnalyzerInterface::class;
  209.         $subscribedServices['sulu_security.salt_generator'] = SaltGenerator::class;
  210.         $subscribedServices['sulu_website.resolver.template_attribute'] = TemplateAttributeResolverInterface::class;
  211.         $subscribedServices['doctrine.orm.entity_manager'] = EntityManagerInterface::class;
  212.         $subscribedServices['?security.password_hasher'] = UserPasswordHasherInterface::class;
  213.         if (\class_exists('Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface')) {
  214.             $subscribedServices['?security.password_encoder'] = 'Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface';
  215.         }
  216.         return $subscribedServices;
  217.     }
  218. }