User Type SonataUserBundle + FOSUserBundle + SonataAdminBundle

I am trying to create 3 users in my project:

  • Client : who will access the interface and have the field name ',' CPF ',' address'.
  • Vendor : who will register offers on a site and have fields "phone", "CNPJ"
  • Admin : who will manage all the client, tenders, offers, etc.

So, I installed 3 packages for this: SonataUserBundle + FosUserBundle + SonataAdminBundle I followed the entire tutorial for each of them. But I do not know how I can create each type of these users.

I use ApplicationSonataUserBundle which generate User and Group entities.

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 Vendor:

 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 ... 

Thus, my Cliente and Vendor objects have no connection with groups. I am trying to add a $ groups relationship but not working. Therefore, when I try to administer these entities, I received an 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

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

Is this the best rating for creating user types? Or instead of the ApplicationUserBundle: extension, the user creates Cliente and Vendor objects (without the ApplicationUserBundle: User extensions), and then create relationships with the user (by placing the $ cliente and $ vendor fields inside the user object and creating the relationship)?

Sorry for the English. I have been trying to do this all week. Follow many tutorials but don't get an answer.

thanks all.

+4
source share
1 answer

One way to make several types of users is to use Doctrine inheritance .

However, I would recommend using one type of user with a role security handler. You must install the SonataUserBundle with Easy Extends ( installation instructions ) so that you can leave the SonataUserBundle unmodified. You must add all the required fields to App \ UserBundle \ Entity \ User and create three user groups: clients, vendors and admins. In your main config.yml add two missing roles (already have ROLE_ADMIN):

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

Assign these three user groups to the appropriate permissions that you created.

Now, in User, set custom fieds to zero, and in your UserAdmin, show that only the user type fieds can see using security levels. Example:

 if ($this->isGranted('ROLE_CLIENT')) { $formMapper->add('cpf'); } 
+1
source

Source: https://habr.com/ru/post/1490065/


All Articles