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
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\EventListener; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
13
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvents; |
14
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent; |
15
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
19
|
|
|
use Symfony\Component\Security\Core\Util\SecureRandomInterface; |
20
|
|
|
|
21
|
|
|
class PrepareUserListener implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var EventDispatcherInterface |
25
|
|
|
*/ |
26
|
|
|
private $eventDispatcher; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SecureRandomInterface |
30
|
|
|
*/ |
31
|
|
|
private $secureRandom; |
32
|
|
|
|
33
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, SecureRandomInterface $secureRandom) |
34
|
|
|
{ |
35
|
|
|
$this->eventDispatcher = $eventDispatcher; |
36
|
|
|
$this->secureRandom = $secureRandom; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
FormEvents::FORM_DATA_PRE_SAVE => [ |
46
|
|
|
'prepareAndDispatchUserCreated', |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function prepareAndDispatchUserCreated(FormEvent $event) |
52
|
|
|
{ |
53
|
|
|
$entity = $event->getForm()->getData(); |
54
|
|
|
|
55
|
|
|
if (!$entity instanceof UserInterface) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// do not dispatch event and do not set random password for existing users |
60
|
|
|
if ($entity->getPassword()) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$entity->setEnabled(false); |
65
|
|
|
$entity->enforcePasswordChange(true); |
66
|
|
|
$entity->setPlainPassword($this->secureRandom->nextBytes(32)); |
67
|
|
|
|
68
|
|
|
$this->eventDispatcher->dispatch( |
69
|
|
|
AdminSecurityEvents::USER_CREATED, |
70
|
|
|
new UserEvent($entity) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|