Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Created August 25, 2015 19:50
Show Gist options
  • Save fabiopaiva/346ad4038d4efea83bf1 to your computer and use it in GitHub Desktop.
Save fabiopaiva/346ad4038d4efea83bf1 to your computer and use it in GitHub Desktop.
Apigility Add Role to access_tokens table
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager -> attach(
MvcEvent::EVENT_FINISH,
array($this, 'onFinish')
);
}
public function onFinish(MvcEvent $e)
{
$routeMatch = $e -> getRouteMatch();
$routeParams = $routeMatch -> getParams();
if ($routeParams['controller'] == 'ZF\OAuth2\Controller\Auth'
&& $routeParams['action'] == 'token') {
if ($e->getResult()->getStatusCode() == 200) {
$data = json_decode($e->getResult()->getContent(), true);
if (isset($data['access_token']) && $data['access_token'] != '' && !$data['role']) {
//find user role from token
$token = $entityManager->tokenRepository->findOneByAccessToken($data['access_token']);
$user = $entityManager->userRepository->findOneByUsername($token->getUserId());
$token->setRole($user->getRole());
$entityManager->persist($token);
$entityManager->flush();
$data['role'] = $user->getRole();
$e->getResult()->setContent(json_encode($data));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment