Skip to content

Commit

Permalink
Documented AuthTokenUpdaterListener class
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeveloper committed May 21, 2015
1 parent 5ff5e9b commit c8f7e22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions EventListener/AuthTokenUpdaterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
use Ant\ChateaClient\Service\Client\ChateaGratisAppClient;
use Ant\Bundle\ChateaSecureBundle\Security\User\User;

/**
* Request listener that updates the HTTP client access token of OAuth2 if an action requirs to be made by the loggedin user
* (action or class having @APIUser annotation)
* Class AuthTokenUpdaterListener
* @package Ant\Bundle\ChateaClientBundle\EventListener
*/
class AuthTokenUpdaterListener
{
private $annotationReader;
private $securityContext;
private $client;
private $annotationClass = 'Ant\Bundle\ChateaClientBundle\Security\Authentication\Annotation\APIUser';

/**
* Constructor
* @param Reader $annotationReader
* @param SecurityContextInterface $securityContext
* @param ChateaGratisAppClient $client
*/
function __construct(Reader $annotationReader, SecurityContextInterface $securityContext, ChateaGratisAppClient $client)
{
$this->annotationReader = $annotationReader;
Expand All @@ -26,12 +38,18 @@ public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();

/**
* if the action has the APIUser annotation, check first if the user is valid and if so update the client acces token
*/
if($this->hasApiUserAnnotation($controller)){
$this->assertUserIsLoggedIn();
$this->updateClientAccessToken();
}
}

/**
* Asserts that a user is logged in
*/
private function assertUserIsLoggedIn()
{
$token = $this->securityContext->getToken();
Expand All @@ -45,6 +63,11 @@ private function assertUserIsLoggedIn()
}
}

/**
* Checks if the action or the hole controller has an @APIUser annotation
* @param $controller
* @return bool
*/
private function hasApiUserAnnotation($controller)
{
$object = new \ReflectionObject($controller[0]);
Expand All @@ -54,6 +77,9 @@ private function hasApiUserAnnotation($controller)
$this->annotationReader->getClassAnnotation($object, $this->annotationClass) != null;
}

/**
* Updates the access token of the HTTP client with the access token of the current user
*/
private function updateClientAccessToken()
{
$user = $this->securityContext->getToken()->getUser();
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ class AreaRestringidaController
}
}
```

Dicha annotación es gestionada por el listener ```Ant\Bundle\ChateaClientBundle\EventListener\AuthTokenUpdaterListener``` que escucha al evento del controlador para averiguar si debe actualizar el access token del usuario en caso necesario.

0 comments on commit c8f7e22

Please sign in to comment.