How to assign roles on successful registration?

被刻印的时光 ゝ 提交于 2019-12-05 21:21:35

问题


I'm using fos user bundle and pugx multi user bundle. I've read all the documentation and I'm new to Symfony. In the pugx multi user bundle there's a sample on every point but one: sucessful registration.

  • Samples of overriding controllers for generating forms => ok
  • Samples of overriding templates for generating forms => ok
  • Samples of overriding successful registration sample => nothing.

Here's my code:

class RegistrationController extends BaseController
{
    public function registerAction(Request $request)
    {   
        $response = parent::registerAction($request);
        return $response;
    }   

    public function registerTeacherAction()
    {   
        return $this->container
            ->get('pugx_multi_user.registration_manager')
            ->register('MyBundle\Entity\PersonTeacher');
    }   

    public function registerStudentAction()
    {   
        return $this->container
            ->get('pugx_multi_user.registration_manager')
            ->register('MyBundle\Entity\PersonStudent');
    }   
}

The problem is with ->get('pugx_multi_user.registration_manager') which returns a manager. In the fos user overring controllers help, they get either a form or a form.handler. I'm having hard times to "link" those with the pugx_multi_user manager.

What code should I put in the registerTeacherAction() to set roles for teacher, and in registerStudentAction() to set roles for student on a successful registration?


回答1:


Solution 1 (Doctrine Listener/Subscriber)


You can easily add a doctrine prePersist listener/subscriber that adds the roles/groups to your entities depending on their type before persisting.

The listener

namespace Acme\YourBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Acme\YourBundle\Entity\Student;

class RoleListener
{
    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entityManager = $args->getEntityManager();

        // check for students, teachers, whatever ...
        if ($entity instanceof Student) {
            $entity->addRole('ROLE_WHATEVER');
            // or
            $entity->addGroup('students');
            // ...
        }

       // ... 
    }
}

The service configuration

# app/config/config.yml or load inside a bundle extension
services:
    your.role_listener:
        class: Acme\YourBundle\EventListener\RoleListener
        tags:
            - { name: doctrine.event_listener, event: prePersist }

Solution 2 (Doctrine LifeCycle Callbacks):


Using lifecycle callbacks you can integrate the role-/group-operations directly into your entity.

/**
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */
class Student
{
    /**
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        $this->addRole('ROLE_WHATEVER');
        $this->addGroup('students');
    }

Solution 3 (Event Dispatcher):


Register an event listener/subscriber for the "fos_user.registration.success" event.

How to create an event listener / The EventDispatcher component.



来源:https://stackoverflow.com/questions/19321146/how-to-assign-roles-on-successful-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!