2013-03-11 9 views
8

पर एक डिफ़ॉल्ट समूह में जोड़ना मैं अपनी पहली गंभीर सिम्फनी 2 प्रोजेक्ट का निर्माण कर रहा हूं। मैं अपने उपयोगकर्ता/समूह प्रबंधन के लिए FOSUserBundle का विस्तार कर रहा हूं, और मैं चाहता हूं कि नए उपयोगकर्ता स्वचालित रूप से डिफ़ॉल्ट समूह में जोड़े जाए। मुझे लगता है कि आप सिर्फ इस तरह उपयोगकर्ता इकाई निर्माता का विस्तार करने के लिए है:नए 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 इकाई मिलता है है?

मैंने इकाई के भीतर से इकाई प्रबंधक का उपयोग करने की कोशिश की, लेकिन तब मुझे एहसास हुआ कि यह बेवकूफ था, और सिम्फनी एक त्रुटि फेंक रहा था। मैं इसके लिए googled, लेकिन शायद setting up a service for that को छोड़कर कोई असली समाधान नहीं मिला ... हालांकि यह मेरे लिए काफी अस्पष्ट लगता है।

उत्तर

10

ठीक है, मैंने artworkad के विचार को लागू करने पर काम करना शुरू कर दिया।

पहली बात मैंने किया था FOSUserBundle को 2.0.*@dev को composer.json में अपडेट कर रहा था, क्योंकि मैं 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> 

एक्सएमएल में, मैं सेवा बताया कि मैं एक तर्क 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 के माध्यम से मिलता है और इसे मेरे डिफ़ॉल्ट समूह में जोड़ता है, जिसे मैं सिद्धांत के माध्यम से प्राप्त करता हूं।

3

आपने यह नहीं कहा कि आपके उपयोगकर्ता कैसे बनाए जाते हैं। जब कुछ व्यवस्थापक उपयोगकर्ता बनाता है या आपके पास कस्टम पंजीकरण कार्रवाई है, तो आप समूह को नियंत्रक की कार्रवाई में सेट कर सकते हैं। https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.md और एक घटना श्रोता का उपयोग करें:

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

लेकिन यदि आप fosuserbundles पंजीकरण में निर्माण आप नियंत्रकों में हुक करने के लिए है का उपयोग करें।

+0

आपके उत्तर का दूसरा भाग ऐसा लगता है कि मैं क्या करना चाहता हूं। मैं इसे लागू करने की कोशिश करूंगा और यहां एक समाधान के रूप में उत्तर पोस्ट करूंगा। – Weengs

संबंधित मुद्दे