/**
  * @param TokenStorageInterface $tokenStorage
  * @param ContributorInterface  $clientDiDefinitionsProvider
  */
 public function __construct(TokenStorageInterface $tokenStorage, ContributorInterface $clientDiDefinitionsProvider)
 {
     $this->items = array(new CallbackConfigMerger(function (array $currentConfig) use($tokenStorage) {
         // we are not making sure that user is authenticated here because we expect that this
         // callback is invoked only when user is already authenticated (invoked from behind a firewall)
         if ($token = $tokenStorage->getToken()) {
             $roles = array();
             foreach ($token->getRoles() as $role) {
                 $roles[] = $role->getRole();
             }
             return array_merge($currentConfig, array('roles' => $roles, 'userProfile' => Authenticator::userToArray($token->getUser())));
         } else {
             return $currentConfig;
         }
     }), new CallbackConfigMerger(function (array $currentConfig) use($clientDiDefinitionsProvider) {
         return array_merge($currentConfig, array('serviceDefinitions' => $clientDiDefinitionsProvider->getItems()));
     }));
 }
 /**
  * Endpoint can be used by MJR to figure out if user is already authenticated and therefore
  * runtime UI can be loaded.
  *
  * @param Request $request
  *
  * @return JsonResponse
  */
 public function isAuthenticatedAction(Request $request)
 {
     $this->initSession($request);
     /* @var SecurityContextInterface $sc */
     $sc = $this->get('security.context');
     $token = $sc->getToken();
     $response = Authenticator::getAuthenticationResponse($token);
     if ($response['success']) {
         $roleNames = [];
         foreach ($token->getRoles() as $roleName) {
             $roleNames[] = $roleName->getRole();
         }
         if (!in_array(ModeraMJRSecurityIntegrationBundle::ROLE_BACKEND_USER, $roleNames)) {
             $response = array('success' => false, 'message' => "You don't have required rights to access administration interface.");
         }
     }
     return new JsonResponse($response);
 }
 public function testUserToArray()
 {
     $user = \Phake::mock(User::clazz());
     \Phake::when($user)->getId()->thenReturn(777);
     \Phake::when($user)->getFullName()->thenReturn('John Doe');
     \Phake::when($user)->getEmail()->thenReturn('*****@*****.**');
     \Phake::when($user)->getUsername()->thenReturn('john.doe');
     $result = Authenticator::userToArray($user);
     $this->assertTrue(is_array($result));
     $this->assertArrayHasKey('id', $result);
     $this->assertArrayHasKey('name', $result);
     $this->assertArrayHasKey('email', $result);
     $this->assertArrayHasKey('username', $result);
     $this->assertEquals(777, $result['id']);
     $this->assertEquals('John Doe', $result['name']);
     $this->assertEquals('*****@*****.**', $result['email']);
     $this->assertEquals('john.doe', $result['username']);
 }