src/mvk/Auth/EventListener/PostAddListener.php line 32

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the Pimcore X Installation by
  4.  * ercas GmbH & CO. KG <https://www.ercasdieagentur.de>
  5.  *
  6.  *  @license GPLv3
  7.  */
  8. namespace App\mvk\Auth\EventListener;
  9. use App\Services\ContainerHelperService;
  10. use App\Services\MailService;
  11. use Pimcore\Event\Model\DataObjectEvent;
  12. use Pimcore\Event\Model\ElementEventInterface;
  13. class PostAddListener
  14. {
  15.     protected const USER_CLASSES = ['Agent'];
  16.     private $containerHelperService;
  17.     private MailService $mailService;
  18.     public function __construct(ContainerHelperService $containerHelperServiceMailService $mailService)
  19.     {
  20.         $this->containerHelperService $containerHelperService;
  21.         $this->mailService $mailService;
  22.     }
  23.     public function onPostAdd(ElementEventInterface $e): void
  24.     {
  25.         if ($e instanceof DataObjectEvent) {
  26.             $user $e->getObject();
  27.             $password$this->generatePassword();
  28.             if ($this->supports($user->getClassName())) {
  29.                 $user->setPassword($password);
  30.                 $user->setActive(false);
  31.                 $user->save();
  32.                 $this->sendMailAction($user$password);
  33.             }
  34.         }
  35.     }
  36.         private function supports($class)
  37.         {
  38.             if (in_array($classself::USER_CLASSES)) {
  39.                 return true;
  40.             }
  41.             return false;
  42.         }
  43.         private function sendMailAction($customer$password)
  44.         {
  45.             $emailDocumentId $this->containerHelperService->get('auth''FIRST_LOGIN_EMAIL_DOCUMENT_ID');
  46.             $username$customer->getUserName() !== NULL $customer->getUserName() : ' '
  47.             $mailParams = [
  48.                 'Vermittler Nr' => $username,
  49.                 'Passwort' => $password,
  50.                 'html'=>true,
  51.                 'import'=>true
  52.             ];
  53.             $mailType='company';
  54.             $this->mailService->sendMail($mailType$emailDocumentId$mailParams);
  55.         }
  56.         private function generatePassword()
  57.         {
  58.             $data '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
  59.             $dataSpecial'$#&?!';
  60.             $dataFinale=substr(str_shuffle($data), 06).substr(str_shuffle($dataSpecial), 02);
  61.             return $dataFinale;
  62.         }
  63. }