Symfony2 - Access Denied

I use Symfony2 for my project and I created two pages. One login page and one index page. I have successfully registered an administrator account (using ROLE_ADMIN ).
However, I received a 403 Forbidden page with the following error:

ERROR - Fixed PHP exception

Symfony \ Component \ HttpKernel \ Exception \ AccessDeniedHttpException: "Access Denied" in. \ Vendor \ symfony \ symfony \ src \ Symfony \ Component \ Security \ Http \ Firewall \ ExceptionListener.php line 100

And this is my configuration in security.yml :

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

when user var_dump. I see the roles are empty:

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

And these are 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 have I done wrong?

+4
source share
1 answer

The doctrine relationship annotations were incorrect:

 /** * @ORM\ManyToMany(targetEntity="Role", inversedBy="users") */ private $roles; /** * @ORM\ManyToMany(targetEntity="User", mappedBy="roles") */ private $users; 
+1
source

Source: https://habr.com/ru/post/1501646/


All Articles