模拟用户时我没有'ROLE PREVIOUS ADMIN'角色

时间:2014-02-13 16:34:54

标签: php symfony fosuserbundle

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

问题是用户冒充永远不会有角色。我也在使用FOSUserBundle。这段代码在某一点上工作,虽然

,但是我知道现在哪些变化导致它失败了

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)我已从路径

中删除了公司包名称
<?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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

你必须放弃允许切换角色作为某种意义。

你也没有明确的ROLE_USER也很奇怪。

答案 1 :(得分:0)

我覆盖了已经由FOS的BaseUser Class定义的几个函数。从我的user.php(实体)中删除它们解决了这个问题。