Skip to content

Commit cf83f62

Browse files
committed
bug #134 [AIBundle] Remove security listener when missing dependencies (valtzu)
This PR was merged into the main branch. Discussion ---------- [AIBundle] Remove security listener when missing dependencies | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #133 | License | MIT Make `security-core` optional + also register the attribute for autoconfiguration & throw to make sure no one tries to use the attribute without the listener (which would make one think that you have some access control when you don't). Commits ------- f81a540 [AIBundle] Remove security listener when missing dependencies
2 parents b71153b + f81a540 commit cf83f62

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/ai-bundle/src/AIBundle.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\AI\AIBundle\Profiler\DataCollector;
2828
use Symfony\AI\AIBundle\Profiler\TraceablePlatform;
2929
use Symfony\AI\AIBundle\Profiler\TraceableToolbox;
30+
use Symfony\AI\AIBundle\Security\Attribute\IsGrantedTool;
3031
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory as AnthropicPlatformFactory;
3132
use Symfony\AI\Platform\Bridge\Azure\OpenAI\PlatformFactory as AzureOpenAIPlatformFactory;
3233
use Symfony\AI\Platform\Bridge\Google\PlatformFactory as GooglePlatformFactory;
@@ -53,6 +54,7 @@
5354
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
5455
use Symfony\Component\DependencyInjection\Reference;
5556
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
57+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
5658

5759
use function Symfony\Component\String\u;
5860

@@ -131,6 +133,14 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
131133
$builder->registerForAutoconfiguration(ResponseConverterInterface::class)
132134
->addTag('symfony_ai.platform.response_converter');
133135

136+
if (!ContainerBuilder::willBeAvailable('symfony/security-core', AuthorizationCheckerInterface::class, ['symfony/ai-bundle'])) {
137+
$builder->removeDefinition('symfony_ai.security.is_granted_attribute_listener');
138+
$builder->registerAttributeForAutoconfiguration(
139+
IsGrantedTool::class,
140+
static fn () => throw new \InvalidArgumentException('Using #[IsGrantedTool] attribute requires additional dependencies. Try running "composer install symfony/security-core".'),
141+
);
142+
}
143+
134144
if (false === $builder->getParameter('kernel.debug')) {
135145
$builder->removeDefinition(DataCollector::class);
136146
$builder->removeDefinition(TraceableToolbox::class);

0 commit comments

Comments
 (0)