Skip to content

Commit 8d32dcd

Browse files
committed
merged branch aerialls/getuser (PR #1990)
Commits ------- f7bf7b5 fixed condition 181332b added a Controller:getUser() shortcut to recover the current user Discussion ---------- [2.1] added a Controller:getUser() shortcut to recover the current user
2 parents 8841679 + f9f7c87 commit 8d32dcd

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 ? null : $token->getUser();
184+
}
185+
168186
/**
169187
* Returns true if the service id is defined.
170188
*

0 commit comments

Comments
 (0)