问题描述
我正在构建我的第一个严肃的Symfony2项目.我正在为我的用户/组管理扩展FosuserBundle,并希望新用户自动添加到默认组中. 我想您只需要像这样扩展用户实体构造函数:
/** * 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实体?
我尝试使用实体内部的实体管理器,但后来我意识到这很愚蠢,Symfony丢了一个错误.我为此进行了搜索,但是没有找到真正的解决方案,除了为此建立服务 ...虽然这对我来说似乎还不清楚.
推荐答案
好吧,我开始努力实施 artworkad 的想法.
我所做的第一件事是将fosuserbundle更新为composer.json中的2.0.*@dev,因为我正在使用v1.3.1,该v1.3.1并未实现fosuserevents类.这是订阅我的注册活动所必需的.
// composer.json "friendsofsymfony/user-bundle": "2.0.*@dev",
然后我添加了一个新服务:
<!-- 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访问学说.然后,我创建了听众:
// 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获得了用户,并将其添加到我的默认组中,我可以通过学说获得.
其他推荐答案
您没有说出如何创建用户.当某些管理员创建用户或您有自定义注册操作时,您可以在控制器的操作中设置组.
$user->addGroup($em->getRepository('...')->find($group_id));
但是,如果您在注册中使用fosuserbundles构建,则必须挂在控制器中: https://github.com/friendsofsymfony/fosuserbundle/blob/master/master/resources/doc/controller_events.md 并使用事件听众.
问题描述
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.
推荐答案
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.
其他推荐答案
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.