Skip to content

Commit

Permalink
Merge pull request hwi#1592 from dmaicher/symfony_5_support
Browse files Browse the repository at this point in the history
Symfony 5 compatibility
  • Loading branch information
XWB authored Jan 20, 2020
2 parents 295bc6c + 760b568 commit fb33993
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 351 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ matrix:
env: SYMFONY_REQUIRE=4.4.*
- php: 7.4
env: SYMFONY_REQUIRE=4.4.*
- php: 7.4
env: SYMFONY_REQUIRE=5.0.* REMOVE_FOS=1
- php: 7.3
env: SYMFONY_REQUIRE=5.0.* REMOVE_FOS=1
- php: 7.4
env: TARGET=csfixer_dry_run

Expand All @@ -29,6 +32,7 @@ before_install:
- composer global require --no-progress --no-scripts --no-plugins symfony/flex

before_script:
- if [[ ${REMOVE_FOS} ]]; then composer remove friendsofsymfony/user-bundle --no-update --no-interaction --dev; fi;
- composer install --prefer-dist --no-interaction --no-scripts

script:
Expand Down
49 changes: 23 additions & 26 deletions Controller/ConnectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
Expand All @@ -40,11 +38,8 @@
/**
* @author Alexander <[email protected]>
*/
final class ConnectController implements ContainerAwareInterface
final class ConnectController extends AbstractController
{
use ContainerAwareTrait;
use ControllerTrait;

/**
* @var OAuthUtils
*/
Expand All @@ -55,10 +50,6 @@ final class ConnectController implements ContainerAwareInterface
*/
private $resourceOwnerMapLocator;

/**
* @param OAuthUtils $oauthUtils
* @param ResourceOwnerMapLocator $resourceOwnerMapLocator
*/
public function __construct(OAuthUtils $oauthUtils, ResourceOwnerMapLocator $resourceOwnerMapLocator)
{
$this->oauthUtils = $oauthUtils;
Expand All @@ -80,12 +71,12 @@ public function __construct(OAuthUtils $oauthUtils, ResourceOwnerMapLocator $res
*/
public function registrationAction(Request $request, $key)
{
$connect = $this->container->getParameter('hwi_oauth.connect');
$connect = $this->getParameter('hwi_oauth.connect');
if (!$connect) {
throw new NotFoundHttpException();
}

$hasUser = $this->isGranted($this->container->getParameter('hwi_oauth.grant_rule'));
$hasUser = $this->isGranted($this->getParameter('hwi_oauth.grant_rule'));
if ($hasUser) {
throw new AccessDeniedException('Cannot connect already registered account.');
}
Expand All @@ -110,14 +101,14 @@ public function registrationAction(Request $request, $key)
;

/* @var $form FormInterface */
$form = $this->container->get('hwi_oauth.registration.form.factory')->createForm();
$form = $this->get('hwi_oauth.registration.form.factory')->createForm();

$formHandler = $this->container->get('hwi_oauth.registration.form.handler');
$formHandler = $this->get('hwi_oauth.registration.form.handler');
if ($formHandler->process($request, $form, $userInformation)) {
$event = new FormEvent($form, $request);
$this->dispatch($event, HWIOAuthEvents::REGISTRATION_SUCCESS);

$this->container->get('hwi_oauth.account.connector')->connect($form->getData(), $userInformation);
$this->get('hwi_oauth.account.connector')->connect($form->getData(), $userInformation);

// Authenticate the user
$this->authenticateUser($request, $form->getData(), $error->getResourceOwnerName(), $error->getAccessToken());
Expand Down Expand Up @@ -172,12 +163,12 @@ public function registrationAction(Request $request, $key)
*/
public function connectServiceAction(Request $request, $service)
{
$connect = $this->container->getParameter('hwi_oauth.connect');
$connect = $this->getParameter('hwi_oauth.connect');
if (!$connect) {
throw new NotFoundHttpException();
}

$hasUser = $this->isGranted($this->container->getParameter('hwi_oauth.grant_rule'));
$hasUser = $this->isGranted($this->getParameter('hwi_oauth.grant_rule'));
if (!$hasUser) {
throw new AccessDeniedException('Cannot connect an account.');
}
Expand Down Expand Up @@ -209,15 +200,15 @@ public function connectServiceAction(Request $request, $service)

// Redirect to the login path if the token is empty (Eg. User cancelled auth)
if (null === $accessToken) {
if ($this->container->getParameter('hwi_oauth.failed_use_referer') && $targetPath = $this->getTargetPath($session)) {
if ($this->getParameter('hwi_oauth.failed_use_referer') && $targetPath = $this->getTargetPath($session)) {
return $this->redirect($targetPath);
}

return $this->redirectToRoute($this->container->getParameter('hwi_oauth.failed_auth_path'));
return $this->redirectToRoute($this->getParameter('hwi_oauth.failed_auth_path'));
}

// Show confirmation page?
if (!$this->container->getParameter('hwi_oauth.connect.confirmation')) {
if (!$this->getParameter('hwi_oauth.connect.confirmation')) {
return $this->getConfirmationResponse($request, $accessToken, $service);
}

Expand Down Expand Up @@ -247,6 +238,12 @@ public function connectServiceAction(Request $request, $service)
]);
}

protected function getParameter(string $name)
{
// Symfony 3.4 compat
return $this->container->getParameter($name);
}

/**
* Get a resource owner by name.
*
Expand All @@ -258,7 +255,7 @@ public function connectServiceAction(Request $request, $service)
*/
private function getResourceOwnerByName($name)
{
foreach ($this->container->getParameter('hwi_oauth.firewall_names') as $firewall) {
foreach ($this->getParameter('hwi_oauth.firewall_names') as $firewall) {
if (!$this->resourceOwnerMapLocator->has($firewall)) {
continue;
}
Expand All @@ -284,7 +281,7 @@ private function getResourceOwnerByName($name)
private function authenticateUser(Request $request, UserInterface $user, $resourceOwnerName, $accessToken, $fakeLogin = true)
{
try {
$userChecker = $this->container->get('hwi_oauth.user_checker');
$userChecker = $this->get('hwi_oauth.user_checker');
$userChecker->checkPreAuth($user);
$userChecker->checkPostAuth($user);
} catch (AccountStatusException $e) {
Expand Down Expand Up @@ -319,7 +316,7 @@ private function getTargetPath(?SessionInterface $session)
return null;
}

foreach ($this->container->getParameter('hwi_oauth.firewall_names') as $providerKey) {
foreach ($this->getParameter('hwi_oauth.firewall_names') as $providerKey) {
$sessionKey = '_security.'.$providerKey.'.target_path';
if ($session->has($sessionKey)) {
return $session->get($sessionKey);
Expand All @@ -341,7 +338,7 @@ private function getTargetPath(?SessionInterface $session)
private function getConfirmationResponse(Request $request, array $accessToken, $service)
{
/** @var $currentToken OAuthToken */
$currentToken = $this->container->get('security.token_storage')->getToken();
$currentToken = $this->get('security.token_storage')->getToken();
/** @var $currentUser UserInterface */
$currentUser = $currentToken->getUser();

Expand All @@ -353,7 +350,7 @@ private function getConfirmationResponse(Request $request, array $accessToken, $
$event = new GetResponseUserEvent($currentUser, $request);
$this->dispatch($event, HWIOAuthEvents::CONNECT_CONFIRMED);

$this->container->get('hwi_oauth.account.connector')->connect($currentUser, $userInformation);
$this->get('hwi_oauth.account.connector')->connect($currentUser, $userInformation);

if ($currentToken instanceof OAuthToken) {
// Update user token with new details
Expand Down
10 changes: 1 addition & 9 deletions Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ final class LoginController
*/
private $session;

/**
* @param AuthenticationUtils $authenticationUtils
* @param Environment $twig
* @param RouterInterface $router
* @param AuthorizationCheckerInterface $authorizationChecker
* @param SessionInterface $session
* @param bool $connect
* @param string $grantRule
*/
public function __construct(
AuthenticationUtils $authenticationUtils,
Environment $twig,
Expand All @@ -85,6 +76,7 @@ public function __construct(
$this->twig = $twig;
$this->router = $router;
$this->authorizationChecker = $authorizationChecker;
$this->session = $session;
$this->connect = $connect;
$this->grantRule = $grantRule;
}
Expand Down
7 changes: 0 additions & 7 deletions Controller/RedirectToServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ final class RedirectToServiceController
*/
private $useReferer;

/**
* @param OAuthUtils $oauthUtils
* @param array $firewallNames
* @param string $targetPathParameter
* @param bool $failedUseReferer
* @param bool $useReferer
*/
public function __construct(OAuthUtils $oauthUtils, array $firewallNames, ?string $targetPathParameter, bool $failedUseReferer, bool $useReferer)
{
$this->oauthUtils = $oauthUtils;
Expand Down
27 changes: 27 additions & 0 deletions Event/AbstractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the HWIOAuthBundle package.
*
* (c) Hardware Info <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace HWI\Bundle\OAuthBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;

if (class_exists(Event::class)) {
// Symfony < 5
abstract class AbstractEvent extends Event
{
}
} else {
// Symfony 5
abstract class AbstractEvent extends ContractsEvent
{
}
}
3 changes: 1 addition & 2 deletions Event/FormEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

namespace HWI\Bundle\OAuthBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Marek Štípek
*/
class FormEvent extends Event
class FormEvent extends AbstractEvent
{
/**
* @var FormInterface
Expand Down
3 changes: 1 addition & 2 deletions Event/UserEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

namespace HWI\Bundle\OAuthBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @author Marek Štípek
*/
class UserEvent extends Event
class UserEvent extends AbstractEvent
{
/**
* @var UserInterface
Expand Down
Loading

0 comments on commit fb33993

Please sign in to comment.