Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tarlepp/588db0caf3faad7391097645bb97d443 to your computer and use it in GitHub Desktop.
Save tarlepp/588db0caf3faad7391097645bb97d443 to your computer and use it in GitHub Desktop.
<?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);
}
}
}
<?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