Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new FOSUserBundle users to a default group on creation

I'm building my first serious Symfony2 project. I'm extending the FOSUserBundle for my user/group management, and I'd like new users to be automatically added to a default group. I guess you just have to extend the User entity constructor like this :

/**
 * Constructor
 */
public function __construct()
{
    parent::__construct();
    $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    // Get $defaultGroup entity somehow ???
    ...
    // Add that group entity to my new user :
    $this->addGroup($defaultGroup);
}

But my question is how do I get my $defaultGroup entity in the first place?

I tried using the entity manager from within the entity, but then I realized it was stupid, and Symfony was throwing an error. I googled for this, but found no real solution except maybe setting up a service for that... although this seems quite unclear for me.

like image 738
MoskitoHero Avatar asked Mar 11 '13 05:03

MoskitoHero


2 Answers

OK, I started working on implementing artworkad's idea.

First thing I did was updating FOSUserBundle to 2.0.*@dev in composer.json, because I was using v1.3.1, which doesn't implement the FOSUserEvents class. This is required to subscribe to my registration event.

// composer.json
"friendsofsymfony/user-bundle": "2.0.*@dev",

Then I added a new service :

<!-- Moskito/Bundle/UserBundle/Resources/config/services.xml -->
<service id="moskito_bundle_user.user_creation" class="Moskito\Bundle\UserBundle\EventListener\UserCreationListener">
    <tag name="kernel.event_subscriber" alias="moskito_user_creation_listener" />
        <argument type="service" id="doctrine.orm.entity_manager"/>
</service>

In the XML, I told the service I needed access to Doctrine through an argument doctrine.orm.entity_manager. Then, I created the Listener :

// Moskito/Bundle/UserBundle/EventListener/UserCreationListener.php

<?php
namespace Moskito\Bundle\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManager;

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class UserCreationListener implements EventSubscriberInterface
{
    protected $em;
    protected $user;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        );
    }

    public function onRegistrationSuccess(FormEvent $event)
    {
        $this->user = $event->getForm()->getData();
        $group_name = 'my_default_group_name';
        $entity = $this->em->getRepository('MoskitoUserBundle:Group')->findOneByName($group_name); // You could do that by Id, too
        $this->user->addGroup($entity);
        $this->em->flush();

    }
}

And basically, that's it !

After each registration success, onRegistrationSuccess() is called, so I get the user through the FormEvent $event and add it to my default group, which I get through Doctrine.

like image 79
MoskitoHero Avatar answered Oct 18 '22 07:10

MoskitoHero


You did not say how your users are created. When some admin creates the users or you have a custom registration action, you can set the group in the controller's action.

$user->addGroup($em->getRepository('...')->find($group_id));

However if you use fosuserbundles build in registration you have to hook into the controllers: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.md and use a event listener.

like image 45
UpCat Avatar answered Oct 18 '22 07:10

UpCat