Skip to content

Instantly share code, notes, and snippets.

@virtualize
Last active April 8, 2020 15:35
Show Gist options
  • Save virtualize/0d95a3f6568e95d2cdc88a13036e648c to your computer and use it in GitHub Desktop.
Save virtualize/0d95a3f6568e95d2cdc88a13036e648c to your computer and use it in GitHub Desktop.
Symfony: call voters via decision manager in cli command
<?php
public function __construct(ManagerRegistry $doctrine, AccessDecisionManagerInterface $decisionManager)
{
parent::__construct();
$this->doctrine = $doctrine;
$this->decisionManager = $decisionManager;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// create new user token, as there is no session user in commands
// where you get your user from is up to you
// 'app_user_provider' is the user provider key from security.yaml
$token = new UsernamePasswordToken($user, [], 'app_user_provider');
// $attributes is an array, in contrary to what a single voter expects
$bool = $this->decisionManager->decide($token, $attributes, $subject);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment