Question

I'm trying to create 3 users in my project:

  • Cliente : Who will access front-end and have fields 'name', 'CPF', 'adress'.
  • Vendedor : Who will register Offers in the site and have fields 'phone', 'CNPJ'
  • Admin : Who will administer all Clientes, Vendedores, Offers, etc...

So.. I installed 3 bundles for that: SonataUserBundle + FosUserBundle + SonataAdminBundle I followed the entire tutorial of each one. But I don't know how can I create each type of this users.

I am using ApplicationSonataUserBundle which generate entities User and Group.

Here is my code:

namespace Sete\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Application\Sonata\UserBundle\Entity\User as BaseUser;

/**
 * Cliente
 *
 * @ORM\Table(name="cliente")
 * @ORM\Entity
 * 
 */
class Cliente extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    ...another fields...
}

and Vendedor:

namespace Sete\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Application\Sonata\UserBundle\Entity\User as BaseUser;

/**
 * Vendedor
 *
 * @ORM\Table(name="vendedor")
 * @ORM\Entity
 */
class Vendedor extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    ...another fields...
}

AppKernel.php

        ...
        new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
        new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
        new Application\Sonata\UserBundle\ApplicationSonataUserBundle()

And config.yml

...
fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Application\Sonata\UserBundle\Entity\User
    group:
        group_class: Application\Sonata\UserBundle\Entity\Group

sonata_user:
manager_type: orm # can be orm or mongodb
...

This way my entities Cliente and Vendedor have no association with groups. I try to add $groups relationship, but not work. So, when I try to admin this entities I got error:

An exception occurred while executing 'SELECT count(DISTINCT c0_.id) AS sclr0 FROM cliente c0_ LEFT JOIN fos_user_user_group f3_ ON f2_.id = f3_.user_id LEFT JOIN fos_user_group f1_ ON f1_.id = f3_.group_id':

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'f2_.id' in 'on clause'

Is that the best pratice to create types of users? Or, Instead of extends ApplicationUserBundle:User, create Cliente and Vendedor entities (without extends ApplicationUserBundle:User), and then create relationship with User (puting fields $cliente and $vendedor inside User entity and making a relationship) ?

Sorry about the english. I trying to do this all week. Follow many tutorials but not got the answer.

Thx all.

Was it helpful?

Solution

One way to go about having multiple user types is to use Doctrine inheritance mapping.

However, I would recommend using single user type together with role security handler. You should install SonataUserBundle with Easy Extends (installation instructions), so that you can leave SonataUserBundle unmodified. You should add all needed fields to the App\UserBundle\Entity\User and create three user Groups: Clientes, Vendedors and Admins. In your main config.yml, add two missing roles (there already is ROLE_ADMIN):

ROLE_SUPER_ADMIN:       [<...>, ROLE_CLIENT, ROLE_VENDOR]

Assign those three user Groups appropriate permissions that you created.

Now, in User, make custom fieds nullable and in you UserAdmin show only the fieds user type can see using security rigths. Example:

if ($this->isGranted('ROLE_CLIENT')) {
    $formMapper->add('cpf');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top