0

I had implemented FOSOAuthServerBundle with Symfony2.8 successfully and it worked. When I tried to work it with Symfony3.2 I had error:

Attempted to load class "SecurityContext" from namespace "Symfony\Component\Security\Core". Did you forget a "use" statement for another namespace?

So I googled and now know that SecurityContext doesn't exists in Symofny 3.2 anymore. But in FOSOAuthServerBundle official documentation "A Note About Security" still exists function loginAction() only compactible with symfony2.

Question is: - can I use this bundle with Symfony 3.2 ? - If yes, is there any documentation how to do it, or better any example ?

thank you very much for answers

milan74sa
  • 1
  • 2

2 Answers2

3

Don't know the FOSOAuthServerBundle in detail. But I guess the example in the documentation of the bundle at a_note_about_security is just outdated.

The security.context service was deprecated since symfony 2.6. Here you can find a description of the changes: symfony.com/blog/new-in-symfony-2-6-security-component-improvements

You could try to replace \Symfony\Component\Security\Core\SecurityContext with \Symfony\Component\Security\Core\Security

<?php
// src/Acme/SecurityBundle/Controller/SecurityController.php

namespace Acme\SecurityBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Security;

class SecurityController extends Controller
{
    public function loginAction()
    {
        $request = $this->getRequest();
        $session = $request->getSession();

        // get the login error if there is one
        if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
        } else {
            $error = $session->get(Security::AUTHENTICATION_ERROR);
            $session->remove(Security::AUTHENTICATION_ERROR);
        }

        // Add the following lines
        if ($session->has('_security.target_path')) {
            if (false !== strpos($session->get('_security.target_path'), $this->generateUrl('fos_oauth_server_authorize'))) {
                $session->set('_fos_oauth_server.ensure_logout', true);
            }
        }

        return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
            // last username entered by the user
            'last_username' => $session->get(Security::LAST_USERNAME),
            'error'         => $error,
        ));
    }
}
W0rma
  • 309
  • 2
  • 9
0

I figured it out. Maybe it help somebody.

public function loginAction(Request $request) {

    $authenticationUtils = $this->get('security.authentication_utils');

    $error = $authenticationUtils->getLastAuthenticationError();
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('AppBundle:Security:login.html.twig', array(
                'last_username' => $lastUsername
                , 'error' => $error
    ));
}
milan74sa
  • 1
  • 2