创建时将新的FOSUserBundle用户添加到默认组 [英] Adding new FOSUserBundle users to a default group on creation
问题描述
我正在构建我的第一个严肃的Symfony2项目.我正在为用户/组管理扩展FOSUserBundle,并且希望将新用户自动添加到默认组. 我想您只需要像这样扩展User实体构造函数即可:
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);
}
但是我的问题是如何首先获得我的$ defaultGroup实体?
But my question is how do I get my $defaultGroup entity in the first place?
我尝试从实体内部使用实体管理器,但是后来我意识到这很愚蠢,并且Symfony抛出错误.我对此进行了谷歌搜索,但没有找到真正的解决方案,除了
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.
推荐答案
好,我开始着手实现 artworkad '的主意.
OK, I started working on implementing artworkad's idea.
我做的第一件事是将composer.json中的FOSUserBundle更新为2.0.*@dev,因为我使用的是v1.3.1,它没有实现FOSUserEvents类.订阅我的注册活动是必需的.
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>
在XML中,我告诉服务我需要通过参数doctrine.orm.entity_manager
访问Doctrine.然后,我创建了监听器:
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();
}
}
基本上就是这样!
每次注册成功后,都会调用onRegistrationSuccess()
,因此我通过FormEvent $event
吸引了用户,并将其添加到我的默认组中,该组是我通过教义获得的.
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.
这篇关于创建时将新的FOSUserBundle用户添加到默认组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!