src/Controller/Website/Auth/AbstractController.php line 225

Open in your IDE?
  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 App\Controller\Website\Auth;
  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\Security\Core\Encoder\UserPasswordEncoderInterface;
  27. /**
  28.  * Contains helper function for all controllers.
  29.  */
  30. abstract class AbstractController extends SymfonyAbstractController
  31. {
  32.     /**
  33.      * @var string
  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.             return $this->getRequestAnalyzer()->getWebspace()->getKey();
  50.         }
  51.         return $this->webspaceKey;
  52.     }
  53.     
  54.     /**
  55.      * Set Password and Salt by a Symfony Form.
  56.      */
  57.     protected function setUserPasswordAndSalt(User $user$form): User
  58.     {
  59.         
  60.         // $plainPassword = $form->get('plainPassword')->getData();
  61.         $plainPassword $form['_password'];
  62.         if (null === $plainPassword) {
  63.             return $user;
  64.         }
  65.         $salt $user->getSalt();
  66.         if (!$salt) {
  67.             $salt $this->getSaltGenerator()->getRandomSalt();
  68.         }
  69.         $user->setSalt($salt);
  70.         $password $this->getUserPasswordEncoder()->encodePassword($user$plainPassword);
  71.         $user->setPassword($password);
  72.         return $user;
  73.     }
  74.     // public function login(User $user, Request $request): void
  75.     // {
  76.     //     if (!$user->getEnabled()) {
  77.     //         return;
  78.     //     }
  79.     //     $token = new UsernamePasswordToken(
  80.     //         $user,
  81.     //         null,
  82.     //         $this->getConfigProperty(Configuration::FIREWALL),
  83.     //         $user->getRoles()
  84.     //     );
  85.     //     $this->tokenStorage->setToken($token);
  86.     //     $event = new InteractiveLoginEvent($request, $token);
  87.     //     $this->eventDispatcher->dispatch($event, SecurityEvents::INTERACTIVE_LOGIN);
  88.     // }
  89.     // public function sendEmails(string $type, User $user): void
  90.     // {
  91.     //     $this->mailFactory->sendEmails(
  92.     //         Mail::create(
  93.     //             $this->getConfigProperty(Configuration::EMAIL_FROM),
  94.     //             $this->getConfigProperty(Configuration::EMAIL_TO),
  95.     //             $this->getConfigTypeProperty($type, Configuration::EMAIL)
  96.     //         ),
  97.     //         $user
  98.     //     );
  99.     // }
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     // public function saveProfile(User $user): ?User
  104.     // {
  105.     //     $this->userManager->updateUser($user);
  106.     //     // Event
  107.     //     $event = new UserProfileSavedEvent($user, $this->config);
  108.     //     $this->eventDispatcher->dispatch($event);
  109.     //     return $user;
  110.     // }
  111.     /**
  112.      * Check if user should be logged in.
  113.      */
  114.     // protected function checkAutoLogin(string $type): bool
  115.     // {
  116.     //     return $this->getCommunityManager($this->getWebspaceKey())->getConfigTypeProperty(
  117.     //         $type,
  118.     //         Configuration::AUTO_LOGIN
  119.     //     );
  120.     // }
  121.     /**
  122.      * Render a specific type template.
  123.      *
  124.      * @param mixed[] $data
  125.      */
  126.     // protected function renderTemplate(string $type, array $data = []): Response
  127.     // {
  128.     //     return $this->render(
  129.     //         $this->getCommunityManager($this->getWebspaceKey())->getConfigTypeProperty(
  130.     //             $type,
  131.     //             Configuration::TEMPLATE
  132.     //         ),
  133.     //         $data
  134.     //     );
  135.     // }
  136.     /**
  137.      * Save all persisted entities.
  138.      */
  139.     protected function saveEntities(): void
  140.     {
  141.         $this->getEntityManager()->flush();
  142.     }
  143.     /**
  144.      * Set Sulu template attributes.
  145.      *
  146.      * @param mixed[] $custom
  147.      *
  148.      * @return mixed[]
  149.      */
  150.     private function getTemplateAttributes(array $custom = []): array
  151.     {
  152.         return $this->getTemplateAttributeResolver()->resolve($custom);
  153.     }
  154.     /**
  155.      * {@inheritdoc}
  156.      *
  157.      * @return User
  158.      */
  159.     public function getUser(): ?User
  160.     {
  161.         $user parent::getUser();
  162.         if (!$user instanceof User) {
  163.             throw new HttpException(403);
  164.         }
  165.         if (null === $user->getContact()->getMainAddress()) {
  166.             // TODO this should be done by the form type not by the controller
  167.             $this->addAddress($user);
  168.         }
  169.         return $user;
  170.     }
  171.     /**
  172.      * Add address to user.
  173.      */
  174.     private function addAddress(User $user): void
  175.     {
  176.         $contact $user->getContact();
  177.         $address = new Address();
  178.         $address->setPrimaryAddress(true);
  179.         $address->setNote('');
  180.         /** @var AddressType $addressType */
  181.         $addressType $this->getEntityManager()->getReference(AddressType::class, 1);
  182.         $address->setAddressType($addressType);
  183.         $contactAddress = new ContactAddress();
  184.         $contactAddress->setMain(true);
  185.         $contactAddress->setAddress($address);
  186.         $contactAddress->setContact($contact);
  187.         $contact->addContactAddress($contactAddress);
  188.     }
  189.     /**
  190.      * {@inheritdoc}
  191.      *
  192.      * @param mixed[] $parameters
  193.      */
  194.     public function render(string $view, array $parameters = [], Response $response null): Response
  195.     {
  196.         return parent::render(
  197.             $view,
  198.             $this->getTemplateAttributes($parameters),
  199.             $response
  200.         );
  201.     }
  202.     /**
  203.      * {@inheritdoc}
  204.      *
  205.      * @param mixed[] $parameters
  206.      */
  207.     public function renderView(string $view, array $parameters = []): string
  208.     {
  209.         return parent::renderView($view$this->getTemplateAttributes($parameters));
  210.     }
  211.     // protected function getCommunityManagerRegistry(): CommunityManagerRegistryInterface
  212.     // {
  213.     //     return $this->container->get('sulu_community.community_manager.registry');
  214.     // }
  215.     protected function getRequestAnalyzer(): RequestAnalyzerInterface
  216.     {
  217.         return $this->container->get('sulu_core.webspace.request_analyzer');
  218.     }
  219.     protected function getSaltGenerator(): SaltGenerator
  220.     {
  221.         return $this->container->get('sulu_security.salt_generator');
  222.     }
  223.     protected function getUserPasswordEncoder(): UserPasswordEncoderInterface
  224.     {
  225.         return $this->container->get('security.password_encoder');
  226.     }
  227.     protected function getTemplateAttributeResolver(): TemplateAttributeResolverInterface
  228.     {
  229.         return $this->container->get('sulu_website.resolver.template_attribute');
  230.     }
  231.     protected function getEntityManager(): EntityManagerInterface
  232.     {
  233.         return $this->container->get('doctrine.orm.entity_manager');
  234.     }
  235.     /**
  236.      * @return array<string|int, string>
  237.      */
  238.     public static function getSubscribedServices(): array
  239.     {
  240.         $subscribedServices parent::getSubscribedServices();
  241.         // $subscribedServices['sulu_community.community_manager.registry'] = CommunityManagerRegistryInterface::class;
  242.         $subscribedServices['sulu_core.webspace.request_analyzer'] = RequestAnalyzerInterface::class;
  243.         $subscribedServices['sulu_security.salt_generator'] = SaltGenerator::class;
  244.         $subscribedServices['security.password_encoder'] = UserPasswordEncoderInterface::class;
  245.         $subscribedServices['sulu_website.resolver.template_attribute'] = TemplateAttributeResolverInterface::class;
  246.         $subscribedServices['doctrine.orm.entity_manager'] = EntityManagerInterface::class;
  247.         return $subscribedServices;
  248.     }
  249. }