Using Symfony5
I've created a command that executes a certain task that I trigger with :
docker-compose exec container bin/console app:do-smthg
Now when this task is triggered it'll go in the database and modify a field so the action is not uselessly repeated for every instance of this entity.
But I can't persist
/ flush
the object cause :
The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL
I've done a bit of digging on that and it might come from the fact the a firewall uses isGranted()
method while launching this command.
Dependencies:
protected static string $commandName = 'app:do-smthg';
private EntityManagerInterface $em;
public function __construct(
EntityManagerInterface $em,
string $name = 'app:send-sms'
)
{
parent::__construct($name);
$this->em = $em;
}
Actual method:
public function execute(InputInterface $input, OutputInterface $output)
{
$instance = $this->instanceRepository->findOneBy(['somefield' => somevalue]);
if ($entity->getField() == false) {
// I do something here
// then I set a field and persist
$instance->setField(true);
$this->em->persist($instance);
$this->em->flush();
}
return Command::SUCCESS;
}
The command works fine without the persist()
and flush()
method which first triggered this error message.
Does anyone have any idea on how I could workaround the firewall calling isGranted()
method when I execute this command ?
Or if there is any other kind of workaround that already exists ?
Edit:
My application is using JWT
Normally the firewall and related security stuff only applies to HTTP requests. Console commands don't have users and permissions as such. So it was puzzling that a simple entity update was apparently triggering isGranted.
Just as a guess I suggested looking into Doctrine events. It's possible that they could be checking for permissions. And as it turns out, I guessed right. I guess the 'value' of this answer is that there is often more going in than meets the eye.