-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #803 from PROCERGS/preview-1.19.2
v1.19.2
- Loading branch information
Showing
15 changed files
with
531 additions
and
135 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/LoginCidadao/CoreBundle/Service/AuthorizationManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* This file is part of the login-cidadao project or it's bundles. | ||
* | ||
* (c) Guilherme Donato <guilhermednt on github> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace LoginCidadao\CoreBundle\Service; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use LoginCidadao\CoreBundle\Entity\Authorization; | ||
use LoginCidadao\CoreBundle\Entity\AuthorizationRepository; | ||
use LoginCidadao\CoreBundle\Model\PersonInterface; | ||
use LoginCidadao\OAuthBundle\Model\ClientInterface; | ||
|
||
class AuthorizationManager | ||
{ | ||
/** @var string merge new scope with old one */ | ||
public const SCOPE_MERGE = 'merge'; | ||
|
||
/** @var string replace old scope by new one */ | ||
public const SCOPE_REPLACE = 'replace'; | ||
|
||
/** @var EntityManagerInterface */ | ||
private $em; | ||
|
||
/** @var AuthorizationRepository */ | ||
private $repository; | ||
|
||
/** | ||
* AuthorizationManager constructor. | ||
* @param EntityManagerInterface $em | ||
* @param AuthorizationRepository $repository | ||
*/ | ||
public function __construct(EntityManagerInterface $em, AuthorizationRepository $repository) | ||
{ | ||
$this->em = $em; | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* @param PersonInterface $person | ||
* @param ClientInterface $client | ||
* @return Authorization|null | ||
*/ | ||
public function getAuthorization(PersonInterface $person, ClientInterface $client): ?Authorization | ||
{ | ||
/** @var Authorization $authorization */ | ||
$authorization = $this->repository->findOneBy([ | ||
'person' => $person, | ||
'client' => $client, | ||
]); | ||
|
||
return $authorization; | ||
} | ||
|
||
/** | ||
* @param PersonInterface $person | ||
* @param ClientInterface $client | ||
* @param array $scope | ||
* @param string $scopeStrategy | ||
* @param bool $flush | ||
* @return Authorization | ||
*/ | ||
public function enforceAuthorization( | ||
PersonInterface $person, | ||
ClientInterface $client, | ||
array $scope, | ||
string $scopeStrategy, | ||
bool $flush = true | ||
): Authorization { | ||
$authorization = $this->getAuthorization($person, $client); | ||
if (null === $authorization) { | ||
$authorization = (new Authorization()) | ||
->setPerson($person) | ||
->setClient($client); | ||
$this->em->persist($authorization); | ||
} | ||
|
||
$authorization->setScope( | ||
$this->mergeScope($authorization->getScope(), $scope, $scopeStrategy) | ||
); | ||
|
||
if ($flush) { | ||
$this->em->flush(); | ||
} | ||
|
||
return $authorization; | ||
} | ||
|
||
private function mergeScope(array $oldScope, array $newScope, string $strategy) | ||
{ | ||
if (self::SCOPE_REPLACE === $strategy) { | ||
return $newScope; | ||
} elseif (self::SCOPE_MERGE === $strategy) { | ||
return array_unique(array_merge($oldScope, $newScope)); | ||
} else { | ||
throw new \InvalidArgumentException("Invalid scope merging strategy: '{$strategy}'"); | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/LoginCidadao/CoreBundle/Tests/Service/AuthorizationManagerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* This file is part of the login-cidadao project or it's bundles. | ||
* | ||
* (c) Guilherme Donato <guilhermednt on github> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace LoginCidadao\CoreBundle\Tests\Service; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use LoginCidadao\CoreBundle\Entity\Authorization; | ||
use LoginCidadao\CoreBundle\Entity\AuthorizationRepository; | ||
use LoginCidadao\CoreBundle\Entity\Person; | ||
use LoginCidadao\CoreBundle\Service\AuthorizationManager; | ||
use LoginCidadao\OAuthBundle\Entity\Client; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AuthorizationManagerTest extends TestCase | ||
{ | ||
public function testGetAuthorization() | ||
{ | ||
$person = new Person(); | ||
$client = new Client(); | ||
$authorization = (new Authorization()) | ||
->setPerson($person) | ||
->setClient($client); | ||
|
||
/** @var EntityManagerInterface|MockObject $em */ | ||
$em = $this->createMock(EntityManagerInterface::class); | ||
|
||
/** @var AuthorizationRepository|MockObject $repo */ | ||
$repo = $this->createMock(AuthorizationRepository::class); | ||
$repo->expects($this->once())->method('findOneBy') | ||
->with(['person' => $person, 'client' => $client]) | ||
->willReturn($authorization); | ||
|
||
$manager = new AuthorizationManager($em, $repo); | ||
$this->assertSame($authorization, $manager->getAuthorization($person, $client)); | ||
} | ||
|
||
public function testEnforceNewAuthorization() | ||
{ | ||
$person = new Person(); | ||
$client = new Client(); | ||
$scope = ['new']; | ||
$strategy = AuthorizationManager::SCOPE_REPLACE; | ||
|
||
/** @var EntityManagerInterface|MockObject $em */ | ||
$em = $this->createMock(EntityManagerInterface::class); | ||
$em->expects($this->once())->method('flush'); | ||
$em->expects($this->once())->method('persist')->with($this->isInstanceOf(Authorization::class)); | ||
|
||
/** @var AuthorizationRepository|MockObject $repo */ | ||
$repo = $this->createMock(AuthorizationRepository::class); | ||
|
||
$manager = new AuthorizationManager($em, $repo); | ||
$authorization = $manager->enforceAuthorization($person, $client, $scope, $strategy); | ||
|
||
$this->assertInstanceOf(Authorization::class, $authorization); | ||
$this->assertContains('new', $authorization->getScope()); | ||
} | ||
|
||
public function testEnforceExistingAuthorization() | ||
{ | ||
$person = new Person(); | ||
$client = new Client(); | ||
$scope = ['new']; | ||
$strategy = AuthorizationManager::SCOPE_MERGE; | ||
|
||
$existingAuthorization = (new Authorization()) | ||
->setPerson($person) | ||
->setClient($client) | ||
->setScope(['old']); | ||
|
||
/** @var EntityManagerInterface|MockObject $em */ | ||
$em = $this->createMock(EntityManagerInterface::class); | ||
$em->expects($this->once())->method('flush'); | ||
$em->expects($this->never())->method('persist')->with($this->isInstanceOf(Authorization::class)); | ||
|
||
/** @var AuthorizationRepository|MockObject $repo */ | ||
$repo = $this->createMock(AuthorizationRepository::class); | ||
$repo->expects($this->once())->method('findOneBy') | ||
->with(['person' => $person, 'client' => $client]) | ||
->willReturn($existingAuthorization); | ||
|
||
$manager = new AuthorizationManager($em, $repo); | ||
$authorization = $manager->enforceAuthorization($person, $client, $scope, $strategy); | ||
|
||
$this->assertInstanceOf(Authorization::class, $authorization); | ||
$this->assertContains('new', $authorization->getScope()); | ||
$this->assertContains('old', $authorization->getScope()); | ||
} | ||
|
||
public function testMergeFailsWithInvalidStrategy() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
|
||
$person = new Person(); | ||
$client = new Client(); | ||
$scope = ['new']; | ||
$strategy = 'invalid'; | ||
|
||
/** @var EntityManagerInterface|MockObject $em */ | ||
$em = $this->createMock(EntityManagerInterface::class); | ||
$em->expects($this->never())->method('flush'); | ||
|
||
/** @var AuthorizationRepository|MockObject $repo */ | ||
$repo = $this->createMock(AuthorizationRepository::class); | ||
|
||
$manager = new AuthorizationManager($em, $repo); | ||
$authorization = $manager->enforceAuthorization($person, $client, $scope, $strategy); | ||
|
||
$this->assertInstanceOf(Authorization::class, $authorization); | ||
$this->assertContains('new', $authorization->getScope()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.