Symfony 3-isGranted不适用于角色

时间:2019-05-06 14:29:07

标签: php symfony access roles

我有一个UserCas实体,该实体具有数据库角色数组(用于FOSU​​serBundle的原理相同)。

通过查看security.yml,我设法根据角色来限制对页面的访问,但是当我使用is_granted时,它没有考虑到我的角色,所以我不明白为什么... < / p>

我有:

access_control:
    - { path: ^/accueil, role: [IS_AUTHENTICATED_ANONYMOUSLY] }
   # - { path: ^/admin, role: ROLE_ADMIN }
    - { path: ^/, role: [IS_AUTHENTICATED_FULLY] }
    - { path: ^/admin/*, roles: [ROLE_ADMIN] }

enter image description here

我的UserCas实体:

<?php

namespace Site\PagesBundle\Entity;

use Serializable;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Site\PagesBundle\Security\Traits\traitUser;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Site\PagesBundle\Entity\PaquetDDLCas;

/**
 * UserCas
 *
 * @ORM\Table(name="user_cas")
 * @ORM\Entity(repositoryClass="Site\PagesBundle\Repository\UserCasRepository")
 * @UniqueEntity("mail")
 */
class UserCas implements \Serializable, UserInterface
{

    use traitUser;


    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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



    /************ MODIF ****************/

    /**
     * @ORM\Column(type="array")
     */
    protected $roles = [];

     /**
     * {@inheritdoc}
     */
    public function addRole($role)
    {
        $role = strtoupper($role);
        if ($role === ['ROLE_USER']) {
            return $this;
        }

        if (!in_array($role, $this->roles, true)) {
            $this->roles[] = $role;
        }

        return $this;
    }

    /**
     * {@inheritdoc}
     * @return array
     */
    public function getRoles()
    {
        return array_unique(array_merge(['ROLE_USER'], $this->roles));
    }

    /**
     * {@inheritdoc}
     */
    public function hasRole($role)
    {
        return in_array(strtoupper($role), $this->getRoles(), true);
    }

    /**
     * {@inheritdoc}
     */
    public function removeRole($role)
    {
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
            unset($this->roles[$key]);
            $this->roles = array_values($this->roles);
        }

        return $this;
    }

        /**
     * {@inheritdoc}
     */
    public function setRoles(array $roles)
    {
        $this->roles = array();

        foreach ($roles as $role) {
            $this->addRole($role);
        }

        return $this;
    }

    public function resetRoles()
    {
        $this->roles = [];
    }

    /******************* FIN MODIF  *********************/



    /**
     * Constructor
     */
    public function __construct()
    {
        $this->setEnabled(true);
        $this->roles = array();

    }



    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }



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

    public function setUsername($username)
    {
        $this->username = $username;
    }



    public function serialize()
    {
        return serialize([
            $this->id,
            $this->username,
            $this->mail
        ]);
    }

    public function unserialize($serialized)
    {
        list(
            $this->id,
            $this->username,
            $this->mail
        ) = unserialize($serialized);
    }



    /*********************** Méthodes pour UserInterface ***************************/

    public function eraseCredentials()
    {
    }

    /**
     * {@inheritdoc}
     */
    public function getSalt()
    {

    }

    /**
     * {@inheritdoc}
     */
    public function getPassword()
    {

    }


}

如果我的树枝文件,我想显示一些内容,即使我有ROLE_ADMIN:

{% if is_granted('ROLE_ADMIN') %}

//...

{% endif %}

但这不起作用。

在我的控制器中,如果我做一个条件:

if (is granted('ROLE_ADMIN))
{
   dump('yes');
}

else
{
  dump('no');
}

我有转储('否')。

但是使用security.yml中的访问控制,限制访问有效

有人可以帮我吗?

0 个答案:

没有答案