Forked from theMadness/OverrideServiceCompilerPassTest.php
Last active
November 14, 2018 20:34
-
-
Save tarlepp/588db0caf3faad7391097645bb97d443 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace AppBundle\DependencyInjection\Compiler; | |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
class OverrideServiceCompilerPass implements CompilerPassInterface | |
{ | |
private $environment; | |
public function __construct(string $environment) | |
{ | |
$this->environment = $environment; | |
} | |
public function process(ContainerBuilder $container): void | |
{ | |
// @TODO: Add parameter check for an enabling flag | |
// use $this->environment here | |
if ($container->hasParameter('security.role_hierarchy.roles')) { | |
$roles = $container->getParameter('security.role_hierarchy.roles'); | |
$roles['ROLE_TO_EXTEND'][] = 'ROLE_CAN_EDIT_SOME_RESOURCE'; | |
$roles['ROLE_TO_EXTEND'][] = 'ROLE_CAN_DELETE_SOME_RESOURCE'; | |
$container->setParameter('security.role_hierarchy.roles', $roles); | |
} | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Tests\AppBundle\DependencyInjection\Compiler; | |
use AppBundle\Security\DeepRoleChecker; | |
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | |
class OverrideServiceCompilerPassTest extends KernelTestCase | |
{ | |
public function testRoleCanEditResourceInjection(): void | |
{ | |
$container = new \ Symfony\Component\DependencyInjection\ContainerBuilder(); | |
$overrideServiceCompilerPass = new \AppBundle\DependencyInjection\Compiler\OverrideServiceCompilerPass('your env'); | |
$overrideServiceCompilerPass->process($container); | |
// Check that your container has expected parameters | |
$actualParameter = $container->getParameter('security.role_hierarchy.roles'); | |
static::assertSame('expected_params', $actualParameter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment