core/lib/Thelia/Action/Administrator.php line 59

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\Administrator\AdministratorEvent;
  15. use Thelia\Core\Event\Administrator\AdministratorUpdatePasswordEvent;
  16. use Thelia\Core\Event\TheliaEvents;
  17. use Thelia\Mailer\MailerFactory;
  18. use Thelia\Model\Admin as AdminModel;
  19. use Thelia\Model\AdminQuery;
  20. use Thelia\Model\ConfigQuery;
  21. use Thelia\Tools\TokenProvider;
  22. class Administrator extends BaseAction implements EventSubscriberInterface
  23. {
  24.     /** @var MailerFactory */
  25.     protected $mailer;
  26.     /** @var TokenProvider */
  27.     protected $tokenProvider;
  28.     public function __construct(MailerFactory $mailerTokenProvider $tokenProvider)
  29.     {
  30.         $this->mailer $mailer;
  31.         $this->tokenProvider $tokenProvider;
  32.     }
  33.     public function create(AdministratorEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  34.     {
  35.         $administrator = new AdminModel();
  36.         $administrator
  37.             ->setFirstname($event->getFirstname())
  38.             ->setLastname($event->getLastname())
  39.             ->setEmail($event->getEmail())
  40.             ->setLogin($event->getLogin())
  41.             ->setPassword($event->getPassword())
  42.             ->setProfileId($event->getProfile())
  43.             ->setLocale($event->getLocale())
  44.         ;
  45.         $administrator->save();
  46.         $event->setAdministrator($administrator);
  47.     }
  48.     public function update(AdministratorEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  49.     {
  50.         if (null !== $administrator AdminQuery::create()->findPk($event->getId())) {
  51.             $administrator
  52.                 ->setFirstname($event->getFirstname())
  53.                 ->setLastname($event->getLastname())
  54.                 ->setLogin($event->getLogin())
  55.                 ->setEmail($event->getEmail())
  56.                 ->setProfileId($event->getProfile())
  57.                 ->setLocale($event->getLocale())
  58.             ;
  59.             if ('' !== $event->getPassword()) {
  60.                 $administrator->setPassword($event->getPassword());
  61.             }
  62.             $administrator->save();
  63.             $event->setAdministrator($administrator);
  64.         }
  65.     }
  66.     public function delete(AdministratorEvent $event): void
  67.     {
  68.         if (null !== $administrator AdminQuery::create()->findPk($event->getId())) {
  69.             $administrator
  70.                 ->delete()
  71.             ;
  72.             $event->setAdministrator($administrator);
  73.         }
  74.     }
  75.     public function updatePassword(AdministratorUpdatePasswordEvent $event): void
  76.     {
  77.         $admin $event->getAdmin();
  78.         $admin
  79.             ->setPassword($event->getPassword())
  80.             ->setPasswordRenewToken(null)
  81.             ->save();
  82.     }
  83.     public function createPassword(AdministratorEvent $event): void
  84.     {
  85.         $admin $event->getAdministrator();
  86.         $email $admin->getEmail();
  87.         if (!empty($email)) {
  88.             $renewToken $this->tokenProvider->getToken();
  89.             $admin
  90.                 ->setPasswordRenewToken($renewToken)
  91.                 ->save();
  92.             $this->mailer->sendEmailMessage(
  93.                 'new_admin_password',
  94.                 [ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName()],
  95.                 [$email => $admin->getFirstname().' '.$admin->getLastname()],
  96.                 [
  97.                     'token' => $renewToken,
  98.                     'admin' => $admin,
  99.                 ]
  100.             );
  101.         }
  102.     }
  103.     /**
  104.      * {@inheritdoc}
  105.      */
  106.     public static function getSubscribedEvents()
  107.     {
  108.         return [
  109.             TheliaEvents::ADMINISTRATOR_CREATE => ['create'128],
  110.             TheliaEvents::ADMINISTRATOR_UPDATE => ['update'128],
  111.             TheliaEvents::ADMINISTRATOR_DELETE => ['delete'128],
  112.             TheliaEvents::ADMINISTRATOR_UPDATEPASSWORD => ['updatePassword'128],
  113.             TheliaEvents::ADMINISTRATOR_CREATEPASSWORD => ['createPassword'128],
  114.         ];
  115.     }
  116. }