Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current client on controller using FOSOAuthServerBundle

Is there an easy way to get the current client on a controller using FOSOAuthServerBundle on Symfony?

I have some properties on my client entity and want to read them in a controller but i can't find the way to get the current client.

I've only found the way to get the current user ($this->container->get('security.context')->getToken()->getUser();), but not the current client.

EDIT: i've now found the way to get the client, but it's not showing me the values of the properties i added to my client entity. The following code:

$token = $this->container->get('security.context')->getToken()->getToken();
$accessToken = $this->container->get('fos_oauth_server.access_token_manager.default')->findTokenBy(array('token' => $token));
$client = $accessToken->getClient();
\Doctrine\Common\Util\Debug::dump($client);

is dumping:

object(stdClass)[1180]
public '__CLASS__' => string 'CC\APIBundle\Entity\Client' (length=26)
public '__IS_PROXY__' => boolean true
public '__PROXY_INITIALIZED__' => boolean false
public 'id' => int 6
public 'name' => null
public 'city' => null
public 'randomId' => null
public 'secret' => null
public 'redirectUris' => 
  array (size=0)
    empty
public 'allowedGrantTypes' => null

So the name and city that my client has on db is not showing... also i think there are many calls to db that could be avoided in a much elegant way...

Any ideas?

like image 938
Noquepoaqui Avatar asked Feb 10 '26 18:02

Noquepoaqui


1 Answers

$tokenManager = $this->get('fos_oauth_server.access_token_manager.default');
$token        = $this->get('security.token_storage')->getToken();
$accessToken  = $tokenManager->findTokenByToken($token->getToken());

$client = $accessToken->getClient();
like image 86
m4s0 Avatar answered Feb 12 '26 16:02

m4s0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!