M
M
Maxim2015-09-23 15:01:31
symfony
Maxim, 2015-09-23 15:01:31

How to correctly implement symfony 2 authorization?

I started to study this framework and I can’t understand how to solve the problem of authorizing users stored in the database correctly.
After reading the documentation, unfortunately in English, with which I am not very friendly, it became clear that this should be done using FOSUserBundle.
Somehow, having installed according to the instructions, I got the opportunity to create users, groups.
But as always, there were questions that I could not find answers to in the documentation.
1) There is some entity users which inherits FOS\UserBundle\Model\User.
contains additional entries.

<?php
namespace ACVote\UsersBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="Users")
 */
class Users extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @ORM\Column(name="surname", type="string", length=255, nullable=true)
     */
    private $surname;

    /**
     * @ORM\ManyToMany(targetEntity="ACVote\UsersBundle\Entity\Groups")
     * @ORM\JoinTable(name="fos_user_user_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;


    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

The firstname lastname fields appeared, but the group field did not. why? The entity for the group has been created. there is a table in the database.
2) When registering a user, by default, he is not assigned any role, group, and you cannot specify the first and last name, how to fix this. As I understand it, you need to inherit from something and write this functionality there. it is not clear from what to inherit and what to predetermine.
3) When creating a group, I don’t know how and where to specify the roles of this group.
4) And probably the most incomprehensible thing is how to replace the standard templates with your pre-made ones.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-09-23
@maxpointn2point

what should be done with FOSUserBundle.

No, FosUserBundle provides basic stuff like account activation etc. This is convenient, but there are a lot of options in which this bundle does more bad than good. In most cases, use its norms.
Because this field is defined in the User base class from the Fos User Bundle. This is a good example of why I don't like using it, I prefer full control over my entities.
This is usually solved by expanding the forms of the type that goes into the user bundle. Well, either your own handler, or your own registration implementation. In general, most of the problems are solved by setting values ​​in the model constructor, but there are options in which this is no longer so convenient.
We read the documentation for the fos user bundle, it's all there. This is one of the most basic needs of a developer, so, as you understand, this can be done by setting the paths to the templates that you want to use in the config.

S
shagguboy, 2015-09-23
@shagguboy

symfony.com/doc/current/cookbook/bundles/inheritan...
symfony.com/doc/current/cookbook/bundles/override.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question