-6

I want to get the Role of the user currently logged. Because I want to show that in my navbar can somebody help me?

My question is about how to get the current role

D. Schreier
  • 1,700
  • 1
  • 22
  • 34

2 Answers2

0

If I remember well, you can get roles in twig by doing {{ app.user && app.user.roles }} but you will get an array of roles.

Hope it helps.

Webvoid
  • 501
  • 2
  • 7
  • Thank you for youre response but this error has been thrown if i directly use {{app.user.roles}} in my twig this is the error: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") – Halit Ozdogru Jun 05 '18 at 09:36
  • yes, because it's an array, and should be treated like that. It you want to dump all roles, you could use a custom filter like join (https://twig.symfony.com/doc/2.x/filters/join.html) or dump (https://twig.symfony.com/doc/2.x/functions/dump.html) – Webvoid Jun 05 '18 at 09:38
  • also don't forget to test if the user exists before acceessing `app.user.roles` to avoid unwanted errors =) – Webvoid Jun 05 '18 at 09:39
  • But i dont want to dump all roles. I just want to show the role that is logged in. – Halit Ozdogru Jun 05 '18 at 09:40
  • in Symfony, an user has an array of roles, not just one. If you are SURE that your users will allways have one and ONE ONLY role, you could do either `{{ app.user.roles | first }}` or `{{ app.user.roles | last }}` – Webvoid Jun 05 '18 at 09:42
  • 1
    Maybe this can help you understand me more: if ($authorizationChecker->isGranted(new Expression('"ROLE_INKOPER" in roles'))) { return $this->redirectToRoute('homepageInkoper'); } is there not a option to show the granted role? – Halit Ozdogru Jun 05 '18 at 09:48
  • It's a good example even if it's in a controller, not what he wants, but the code is still usefull to know – Webvoid Jun 05 '18 at 09:49
  • but can you help me maybe to get that granted role? – Halit Ozdogru Jun 05 '18 at 09:54
  • as I said, if your user allways have only one role and you cascaded your roles in your security.yml, you should be able to get the role of your user by just doing in your twig `{{ app.user && app.user.roles[0] }}` – Webvoid Jun 05 '18 at 09:57
  • Thank you very much it worked for me but is there a possibilty to change ROLE_ADMIN in the user interface for example to admin? – Halit Ozdogru Jun 05 '18 at 10:03
  • yep, what you should do is for example `{{ ['ROLE_ADMIN' => 'admin'][app.user.roles[0] }}`, in fact, you create an associative array and exploit it just inline =) – Webvoid Jun 05 '18 at 10:10
  • Better: you can define your own twig filter and do for example `{{ app.user.roles[0] | roleDisplayer }}`, check https://symfony.com/doc/current/templating/twig_extension.html for more infos =) – Webvoid Jun 05 '18 at 10:14
  • i got an error: Unclosed "[". i dont know how to fix it is in youre array the last one – Halit Ozdogru Jun 05 '18 at 10:15
  • I forgot one `]`, it's `{{ ['ROLE_ADMIN' => 'admin'][app.user.roles[0]] }}` – Webvoid Jun 05 '18 at 10:16
0

you can get from current user,when we get the role,it returns array of roles

$user = $this->getUser();
$role = $user->getRoles();

in twig

{{app.user.roles}}
Robert
  • 3,373
  • 1
  • 18
  • 34