as instructed in https://github.com/glennthehuman/encryption-bundle/blob/master/Resources/doc/index.md
I executed: php bin/console jagilpe:encryption:user:generate_keys
But I got:
There are no commands defined in the "jagilpe:encryption:user" namespace.
So, I checked this folder structure With the code:
<?php
namespace Jagilpe\EncryptionBundle\Command;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Jagilpe\EncryptionBundle\Entity\PKEncryptionEnabledUserInterface;
use Symfony\Component\Console\Helper\ProgressBar;
class CreateUserKeysCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('jagilpe:encryption:user:generate_keys')
->setDescription('Generates the encryption keys of a user')
->addArgument(
'usename',
InputArgument::OPTIONAL,
'The name of the user whose keys we want to create.'
)
->addOption(
'all',
null,
InputOption::VALUE_NONE,
'If the keys of all users should be generated.'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Input parameters
$userName = $input->getArgument('usename');
$allUsers = $input->getOption('all');
if (!$userName && !$allUsers) {
throw new \RuntimeException('Wrong parameters given');
}
if ($userName && $allUsers) {
throw new \RuntimeException('Ambiguous parameters given');
}
$users = $this->getUsers($userName);
$total = count($users);
$message = "Generating the encryption keys for $total users";
$output->writeln($message);
$progress = new ProgressBar($output, $total);
foreach ($users as $user) {
$this->generateKeys($user);
$this->saveUser($user);
$progress->advance();
}
$progress->finish();
$output->writeln('');
}
private function getUsers($userName)
{
$container = $this->getContainer();
$entityManager = $container->get('doctrine')->getManager();
$encryptionSettings = $container->getParameter('jagilpe_encryption.settings');
$userClasses = $encryptionSettings['user_classes'];
$users = array();
foreach ($userClasses as $userClass) {
$userRepo = $entityManager->getRepository($userClass);
if ($userName) {
$user = $userRepo->findOneBy(array('username' => $userName));
$users = array($user);
break;
}
else {
$users = array_merge($users, $userRepo->findAll());
}
}
return $users;
}
private function generateKeys(PKEncryptionEnabledUserInterface $user)
{
if (!$user->getPublicKey() || !$user->getPrivateKey()) {
$container = $this->getContainer();
$keyManager = $container->get('jagilpe_encryption.key_manager');
$keyManager->generateUserPKIKeys($user);
}
}
private function saveUser(PKEncryptionEnabledUserInterface $user)
{
$userClass = ClassUtils::getClass($user);
$userRepo = $this->getContainer()->get('doctrine')->getManager()->getRepository($userClass);
$userRepo->save($user);
}
}
What's wrong with this?
By the way, I was able to install the bundle without any problem. The source codes can be correctly accessed and used in my own codes. I just cannot run the aforementioned command properly. I also created my own command in my own Command directory and they were properly detected and executed.
This one concept helped me to get working commands from 3rd party vendors in symfony 4.*.
Just register needed command in your services.yaml file and you are ready to go.