How to arrange in Symfony3 loading roles from the DB?

I'm almost there.
There is a User table, Role table, the user_role table for communication many-to-many users and roles.
Model users provides a method getRoles() and
implements UserInterface
namespace AppBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;

class User implements UserInterface
{

 // ...

/**
 * @var \Doctrine\Common\Collections\Collection
*/
 private $userRoles;

/**
 * Constructor
*/
 public function __construct()
{
 $this->userRoles = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Get userRoles
*
 * @return \Doctrine\Common\Collections\Collection
*/
 public function getUserRoles()
{
 return $this->userRoles;
}

/**
 * Getter for the array of roles.
*
 * @return array An array of Role objects
*/
 public function getRoles()
{
 return $this->getUserRoles()->map(function ($item) {
 return $item->getName();
})->toArray();
}

/**
 * @param $roleName
*
 * @return bool
*/
 public function hasRole($roleName)
{
 return in_array($roleName, $this->getRoles());
}

}




I learned that to download role UsernamePasswordToken you can use the re-authenticate the user
The overridden of the token of the user:
$tokenStorage = $this->get('security.token_storage');
 /** @var $user User */
 $user = $tokenStorage->getToken()->getUser();

 $token = new UsernamePasswordToken(
$user,
null,
'main',
$user->getRoles()
);
 $this->get('security.token_storage')->setToken($token);

If you don't override the token $token->getRoles() will give only the role ROLE_ADMIN (I don't know where it comes from, and presumably it's the Symphony he assigns it by default in DEV mode). But when it is overridden, $token->getRoles() returns all the roles from the database available to the user. That gives us an opportunity to voter-Ah to do so:

protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
 // ...

 if ($this->decisionManager->decide($token, array('ROLE_DB_SUPER_ADMIN'))) {
 return true;
}

 // ...
 }


So, the question is, where is the best Simfony3 to do an override of the token (see code above)? It is normal practice? As I understand it, to do this override is better hung some kernel-listener. But before doing so, wanted to know how to do it competently.
June 27th 19 at 15:29
2 answers
June 27th 19 at 15:31
Solution
In section firewalls.main not specified `provider: main`. It is necessary to specify explicitly
June 27th 19 at 15:33
The problem was solved when I ran for Council @BoShurik
But I noticed such a thing (MB someone come in handy) - if in the database to change the name of the role add the new role to reassign the user, etc $this->get('security.token_storage')->getToken()->getRoles() will still be given the old list of roles of the user.
If you clear the cache via the console, or even delete all the cache folder, it won't help. Helps to re-authenticate (log out, log in). The fact that, at the end of each request, symfony serializes (serialize) the user object in $_SESSOIN, and at the beginning of a new query desiatiletie (unserialize) the object from the session. As I understand it, with the roles of the user it also comes.
In this case there is the option
`security.always_authenticate_before_granting`
Will put to true and it will work. (in the Profiler will show the wrong role, but when referring to the method isGranted - roles will be correct) - Candace_Bogisich commented on June 27th 19 at 15:36

Find more questions by tags PHPSymfony