Skip to content

Commit cacd7c6

Browse files
author
Julien Brochet
committed
added a Controller:getUser() shortcut to recover the current user
1 parent 70932c6 commit cacd7c6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Controller/Controller.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ public function getDoctrine()
165165
return $this->container->get('doctrine');
166166
}
167167

168+
/**
169+
* Get a user from the Security Context
170+
*
171+
* @return mixed
172+
*
173+
* @throws \LogicException If SecurityBundle is not available
174+
*/
175+
public function getUser()
176+
{
177+
if (!$this->container->has('security.context')) {
178+
throw new \LogicException('The SecurityBundle is not installed in your application.');
179+
}
180+
181+
$token = $this->container->get('security.context')->getToken();
182+
183+
return null === $token ?: $token->getUser();
184+
}
185+
168186
/**
169187
* Returns true if the service id is defined.
170188
*

0 commit comments

Comments
 (0)