Ejemplo n.º 1
0
 public function testGetAuthenticationResponse()
 {
     $token = \Phake::mock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $resp = Authenticator::getAuthenticationResponse($token);
     $this->assertInternalType('array', $resp);
     $this->assertArrayHasKey('success', $resp);
     $this->assertFalse($resp['success']);
     $user = new User();
     $user->setFirstName('John');
     $user->setLastName('Doe');
     $user->setEmail('*****@*****.**');
     $user->setUsername('john.doe');
     $role = \Phake::mock('Symfony\\Component\\Security\\Core\\Role\\RoleInterface');
     \Phake::when($role)->getRole()->thenReturn('ROLE_USER');
     \Phake::when($token)->isAuthenticated()->thenReturn(true);
     \Phake::when($token)->getUser()->thenReturn($user);
     \Phake::when($token)->getRoles()->thenReturn(array($role));
     $resp = Authenticator::getAuthenticationResponse($token);
     $this->assertInternalType('array', $resp);
     $this->assertArrayHasKey('success', $resp);
     $this->assertTrue($resp['success']);
     $this->assertArrayHasKey('profile', $resp);
     $this->assertInternalType('array', $resp['profile']);
     $this->assertEquals(array('id' => $user->getId(), 'name' => $user->getFullName(), 'email' => $user->getEmail(), 'username' => $user->getUsername()), $resp['profile']);
 }
Ejemplo n.º 2
0
 /**
  * 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);
 }