0

i try :

in console :

php app/console fos:user:promote grek admin
Role "admin" has been added to user "grek".

if ($this->get('security.context')->isGranted('ROLE_ADMIN')) die('ok');

this is not show why ?

i log off, log in and same situtation

var_dump($this->get('security.context')->getToken()->getUser());

i>protected</i> 'roles' <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=4)</i>
      0 ADMIN'</font> <i>(length=5)</i>
      1 OWNER'</font> <i>(length=5)</i>
      2 TESTER'</font> <i>(length=6)</i>


 $user = $this->get('security.context')->getToken()->getUser();
  var_dump($user->getRoles());

array (size=1)
  0 => string 'ROLE_USER' (length=9)

why $user->getRoles() give only one ?

when i add php app/console fos:user:promote grek ADMIN php app/console fos:user:promote grek ROLE_ADMIN php app/console fos:user:promote grek admin

then check - i dont have role in if ($this->get('security.context')->isGranted

And somethink else :

php app/console fos:user:promote grek test
Role "test" has been added to user "grek".
php app/console fos:user:demote grek test
User "grek" didn't have "test" role.

What is this ?

Developer
  • 2,731
  • 2
  • 41
  • 71

1 Answers1

1

You have granted the user the role ADMIN, when it should be ROLE_ADMIN

UPDATE

You can solve your problem either running

php app/console fos:user:promote grek role_admin

Or changing

if ($this->get('security.context')->isGranted('ROLE_ADMIN')) die('ok');

to

if ($this->get('security.context')->isGranted('ADMIN')) die('ok');

  • It helps to be even a bit more explicit with answers - i.e. reference the particular line of code that your answer applies to and provide an example of the corrected syntax. – larsAnders Apr 11 '14 at 16:49