PUGX / PUGXMultiUserBundle

An extension for FOSUserBundle to handle users of different types. Compatible with Doctrine ORM.
163 stars 96 forks source link

User discriminator based on class instead of name? #112

Open clytemnestra opened 8 years ago

clytemnestra commented 8 years ago

I want to provide different login/register pages/forms for my users, but the users are the same class. I don't want to populate the database with additional tables.

pugx_multi_user:
  users:
    normal:
        entity: 
          class: AppBundle\Entity\SfGuardUser
        registration:
          form:
            type: Application\FOS\UserBundle\Form\RegistrationType
          template: AcmeUserBundle:Registration:1.form.html.twig
    admins:
        entity: 
          class: AppBundle\Entity\SfGuardUser
        registration:
          form:
            type: Application\FOS\UserBundle\Form\RegistrationType
          template: ApplicationFOSUserBundle:Registration:2.html.twig
class RegistrationController extends Controller
{
    /**
     * @Route("/asd1", name="login1")
     */
    public function registerUserAction()
    {
        return $this->container
            ->get('pugx_multi_user.registration_manager')
            ->register('AppBundle\Entity\SfGuardUser');
    }

    /**
     * @Route("/asd2", name="login2")
     */
    public function registerAdminAction()
    {
        return $this->container
            ->get('pugx_multi_user.registration_manager')
            ->register('AppBundle\Entity\SfGuardUser');
    }
}

This configuration doesn't work unfortunately, mainly because UserDiscriminator takes values by class instead of user type names. For instance, to get the template, the discriminator does this

    /**
     * 
     * @return string
     */
    public function getTemplate($name)
    {
        return $this->conf[$this->getClass()][$name]['template'];
    }

Which will return it by AppBundle\Entity\SfGuardUser instead of normal or admins

Is there a way to have a single class for multiple types of users?