Question

            {% if is_granted('ROLE_PREVIOUS_ADMIN') %}
            <a href="{{ path('redirect_to_admin', {'_switch_user': '_exit'}) }}">Stop viewing as: {{app.user.username}}</a>
            {% endif %}

Problem is user impersonating never has the role. I'm using the FOSUserBundle as well. This code was working at one point, Idont know what changes are causing it to fail now though

security.yml

security:
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        main:
            switch_user: true
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                success_handler: login_success_handler
            logout:
                path: fos_user_security_logout
                invalidate_session: false
                #remove invalidate once we upgrade to 5.4.11
            anonymous:    true

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

User.php (entity) I have removed company bundle name from the paths

<?php

namespace Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;

    /**
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * groups={"Registration", "Profile"}
     * @ORM\ManyToOne(targetEntity="Community", inversedBy="users")
     * @ORM\JoinColumn(name="community_id", referencedColumnName="id")
     */
    protected $community;

    public function __construct()
    {
        parent::__construct();
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));
        //everyone is a user
        $this->roles = array('ROLE_USER');
    }

    /**
     * @inheritDoc
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * @inheritDoc
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    public function isEnabled()
    {
        return $this->isActive;
    }
    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return User
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Set community
     *
     * @param \Entity\Community $community
     * @return User
     */
    public function setCommunity(\Entity\Community $community = null)
    {
        $this->community = $community;

        return $this;
    }

    /**
     * Get community
     *
     * @return \Entity\Community 
     */
    public function getCommunity()
    {
        return $this->community;
    }
}
Was it helpful?

Solution 2

I was overriding several functions that were already defined by FOS's BaseUser Class. Removing them from my user.php (entity) resolved this issue.

OTHER TIPS

role_hierarchy:
    ROLE_ADMIN:       [ROLE_USER  ROLE_ALLOWED_TO_SWITCH]
    ROLE_SUPER_ADMIN: [ROLE_ADMIN ROLE_ALLOWED_TO_SWITCH]

You must have dropped the allowed to switch role as some point.

Also strange that you have no explicit ROLE_USER.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top