forked from FriendsOfSymfony/FOSUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserListener.php
85 lines (75 loc) · 2.32 KB
/
UserListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Document;
use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Doctrine MongoDB ODM listener updating the canonical fields and the password.
*
* @author Christophe Coevoet <[email protected]>
*/
class UserListener implements EventSubscriber
{
/**
* @var \FOS\UserBundle\Model\UserManagerInterface
*/
private $userManager;
/**
* @var ContainerInterface
*/
private $container;
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getSubscribedEvents()
{
return array(
Events::prePersist,
Events::preUpdate,
);
}
public function prePersist(LifecycleEventArgs $args)
{
$this->handleEvent($args);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$this->handleEvent($args);
}
private function handleEvent(LifecycleEventArgs $args)
{
$entity = $args->getDocument();
if ($entity instanceof UserInterface) {
if (null === $this->userManager) {
$this->userManager = $this->container->get('fos_user.user_manager');
}
$this->userManager->updateCanonicalFields($entity);
$this->userManager->updatePassword($entity);
if ($args instanceof PreUpdateEventArgs) {
// We are doing a update, so we must force Doctrine to update the
// changeset in case we changed something above
$dm = $args->getDocumentManager();
$uow = $dm->getUnitOfWork();
$meta = $dm->getClassMetadata(get_class($entity));
$uow->recomputeSingleDocumentChangeSet($meta, $entity);
}
}
}
}