aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php')
-rw-r--r--src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php b/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php
new file mode 100644
index 00000000..92626b15
--- /dev/null
+++ b/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php
@@ -0,0 +1,54 @@
1<?php
2
3namespace Wallabag\FederationBundle\EventListener;
4
5use Doctrine\ORM\EntityManager;
6use FOS\UserBundle\Event\UserEvent;
7use FOS\UserBundle\FOSUserEvents;
8use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10use Wallabag\CoreBundle\Entity\Config;
11use Wallabag\FederationBundle\Entity\Account;
12
13/**
14 * This listener will create the associated configuration when a user register.
15 * This configuration will be created right after the registration (no matter if it needs an email validation).
16 */
17class CreateAccountListener implements EventSubscriberInterface
18{
19 private $em;
20 private $domainName;
21
22 public function __construct(EntityManager $em, $domainName)
23 {
24 $this->em = $em;
25 $this->domainName = $domainName;
26 }
27
28 public static function getSubscribedEvents()
29 {
30 return [
31 // when a user register using the normal form
32 FOSUserEvents::REGISTRATION_COMPLETED => 'createAccount',
33 // when we manually create a user using the command line
34 // OR when we create it from the config UI
35 FOSUserEvents::USER_CREATED => 'createAccount',
36 ];
37 }
38
39 public function createAccount(UserEvent $event)
40 {
41 $user = $event->getUser();
42 $account = new Account();
43 $account->setUser($user)
44 ->setUsername($user->getUsername())
45 ->setServer($this->domainName);
46
47 $this->em->persist($account);
48
49 $user->setAccount($account);
50
51 $this->em->persist($user);
52 $this->em->flush();
53 }
54}