Skip to content

Commit 5fece85

Browse files
committed
Merge pull request #374 from Ener-Getick/SF3
[Extended] Add symfony 3.0 support
2 parents 3d2a16e + 93172a3 commit 5fece85

File tree

11 files changed

+243
-61
lines changed

11 files changed

+243
-61
lines changed

.travis.yml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: php
22

3+
sudo: false
4+
35
php:
46
- 5.3
57
- 5.4
@@ -16,29 +18,22 @@ matrix:
1618
- php: 5.6
1719
env: SYMFONY_VERSION=2.3.*
1820
- php: 5.6
19-
env: SYMFONY_VERSION=2.5.*
20-
- php: 5.6
21-
env: SYMFONY_VERSION=2.6.*
22-
- php: 5.6
23-
env: SYMFONY_VERSION=2.7.*@dev
21+
env: SYMFONY_VERSION=2.7.*
2422
- php: 5.6
25-
env: SYMFONY_VERSION=2.8.*@dev
23+
env: SYMFONY_VERSION=2.8.*
2624
- php: 5.6
27-
env: SYMFONY_VERSION="3.0.x-dev as 2.6"
25+
env: SYMFONY_VERSION=3.0.*
2826
allow_failures:
2927
- php: hhvm
3028
- php: nightly
31-
- env: SYMFONY_VERSION=2.7.*@dev
32-
- env: SYMFONY_VERSION=2.8.*@dev
33-
- env: SYMFONY_VERSION="3.0.x-dev as 2.6"
3429

3530
cache:
3631
directories:
3732
- vendor
3833
- $HOME/.composer/cache
3934

4035
before_script:
41-
- echo "extension=mongo.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
36+
- echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
4237
- composer selfupdate
4338
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
4439
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS

Controller/AuthorizeController.php

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313

1414
use FOS\OAuthServerBundle\Event\OAuthEvent;
1515
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
16-
use OAuth2\OAuth2;
16+
use FOS\OAuthServerBundle\Model\ClientInterface;
1717
use OAuth2\OAuth2ServerException;
18-
use Symfony\Component\DependencyInjection\ContainerAware;
18+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19+
use Symfony\Component\DependencyInjection\ContainerInterface;
20+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1921
use Symfony\Component\HttpFoundation\Request;
22+
use Symfony\Component\HttpFoundation\Response;
2023
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2124
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2225
use Symfony\Component\Security\Core\User\UserInterface;
@@ -26,19 +29,34 @@
2629
*
2730
* @author Chris Jones <[email protected]>
2831
*/
29-
class AuthorizeController extends ContainerAware
32+
class AuthorizeController implements ContainerAwareInterface
3033
{
3134
/**
3235
* @var \FOS\OAuthServerBundle\Model\ClientInterface
3336
*/
3437
private $client;
3538

39+
/**
40+
* @var ContainerInterface
41+
*/
42+
protected $container;
43+
44+
/**
45+
* Sets the container.
46+
*
47+
* @param ContainerInterface|null $container A ContainerInterface instance or null
48+
*/
49+
public function setContainer(ContainerInterface $container = null)
50+
{
51+
$this->container = $container;
52+
}
53+
3654
/**
3755
* Authorize
3856
*/
3957
public function authorizeAction(Request $request)
4058
{
41-
$user = $this->container->get('security.context')->getToken()->getUser();
59+
$user = $this->getTokenStorage()->getToken()->getUser();
4260

4361
if (!$user instanceof UserInterface) {
4462
throw new AccessDeniedException('This user does not have access to this section.');
@@ -58,7 +76,7 @@ public function authorizeAction(Request $request)
5876
);
5977

6078
if ($event->isAuthorizedClient()) {
61-
$scope = $this->container->get('request')->get('scope', null);
79+
$scope = $request->get('scope', null);
6280

6381
return $this->container
6482
->get('fos_oauth_server.server')
@@ -81,13 +99,14 @@ public function authorizeAction(Request $request)
8199
/**
82100
* @param UserInterface $user
83101
* @param AuthorizeFormHandler $formHandler
102+
* @param Request $request
84103
*
85104
* @return Response
86105
*/
87106
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
88107
{
89108
if (true === $this->container->get('session')->get('_fos_oauth_server.ensure_logout')) {
90-
$this->container->get('security.context')->setToken(null);
109+
$this->getTokenStorage()->setToken(null);
91110
$this->container->get('session')->invalidate();
92111
}
93112

@@ -127,16 +146,20 @@ protected function getRedirectionUrl(UserInterface $user)
127146
protected function getClient()
128147
{
129148
if (null === $this->client) {
130-
if (null === $clientId = $this->container->get('request')->get('client_id')) {
131-
$form = $this->container->get('fos_oauth_server.authorize.form');
132-
$clientId = $this->container->get('request')
133-
->get(sprintf('%s[client_id]', $form->getName()), null, true);
149+
$request = $this->getCurrentRequest();
150+
151+
$client = null;
152+
if (null !== $request) {
153+
if (null === $clientId = $request->get('client_id')) {
154+
$form = $this->container->get('fos_oauth_server.authorize.form');
155+
$clientId = $request->get(sprintf('%s[client_id]', $form->getName()), null, true);
156+
}
157+
158+
$client = $this->container
159+
->get('fos_oauth_server.client_manager')
160+
->findClientByPublicId($clientId);
134161
}
135162

136-
$client = $this->container
137-
->get('fos_oauth_server.client_manager')
138-
->findClientByPublicId($clientId);
139-
140163
if (null === $client) {
141164
throw new NotFoundHttpException('Client not found.');
142165
}
@@ -146,4 +169,26 @@ protected function getClient()
146169

147170
return $this->client;
148171
}
172+
173+
private function getCurrentRequest() {
174+
if($this->container->has('request_stack')) {
175+
$request = $this->container->get('request_stack')->getCurrentRequest();
176+
if(null === $request) {
177+
throw new \RuntimeException('No current request.');
178+
}
179+
180+
return $request;
181+
} else {
182+
return $this->container->get('request');
183+
}
184+
}
185+
186+
private function getTokenStorage()
187+
{
188+
if($this->container->has('security.token_storage')) {
189+
return $this->container->get('security.token_storage');
190+
}
191+
192+
return $this->container->get('security.context');
193+
}
149194
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSOAuthServerBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\OAuthServerBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @author Ener-Getick <[email protected]>
20+
*
21+
* @internal
22+
*/
23+
final class RequestStackCompilerPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
if($container->has('request_stack')) {
31+
return;
32+
}
33+
34+
$definition = $container->getDefinition('fos_oauth_server.authorize.form.handler.default');
35+
$definition->addMethodCall('setContainer', array(new Reference('service_container')));
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSOAuthServerBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\OAuthServerBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @author Andras Ratz <[email protected]>
20+
*/
21+
class TokenStorageCompilerPass implements CompilerPassInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function process(ContainerBuilder $container)
27+
{
28+
$definition = $container->getDefinition('fos_oauth_server.security.authentication.listener');
29+
30+
if ($container->hasDefinition('security.token_storage') === false) {
31+
$definition->replaceArgument(0, new Reference('security.context'));
32+
}
33+
}
34+
}

FOSOAuthServerBundle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOS\OAuthServerBundle;
1313

14+
use FOS\OAuthServerBundle\DependencyInjection\Compiler\TokenStorageCompilerPass;
15+
use FOS\OAuthServerBundle\DependencyInjection\Compiler\RequestStackCompilerPass;
1416
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
1517
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
1618
use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
@@ -35,5 +37,7 @@ public function build(ContainerBuilder $container)
3537
}
3638

3739
$container->addCompilerPass(new GrantExtensionsCompilerPass());
40+
$container->addCompilerPass(new TokenStorageCompilerPass());
41+
$container->addCompilerPass(new RequestStackCompilerPass());
3842
}
3943
}

Form/Handler/AuthorizeFormHandler.php

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,55 @@
1111

1212
namespace FOS\OAuthServerBundle\Form\Handler;
1313

14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1416
use Symfony\Component\Form\FormInterface;
1517
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1619
use FOS\OAuthServerBundle\Form\Model\Authorize;
1720

1821
/**
1922
* @author Chris Jones <[email protected]>
2023
*/
2124
class AuthorizeFormHandler
2225
{
23-
protected $request;
26+
/**
27+
* @var FormInterface
28+
*/
2429
protected $form;
2530

26-
public function __construct(FormInterface $form, Request $request)
31+
/**
32+
* @var ContainerInterface
33+
*/
34+
protected $container;
35+
36+
/**
37+
* @var RequestStack|Request|null
38+
*/
39+
private $requestStack;
40+
41+
/**
42+
* @param FormInterface $form
43+
* @param Request|RequestStack $requestStack
44+
*/
45+
public function __construct(FormInterface $form, $requestStack = null)
2746
{
47+
if(null !== $requestStack && !$requestStack instanceof RequestStack && !$requestStack instanceof Request) {
48+
throw new \InvalidArgumentException(sprintf('Argument 2 of %s must be an instanceof RequestStack or Request', __CLASS__));
49+
}
50+
2851
$this->form = $form;
29-
$this->request = $request;
52+
$this->requestStack = $requestStack;
53+
}
54+
55+
/**
56+
* Sets the container.
57+
*
58+
* @param ContainerInterface|null $container A ContainerInterface instance or null
59+
*/
60+
public function setContainer(ContainerInterface $container = null)
61+
{
62+
$this->container = $container;
3063
}
3164

3265
public function isAccepted()
@@ -41,17 +74,20 @@ public function isRejected()
4174

4275
public function process()
4376
{
44-
$this->form->setData(new Authorize(
45-
$this->request->request->has('accepted'),
46-
$this->request->query->all()
47-
));
77+
$request = $this->getCurrentRequest();
78+
if (null !== $request) {
79+
$this->form->setData(new Authorize(
80+
$request->request->has('accepted'),
81+
$request->query->all()
82+
));
4883

49-
if ('POST' === $this->request->getMethod()) {
50-
$this->form->bind($this->request);
51-
if ($this->form->isValid()) {
52-
$this->onSuccess();
84+
if ('POST' === $request->getMethod()) {
85+
$this->form->handleRequest($request);
86+
if ($this->form->isValid()) {
87+
$this->onSuccess();
5388

54-
return true;
89+
return true;
90+
}
5591
}
5692
}
5793

@@ -63,6 +99,15 @@ public function getScope()
6399
return $this->form->getData()->scope;
64100
}
65101

102+
public function __get($name)
103+
{
104+
if ($name === 'request') {
105+
@trigger_error(sprintf('%s::$request is deprecated since 1.4 and will be removed in 2.0.', __CLASS__), E_USER_DEPRECATED);
106+
107+
return $this->getCurrentRequest();
108+
}
109+
}
110+
66111
/**
67112
* Put form data in $_GET so that OAuth2 library will call Request::createFromGlobals()
68113
*
@@ -79,4 +124,17 @@ protected function onSuccess()
79124
'scope' => $this->form->getData()->scope,
80125
);
81126
}
127+
128+
private function getCurrentRequest()
129+
{
130+
if (null !== $this->requestStack) {
131+
if ($this->requestStack instanceof Request) {
132+
return $this->requestStack;
133+
} else {
134+
return $this->requestStack->getCurrentRequest();
135+
}
136+
}
137+
138+
return $this->container->get('request');
139+
}
82140
}

0 commit comments

Comments
 (0)