Skip to content

Commit 8ad51a6

Browse files
dnahrebeckidkarlovi
authored andcommitted
- controller as a service
1 parent 71530ec commit 8ad51a6

File tree

7 files changed

+163
-67
lines changed

7 files changed

+163
-67
lines changed

Controller/AuthorizeController.php

Lines changed: 126 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414
use FOS\OAuthServerBundle\Event\OAuthEvent;
1515
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
1616
use FOS\OAuthServerBundle\Model\ClientInterface;
17+
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
18+
use OAuth2\OAuth2;
1719
use OAuth2\OAuth2ServerException;
18-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19-
use Symfony\Component\DependencyInjection\ContainerInterface;
20+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
21+
use Symfony\Component\EventDispatcher\EventDispatcher;
22+
use Symfony\Component\Form\Form;
2023
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpFoundation\RequestStack;
2125
use Symfony\Component\HttpFoundation\Response;
26+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2227
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28+
use Symfony\Component\Routing\Router;
29+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2330
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2431
use Symfony\Component\Security\Core\User\UserInterface;
2532

@@ -28,66 +35,147 @@
2835
*
2936
* @author Chris Jones <[email protected]>
3037
*/
31-
class AuthorizeController implements ContainerAwareInterface
38+
class AuthorizeController
3239
{
3340
/**
3441
* @var ClientInterface
3542
*/
3643
private $client;
3744

3845
/**
39-
* @var ContainerInterface
46+
* @var SessionInterface
4047
*/
41-
protected $container;
48+
private $session;
4249

4350
/**
44-
* Sets the container.
51+
* @var Form
52+
*/
53+
private $authorizeForm;
54+
55+
/**
56+
* @var AuthorizeFormHandler
57+
*/
58+
private $authorizeFormHandler;
59+
60+
/**
61+
* @var OAuth2
62+
*/
63+
private $oAuth2Server;
64+
65+
/**
66+
* @var EngineInterface
67+
*/
68+
private $templating;
69+
70+
/**
71+
* @var RequestStack
72+
*/
73+
private $requestStack;
74+
75+
/**
76+
* @var TokenStorageInterface
77+
*/
78+
private $tokenStorage;
79+
80+
/**
81+
* @var Router
82+
*/
83+
private $router;
84+
85+
/**
86+
* @var ClientManagerInterface
87+
*/
88+
private $clientManager;
89+
90+
/**
91+
* @var string
92+
*/
93+
private $templateEngineType;
94+
95+
/**
96+
* @var EventDispatcher
97+
*/
98+
private $eventDispatcher;
99+
100+
/**
101+
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
102+
* Thus, there is considered a bad practice to fetch services directly from container.
103+
* @todo This controller could be refactored to do not rely on so many dependencies
45104
*
46-
* @param ContainerInterface|null $container A ContainerInterface instance or null
105+
* @param RequestStack $requestStack
106+
* @param SessionInterface $session
107+
* @param Form $authorizeForm
108+
* @param AuthorizeFormHandler $authorizeFormHandler
109+
* @param OAuth2 $oAuth2Server
110+
* @param EngineInterface $templating
111+
* @param TokenStorageInterface $tokenStorage
112+
* @param Router $router
113+
* @param ClientManagerInterface $clientManager
114+
* @param EventDispatcher $eventDispatcher
115+
* @param string $templateEngineType
47116
*/
48-
public function setContainer(ContainerInterface $container = null)
49-
{
50-
$this->container = $container;
117+
public function __construct(
118+
RequestStack $requestStack,
119+
SessionInterface $session,
120+
Form $authorizeForm,
121+
AuthorizeFormHandler $authorizeFormHandler,
122+
OAuth2 $oAuth2Server,
123+
EngineInterface $templating,
124+
TokenStorageInterface $tokenStorage,
125+
Router $router,
126+
ClientManagerInterface $clientManager,
127+
EventDispatcher $eventDispatcher,
128+
$templateEngineType = 'twig'
129+
) {
130+
$this->requestStack = $requestStack;
131+
$this->session = $session;
132+
$this->authorizeForm = $authorizeForm;
133+
$this->authorizeFormHandler = $authorizeFormHandler;
134+
$this->oAuth2Server = $oAuth2Server;
135+
$this->templating = $templating;
136+
$this->tokenStorage = $tokenStorage;
137+
$this->router = $router;
138+
$this->clientManager = $clientManager;
139+
$this->templateEngineType = $templateEngineType;
140+
$this->eventDispatcher = $eventDispatcher;
51141
}
52142

53143
/**
54144
* Authorize.
55145
*/
56146
public function authorizeAction(Request $request)
57147
{
58-
$user = $this->getTokenStorage()->getToken()->getUser();
148+
$user = $this->tokenStorage->getToken()->getUser();
59149

60150
if (!$user instanceof UserInterface) {
61151
throw new AccessDeniedException('This user does not have access to this section.');
62152
}
63153

64-
if (true === $this->container->get('session')->get('_fos_oauth_server.ensure_logout')) {
65-
$this->container->get('session')->invalidate(600);
66-
$this->container->get('session')->set('_fos_oauth_server.ensure_logout', true);
154+
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
155+
$this->session->invalidate(600);
156+
$this->session->set('_fos_oauth_server.ensure_logout', true);
67157
}
68158

69-
$form = $this->container->get('fos_oauth_server.authorize.form');
70-
$formHandler = $this->container->get('fos_oauth_server.authorize.form.handler');
159+
$form = $this->authorizeForm;
160+
$formHandler = $this->authorizeFormHandler;
71161

72-
$event = $this->container->get('event_dispatcher')->dispatch(
162+
$event = $this->eventDispatcher->dispatch(
73163
OAuthEvent::PRE_AUTHORIZATION_PROCESS,
74164
new OAuthEvent($user, $this->getClient())
75165
);
76166

77167
if ($event->isAuthorizedClient()) {
78168
$scope = $request->get('scope', null);
79169

80-
return $this->container
81-
->get('fos_oauth_server.server')
82-
->finishClientAuthorization(true, $user, $request, $scope);
170+
return $this->oAuth2Server->finishClientAuthorization(true, $user, $request, $scope);
83171
}
84172

85173
if (true === $formHandler->process()) {
86174
return $this->processSuccess($user, $formHandler, $request);
87175
}
88176

89-
return $this->container->get('templating')->renderResponse(
90-
'FOSOAuthServerBundle:Authorize:authorize.html.'.$this->container->getParameter('fos_oauth_server.template.engine'),
177+
return $this->templating->renderResponse(
178+
'FOSOAuthServerBundle:Authorize:authorize.html.'.$this->templateEngineType,
91179
array(
92180
'form' => $form->createView(),
93181
'client' => $this->getClient(),
@@ -104,24 +192,23 @@ public function authorizeAction(Request $request)
104192
*/
105193
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
106194
{
107-
if (true === $this->container->get('session')->get('_fos_oauth_server.ensure_logout')) {
108-
$this->getTokenStorage()->setToken(null);
109-
$this->container->get('session')->invalidate();
195+
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
196+
$this->tokenStorage->setToken(null);
197+
$this->session->invalidate();
110198
}
111199

112-
$this->container->get('event_dispatcher')->dispatch(
200+
$this->eventDispatcher->dispatch(
113201
OAuthEvent::POST_AUTHORIZATION_PROCESS,
114202
new OAuthEvent($user, $this->getClient(), $formHandler->isAccepted())
115203
);
116204

117-
$formName = $this->container->get('fos_oauth_server.authorize.form')->getName();
205+
$formName = $this->authorizeForm->getName();
118206
if (!$request->query->all() && $request->request->has($formName)) {
119207
$request->query->add($request->request->get($formName));
120208
}
121209

122210
try {
123-
return $this->container
124-
->get('fos_oauth_server.server')
211+
return $this->oAuth2Server
125212
->finishClientAuthorization($formHandler->isAccepted(), $user, $request, $formHandler->getScope());
126213
} catch (OAuth2ServerException $e) {
127214
return $e->getHttpResponse();
@@ -137,7 +224,7 @@ protected function processSuccess(UserInterface $user, AuthorizeFormHandler $for
137224
*/
138225
protected function getRedirectionUrl(UserInterface $user)
139226
{
140-
return $this->container->get('router')->generate('fos_oauth_server_profile_show');
227+
return $this->router->generate('fos_oauth_server_profile_show');
141228
}
142229

143230
/**
@@ -151,14 +238,12 @@ protected function getClient()
151238
$client = null;
152239
if (null !== $request) {
153240
if (null === $clientId = $request->get('client_id')) {
154-
$form = $this->container->get('fos_oauth_server.authorize.form');
241+
$form = $this->authorizeForm;
155242
$formData = $request->get($form->getName(), array());
156243
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
157244
}
158245

159-
$client = $this->container
160-
->get('fos_oauth_server.client_manager')
161-
->findClientByPublicId($clientId);
246+
$client = $this->clientManager->findClientByPublicId($clientId);
162247
}
163248

164249
if (null === $client) {
@@ -171,26 +256,16 @@ protected function getClient()
171256
return $this->client;
172257
}
173258

259+
/**
260+
* @return null|Request
261+
*/
174262
private function getCurrentRequest()
175263
{
176-
if ($this->container->has('request_stack')) {
177-
$request = $this->container->get('request_stack')->getCurrentRequest();
178-
if (null === $request) {
179-
throw new \RuntimeException('No current request.');
180-
}
181-
182-
return $request;
183-
} else {
184-
return $this->container->get('request');
185-
}
186-
}
187-
188-
private function getTokenStorage()
189-
{
190-
if ($this->container->has('security.token_storage')) {
191-
return $this->container->get('security.token_storage');
264+
$request = $this->requestStack->getCurrentRequest();
265+
if (null === $request) {
266+
throw new \RuntimeException('No current request.');
192267
}
193268

194-
return $this->container->get('security.context');
269+
return $request;
195270
}
196271
}

Form/Handler/AuthorizeFormHandler.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ public function __construct(FormInterface $form, $requestStack = null)
5151
$this->requestStack = $requestStack;
5252
}
5353

54-
/**
55-
* Sets the container.
56-
*
57-
* @param ContainerInterface|null $container A ContainerInterface instance or null
58-
*/
59-
public function setContainer(ContainerInterface $container = null)
60-
{
61-
$this->container = $container;
62-
}
63-
6454
public function isAccepted()
6555
{
6656
return $this->form->getData()->accepted;
@@ -133,7 +123,5 @@ private function getCurrentRequest()
133123
return $this->requestStack->getCurrentRequest();
134124
}
135125
}
136-
137-
return $this->container->get('request');
138126
}
139127
}

Resources/config/authorize.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@
2020

2121
<service id="fos_oauth_server.authorize.form.handler.default" class="FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler">
2222
<argument type="service" id="fos_oauth_server.authorize.form" />
23-
<argument type="service" id="request_stack" on-invalid="ignore" />
23+
<argument type="service" id="request_stack" />
24+
</service>
25+
26+
<service id="fos_oauth_server.controller.authorize" class="FOS\OAuthServerBundle\Controller\AuthorizeController">
27+
<argument type="service" id="request_stack" />
28+
<argument type="service" id="session" />
29+
<argument type="service" id="fos_oauth_server.authorize.form" />
30+
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
31+
<argument type="service" id="fos_oauth_server.server" />
32+
<argument type="service" id="templating" />
33+
<argument type="service" id="security.token_storage" />
34+
<argument type="service" id="router" />
35+
<argument type="service" id="fos_oauth_server.client_manager" />
36+
<argument type="service" id="event_dispatcher" />
37+
<argument>%fos_oauth_server.template.engine%</argument>
2438
</service>
2539
</services>
2640

Resources/config/routing/authorize.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

77
<route id="fos_oauth_server_authorize" path="/oauth/v2/auth" methods="GET POST">
8-
<default key="_controller">FOSOAuthServerBundle:Authorize:authorize</default>
8+
<default key="_controller">fos_oauth_server.controller.authorize:authorizeAction</default>
99
</route>
1010

1111
</routes>

Resources/doc/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Getting Started With FOSOAuthServerBundle
33

44
## Prerequisites
55

6-
This version of the bundle requires Symfony 2.1.
6+
This version of the bundle requires Symfony 2.8.
77
If you are using Symfony 2.0.x, please use the 1.1.1 release of the bundle (or lower), and follow
88
[this documentation](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/1.1.1/README.md).
99

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace FOS\OAuthServerBundle\Tests\Functional\Controller;
4+
5+
use FOS\OAuthServerBundle\Controller\AuthorizeController;
6+
use FOS\OAuthServerBundle\Tests\Functional\TestCase;
7+
8+
class AuthorizeControllerTest extends TestCase
9+
{
10+
public function testAuthorizeControllerIsAccessibleViaController()
11+
{
12+
$kernel = static::createKernel(array('env' => 'orm'));
13+
$kernel->boot();
14+
15+
$authController = $kernel->getContainer()->get('fos_oauth_server.controller.authorize');
16+
17+
$this->assertInstanceOf(AuthorizeController::class, $authController);
18+
}
19+
}

Tests/Functional/config/config_orm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ imports:
44
doctrine:
55
dbal:
66
driver: pdo_sqlite
7-
path: %kernel.cache_dir%/data.sqlite
7+
path: '%kernel.cache_dir%/data.sqlite'
88
orm:
99
entity_managers:
1010
default:

0 commit comments

Comments
 (0)