Last active
April 8, 2020 15:35
-
-
Save virtualize/0d95a3f6568e95d2cdc88a13036e648c to your computer and use it in GitHub Desktop.
Symfony: call voters via decision manager in cli command
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
<?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