This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow access annotation without admin annotation present, update read…
…me and tests
- Loading branch information
1 parent
78763f5
commit b3de700
Showing
8 changed files
with
221 additions
and
65 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataAnnotationBundle\DependencyInjection\Compiler; | ||
|
||
use Doctrine\Common\Annotations\Reader; | ||
use KunicMarko\SonataAnnotationBundle\Annotation\Access; | ||
use KunicMarko\SonataAnnotationBundle\Annotation\Admin; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
/** | ||
* @author Marko Kunic <[email protected]> | ||
*/ | ||
final class AccessCompilerPass implements CompilerPassInterface | ||
{ | ||
private const ENTITY_ARGUMENT_IN_SERVICE_DEFINITION = 1; | ||
|
||
/** | ||
* @var Reader | ||
*/ | ||
private $annotationReader; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
$this->annotationReader = $container->get('annotation_reader'); | ||
$roles = $container->getParameter('security.role_hierarchy.roles'); | ||
|
||
foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $tag) { | ||
if (!($class = $this->getClass($container, $id))) { | ||
continue; | ||
} | ||
|
||
if ($permissions = $this->getRoles(new \ReflectionClass($class), $this->getRolePrefix($id))) { | ||
$roles = array_merge_recursive($roles, $permissions); | ||
} | ||
} | ||
|
||
$container->setParameter('security.role_hierarchy.roles', $roles); | ||
} | ||
|
||
private function getClass(ContainerBuilder $container, string $id): ?string | ||
{ | ||
$definition = $container->getDefinition($id); | ||
|
||
//Entity can be a class or a parameter | ||
$class = $definition->getArgument(self::ENTITY_ARGUMENT_IN_SERVICE_DEFINITION); | ||
|
||
if ($class[0] !== '%') { | ||
return $class; | ||
} | ||
|
||
if ($container->hasParameter($class = trim($class, '%'))) { | ||
return $container->getParameter($class); | ||
} | ||
|
||
throw new \LogicException(sprintf( | ||
'Service "%s" has a parameter "%s" as an argument but it is not found.', | ||
$id, | ||
$class | ||
)); | ||
} | ||
|
||
private function getRolePrefix(string $serviceId): string | ||
{ | ||
return 'ROLE_' . str_replace('.', '_', strtoupper($serviceId)) . '_'; | ||
} | ||
|
||
private function getRoles(\ReflectionClass $class, string $prefix): array | ||
{ | ||
$roles = []; | ||
|
||
foreach ($this->annotationReader->getClassAnnotations($class) as $annotation) { | ||
if (!$annotation instanceof Access) { | ||
continue; | ||
} | ||
|
||
$roles[$annotation->getRole()] = array_map( | ||
function (string $permission) use ($prefix) { | ||
return $prefix . strtoupper($permission); | ||
}, | ||
$annotation->permissions | ||
); | ||
} | ||
|
||
return $roles; | ||
} | ||
} |
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
89 changes: 89 additions & 0 deletions
89
tests/DependencyInjection/Compiler/AccessCompilerPassTest.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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataAnnotationBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use KunicMarko\SonataAnnotationBundle\DependencyInjection\Compiler\AccessCompilerPass; | ||
use KunicMarko\SonataAnnotationBundle\DependencyInjection\Compiler\AutoRegisterCompilerPass; | ||
use KunicMarko\SonataAnnotationBundle\Tests\Fixtures\AccessExceptionClass; | ||
use KunicMarko\SonataAnnotationBundle\Tests\Fixtures\AnnotationClass; | ||
use KunicMarko\SonataAnnotationBundle\Tests\Fixtures\AnnotationExceptionClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* @author Marko Kunic <[email protected]> | ||
*/ | ||
final class AccessCompilerPassTest extends TestCase | ||
{ | ||
/** | ||
* @var ContainerBuilder | ||
*/ | ||
private $container; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->container = new ContainerBuilder(); | ||
$this->container->set('annotation_reader', new AnnotationReader()); | ||
$this->container->setParameter('security.role_hierarchy.roles', []); | ||
} | ||
|
||
public function testProcess(): void | ||
{ | ||
$this->initAdminClasses(); | ||
|
||
$accessCompilerPass = new AccessCompilerPass(); | ||
$accessCompilerPass->process($this->container); | ||
|
||
$this->assertArrayHasKey('ROLE_VENDOR', $roles = $this->container->getParameter('security.role_hierarchy.roles')); | ||
$this->assertContains('ROLE_APP_ADMIN_ANNOTATIONCLASS_LIST', $roles['ROLE_VENDOR']); | ||
$this->assertContains('ROLE_APP_ADMIN_ANNOTATIONEXCEPTIONCLASS_ALL', $roles['ROLE_VENDOR']); | ||
} | ||
|
||
private function initAdminClasses( | ||
$classes = [AnnotationClass::class, AnnotationExceptionClass::class, null] | ||
) { | ||
foreach ($classes as $key => $class) { | ||
$definition = new Definition( | ||
'test', | ||
[null, $class, null] | ||
); | ||
|
||
$definition->addTag('sonata.admin', []); | ||
|
||
$className = explode("\\", $class ?? ''); | ||
|
||
$this->container->setDefinition( | ||
'app.admin.' . end($className), | ||
$definition | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @expectedException \LogicException | ||
* @expectedExceptionMessage Service "app.admin.%class%" has a parameter "class" as an argument but it is not found. | ||
*/ | ||
public function testProcessExceptionParamNotFound(): void | ||
{ | ||
$this->initAdminClasses(['%class%']); | ||
|
||
$accessCompilerPass = new AccessCompilerPass(); | ||
$accessCompilerPass->process($this->container); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Argument "role" is mandatory in "KunicMarko\SonataAnnotationBundle\Annotation\Access" annotation. | ||
*/ | ||
public function testProcessExceptionAnnotation(): void | ||
{ | ||
$this->initAdminClasses([AccessExceptionClass::class]); | ||
|
||
$accessCompilerPass = new AccessCompilerPass(); | ||
$accessCompilerPass->process($this->container); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata; | ||
|
||
/** | ||
* @Sonata\Admin("Test") | ||
* @Sonata\Access() | ||
* | ||
* @author Marko Kunic <[email protected]> | ||
|
Oops, something went wrong.