简体   繁体   中英

Symfony2 - Access Denied

I am using Symfony2 for my project and I created two pages. One login page and one index page. I have logged in admin account (with ROLE_ADMIN ) successfully.
However I received 403 Forbidden page with the following error:

ERROR - Uncaught PHP Exception
Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException: "Access Denied" at .\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Security\\Http\\Firewall\\ExceptionListener.php line 100

And this is my config in security.yml :

access_control: 
   - { path: ^/vs/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
   - { path: ^/vs/index, roles: ROLE_ADMIN }

when I var_dump user. I see that roles is empty:

private 'roles' => 
    object(Doctrine\ORM\PersistentCollection)[293]
      private 'snapshot' => 
        array (size=0)
          empty

And this is the Roles from my User.php :

/**
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
 * @ORM\JoinTable(name="user_role",
 * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 * inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 */
private $roles;

And this is what I get when using $user->getRoles() :

array (size=1)
  0 => 
    object(...\Entity\Role)[397]
      private 'id' => int 1
      private 'name' => string 'admin' (length=5)
      private 'role' => string 'ROLE_ADMIN' (length=10)
      private 'users' => 
        object(Doctrine\ORM\PersistentCollection)[398]
          private 'snapshot' => 
            array (size=0)

What did I do wrong?

The doctrine relation annotations were wrong:

/** 
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users") 
 */ 
private $roles; 

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="roles") 
 */ 
private $users; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM