Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:40:29+00:00 2026-06-09T13:40:29+00:00

How can I get a security token for any user, not only the one

  • 0

How can I get a security token for any user, not only the one currently logged in ?

I would like to be able to call isGranted() on a user fetched from the database

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:40:31+00:00Added an answer on June 9, 2026 at 1:40 pm

    isGranted() comes from the Security service, so it would be hard/unnecessary to use that to get Roles without adjusting the state of the session.

    Don’t get me wrong, it’s definitely possible… This would work, for example:

    public function strangeAction()
    {
        // Get your User, however you normally get it
        $user = $userRepository->find($id);
        // Save the current token so you can put it back later
        $previousToken = $this->get("security.context")->getToken();
        // Create a new token
        $token = new UsernamePasswordToken($user, null, "main", $user->getRoles());
        // Update the security context with the new token
        $this->get("security.context")->setToken($token);
        // Now you have access to isGranted()
        if ($this->get("security.context")->isGranted("ROLE_SOMETHING"))
        { /* Do something here */ }
        // Don't forget to reset the token!
        $this->get("security.context")->setToken($previousToken);
    }
    

    …but that really makes no sense.

    In reality, you don’t need the token. A much better way of doing this would be to add an isGranted() method into your User entity:

    // Namespace\YourBundle\Entity\User.php
    
    class User
    {
        ...
        public function isGranted($role)
        {
        return in_array($role, $this->getRoles());
        }
        ...
    }
    

    Now you can get those roles in your controllers:

    public function notSoStrangeAction()
    {
        // Get your User, however you normally get it
        $user = $userRepository->find($id);
        // Find out if that User has a Role associated to it
        if ($user->isGranted("ROLE_SOMETHING"))
        { /* Do something here */ }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring Security 3.0.7 How can I get with java code the access
The error: ID4175: The issuer of the security token was not recognized by the
I've been pulling my hair out over this one and I just can't get
Can get all triples with value null in specific field? All people with date_of_birth
We can get class Class object by 3 methods: MyClass.class obj.getClass Class.forName(className) I don't
I can get the file path with: (message (file-name-directory (or buffer-file-name load-file-name))) But if
I can get this to work: [<DllImport(user32.dll)>] extern bool GetClientRect(nativeint, RECT*) let getClientRect hwnd
You can get the nodes of the document object of an <iframe> with the
You can get underground processes by ps ux I am searching a way to
You can get a list of databases using PRAGMA database_list or a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.