Skip to content

Commit 93a30d0

Browse files
dinamicdkarlovi
authored andcommitted
Merge 1.6 into master
1 parent 85409bb commit 93a30d0

27 files changed

+732
-277
lines changed

Controller/AuthorizeController.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use Symfony\Component\HttpFoundation\Response;
3030
use Symfony\Component\HttpFoundation\Session\SessionInterface;
3131
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32-
use Symfony\Component\Routing\Router;
32+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
3333
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
3434
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
3535
use Symfony\Component\Security\Core\User\UserInterface;
@@ -86,7 +86,7 @@ class AuthorizeController implements ContainerAwareInterface
8686
private $tokenStorage;
8787

8888
/**
89-
* @var Router
89+
* @var UrlGeneratorInterface
9090
*/
9191
private $router;
9292

@@ -118,7 +118,7 @@ class AuthorizeController implements ContainerAwareInterface
118118
* @param OAuth2 $oAuth2Server
119119
* @param EngineInterface $templating
120120
* @param TokenStorageInterface $tokenStorage
121-
* @param Router $router
121+
* @param UrlGeneratorInterface $router
122122
* @param ClientManagerInterface $clientManager
123123
* @param EventDispatcher $eventDispatcher
124124
* @param string $templateEngineType
@@ -131,7 +131,7 @@ public function __construct(
131131
OAuth2 $oAuth2Server,
132132
EngineInterface $templating,
133133
TokenStorageInterface $tokenStorage,
134-
Router $router,
134+
UrlGeneratorInterface $router,
135135
ClientManagerInterface $clientManager,
136136
EventDispatcher $eventDispatcher,
137137
$templateEngineType = 'twig'
@@ -253,25 +253,23 @@ protected function getRedirectionUrl(UserInterface $user)
253253
*/
254254
protected function getClient()
255255
{
256-
if (null === $this->client) {
257-
$request = $this->getCurrentRequest();
256+
if (null !== $this->client) {
257+
return $this->client;
258+
}
258259

259-
$client = null;
260-
if (null !== $request) {
261-
if (null === $clientId = $request->get('client_id')) {
262-
$form = $this->authorizeForm;
263-
$formData = $request->get($form->getName(), []);
264-
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
265-
}
260+
if (null === $request = $this->getCurrentRequest()) {
261+
throw new NotFoundHttpException('Client not found.');
262+
}
266263

267-
$client = $this->clientManager->findClientByPublicId($clientId);
268-
}
264+
if (null === $clientId = $request->get('client_id')) {
265+
$formData = $request->get($this->authorizeForm->getName(), []);
266+
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
267+
}
269268

270-
if (null === $client) {
271-
throw new NotFoundHttpException('Client not found.');
272-
}
269+
$this->client = $this->clientManager->findClientByPublicId($clientId);
273270

274-
$this->client = $client;
271+
if (null === $this->client) {
272+
throw new NotFoundHttpException('Client not found.');
275273
}
276274

277275
return $this->client;

Entity/AuthCodeManager.php

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

1414
namespace FOS\OAuthServerBundle\Entity;
1515

16-
use Doctrine\ORM\EntityManager;
16+
use Doctrine\ORM\EntityManagerInterface;
1717
use FOS\OAuthServerBundle\Model\AuthCodeInterface;
1818
use FOS\OAuthServerBundle\Model\AuthCodeManager as BaseAuthCodeManager;
1919

2020
class AuthCodeManager extends BaseAuthCodeManager
2121
{
2222
/**
23-
* @var EntityManager
23+
* @var EntityManagerInterface
2424
*/
2525
protected $em;
2626

@@ -30,10 +30,10 @@ class AuthCodeManager extends BaseAuthCodeManager
3030
protected $class;
3131

3232
/**
33-
* @param EntityManager $em
34-
* @param string $class
33+
* @param EntityManagerInterface $em
34+
* @param string $class
3535
*/
36-
public function __construct(EntityManager $em, $class)
36+
public function __construct(EntityManagerInterface $em, $class)
3737
{
3838
$this->em = $em;
3939
$this->class = $class;

Form/Handler/AuthorizeFormHandler.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,34 @@ public function isRejected()
7373
return !$this->form->getData()->accepted;
7474
}
7575

76+
/**
77+
* @return bool
78+
*/
7679
public function process()
7780
{
7881
$request = $this->getCurrentRequest();
79-
if (null !== $request) {
80-
$this->form->setData(new Authorize(
81-
$request->request->has('accepted'),
82-
$request->query->all()
83-
));
84-
85-
if ('POST' === $request->getMethod()) {
86-
$this->form->handleRequest($request);
87-
if ($this->form->isValid()) {
88-
$this->onSuccess();
89-
90-
return true;
91-
}
92-
}
82+
83+
if (null === $request) {
84+
return false;
85+
}
86+
87+
$this->form->setData(new Authorize(
88+
$request->request->has('accepted'),
89+
$request->query->all()
90+
));
91+
92+
if ('POST' !== $request->getMethod()) {
93+
return false;
94+
}
95+
96+
$this->form->handleRequest($request);
97+
if (!$this->form->isValid()) {
98+
return false;
9399
}
94100

95-
return false;
101+
$this->onSuccess();
102+
103+
return true;
96104
}
97105

98106
public function getScope()
@@ -119,14 +127,14 @@ protected function onSuccess()
119127

120128
private function getCurrentRequest()
121129
{
122-
if (null !== $this->requestStack) {
123-
if ($this->requestStack instanceof Request) {
124-
return $this->requestStack;
125-
}
130+
if (null === $this->requestStack) {
131+
return $this->container->get('request');
132+
}
126133

127-
return $this->requestStack->getCurrentRequest();
134+
if ($this->requestStack instanceof Request) {
135+
return $this->requestStack;
128136
}
129137

130-
return $this->container->get('request');
138+
return $this->requestStack->getCurrentRequest();
131139
}
132140
}

Tests/Controller/AuthorizeControllerTest.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the FOSOAuthServerBundle package.
7+
*
8+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
314
namespace FOS\OAuthServerBundle\Tests\Controller;
415

516
use FOS\OAuthServerBundle\Controller\AuthorizeController;
@@ -8,6 +19,7 @@
819
use FOS\OAuthServerBundle\Model\ClientInterface;
920
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
1021
use OAuth2\OAuth2;
22+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1123
use Symfony\Component\EventDispatcher\EventDispatcher;
1224
use Symfony\Component\Form\Form;
1325
use Symfony\Component\Form\FormView;
@@ -18,12 +30,11 @@
1830
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1931
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2032
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
21-
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
2233
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2334
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2435
use Symfony\Component\Security\Core\User\UserInterface;
2536

26-
class AuthorizeControllerTest extends \PHPUnit_Framework_TestCase
37+
class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
2738
{
2839
/**
2940
* @var \PHPUnit_Framework_MockObject_MockObject|RequestStack
@@ -90,6 +101,16 @@ class AuthorizeControllerTest extends \PHPUnit_Framework_TestCase
90101
*/
91102
protected $request;
92103

104+
/**
105+
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterBag
106+
*/
107+
protected $requestQuery;
108+
109+
/**
110+
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterBag
111+
*/
112+
protected $requestRequest;
113+
93114
/**
94115
* @var \PHPUnit_Framework_MockObject_MockObject|UserInterface
95116
*/
@@ -168,18 +189,22 @@ public function setUp()
168189
$this->templateEngineType
169190
);
170191

171-
$this->request = $this->getMockBuilder(Request::class)
192+
/** @var \PHPUnit_Framework_MockObject_MockObject&Request $request */
193+
$request = $this->getMockBuilder(Request::class)
172194
->disableOriginalConstructor()
173195
->getMock()
174196
;
175-
$this->request->query = $this->getMockBuilder(ParameterBag::class)
197+
$this->requestQuery = $this->getMockBuilder(ParameterBag::class)
176198
->disableOriginalConstructor()
177199
->getMock()
178200
;
179-
$this->request->request = $this->getMockBuilder(ParameterBag::class)
201+
$this->requestRequest = $this->getMockBuilder(ParameterBag::class)
180202
->disableOriginalConstructor()
181203
->getMock()
182204
;
205+
$request->query = $this->requestQuery;
206+
$request->request = $this->requestRequest;
207+
$this->request = $request;
183208
$this->user = $this->getMockBuilder(UserInterface::class)
184209
->disableOriginalConstructor()
185210
->getMock()
@@ -219,10 +244,8 @@ public function testAuthorizeActionWillThrowAccessDeniedException()
219244
->willReturn(null)
220245
;
221246

222-
$this->setExpectedException(
223-
AccessDeniedException::class,
224-
'This user does not have access to this section.');
225-
;
247+
$this->expectException(AccessDeniedException::class);
248+
$this->expectExceptionMessage('This user does not have access to this section.');
226249

227250
$this->instance->authorizeAction($this->request);
228251
}
@@ -292,7 +315,7 @@ public function testAuthorizeActionWillRenderTemplate()
292315
->with(
293316
'FOSOAuthServerBundle:Authorize:authorize.html.twig',
294317
[
295-
'form' => $this->formView,
318+
'form' => $this->formView,
296319
'client' => $this->client,
297320
]
298321
)
@@ -346,7 +369,7 @@ public function testAuthorizeActionWillFinishClientAuthorization()
346369
->willReturn(true)
347370
;
348371

349-
$randomScope = 'scope' . \random_bytes(10);
372+
$randomScope = 'scope'.\random_bytes(10);
350373

351374
$this->request
352375
->expects($this->at(0))
@@ -451,7 +474,7 @@ public function testAuthorizeActionWillEnsureLogout()
451474
->with(
452475
'FOSOAuthServerBundle:Authorize:authorize.html.twig',
453476
[
454-
'form' => $this->formView,
477+
'form' => $this->formView,
455478
'client' => $this->client,
456479
]
457480
)
@@ -525,28 +548,28 @@ public function testAuthorizeActionWillProcessAuthorizationForm()
525548
)
526549
;
527550

528-
$formName = 'formName' . \random_bytes(10);
551+
$formName = 'formName'.\random_bytes(10);
529552

530553
$this->form
531554
->expects($this->once())
532555
->method('getName')
533556
->willReturn($formName)
534557
;
535558

536-
$this->request->query
559+
$this->requestQuery
537560
->expects($this->once())
538561
->method('all')
539562
->willReturn([])
540563
;
541564

542-
$this->request->request
565+
$this->requestRequest
543566
->expects($this->once())
544567
->method('has')
545568
->with($formName)
546569
->willReturn(false)
547570
;
548571

549-
$randomScope = 'scope' . \random_bytes(10);
572+
$randomScope = 'scope'.\random_bytes(10);
550573

551574
$this->authorizeFormHandler
552575
->expects($this->once())

0 commit comments

Comments
 (0)