I
I
Ilya2015-10-01 16:47:15
PHP
Ilya, 2015-10-01 16:47:15

How in Symfony2 to automatically update user roles within sessions?

I have a small project that implements a custom system, due to:

<?php
class User extends \FOS\UserBundle\Model\User {
    /**
     * @ORM\OneToOne(targetEntity="Group")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
     * @var $group \Cyber\CraftBundle\Entity\Group
     */
    protected $group;

    //... другие поля
}

/**
 * @ORM\Entity(repositoryClass="GroupRepository")
 * @ORM\Table(name="cyber_groups")
 */
class Group implements \FOS\UserBundle\Model\GroupInterface
{
    /**
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="cyber_groups_user_roles")
     */
    protected $roles;

    /**
     * @ORM\OneToOne(targetEntity="Group")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     */
    protected $parent;
}

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;

/**
 * Role Entity
 *
 * @ORM\Entity(repositoryClass="RoleRepository")
 * @ORM\Table(name="cyber_roles")
 */
class Role implements RoleInterface {
    public static $ROLE_DEFAULT = "VIEW_SITE";
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\Column(type="string", name="role", unique=true, length=70)
     */
    private $role;
}

However, after authorization, as I understand it, all roles are recorded in the session, which contradicts the architecture of my application, since the admin panel has a function to change the user group - thus, $user->getRoles() will return completely different role values. How can I force the renewal of these rights with each visit to the site, and not store them in sessions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2015-10-01
@XuPoH

Solved the problem by adding

security:
    always_authenticate_before_granting: true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question