1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
15
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvents; |
16
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent; |
17
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
21
|
|
|
|
22
|
|
|
class PrepareUserListener implements EventSubscriberInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EventDispatcherInterface |
26
|
|
|
*/ |
27
|
|
|
private $eventDispatcher; |
28
|
|
|
|
29
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
30
|
|
|
{ |
31
|
|
|
$this->eventDispatcher = $eventDispatcher; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function getSubscribedEvents(): array |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
FormEvents::FORM_DATA_PRE_SAVE => [ |
38
|
|
|
'prepareAndDispatchUserCreated', |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function prepareAndDispatchUserCreated(FormEvent $event): void |
44
|
|
|
{ |
45
|
|
|
$entity = $event->getForm()->getData(); |
46
|
|
|
|
47
|
|
|
if (!$entity instanceof UserInterface) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// do not dispatch event and do not set random password for existing users |
52
|
|
|
if ($entity->getPassword()) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$entity->setEnabled(false); |
57
|
|
|
$entity->enforcePasswordChange(true); |
58
|
|
|
$entity->setPlainPassword(bin2hex(random_bytes(32))); |
59
|
|
|
|
60
|
|
|
$this->eventDispatcher->dispatch( |
61
|
|
|
AdminSecurityEvents::USER_CREATED, |
|
|
|
|
62
|
|
|
new UserEvent($entity) |
|
|
|
|
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|