From b45191bd04fee37c69161464b8c75e95e902a283 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 26 Jan 2021 13:47:35 +0100 Subject: [PATCH] resolving deprecated private service injection on commands classes --- Command/ChillImportUsersCommand.php | 4 +- .../ChillUserSendRenewPasswordCodeCommand.php | 13 +++-- Command/LoadAndUpdateLanguagesCommand.php | 33 +++++++++++-- Command/LoadCountriesCommand.php | 29 +++++++++-- Command/LoadPostalCodesCommand.php | 39 ++++++++++++--- Command/SetPasswordCommand.php | 48 ++++++++++++++----- Controller/PermissionsGroupController.php | 1 + config/services/command.yaml | 35 +++++++++++++- 8 files changed, 168 insertions(+), 34 deletions(-) diff --git a/Command/ChillImportUsersCommand.php b/Command/ChillImportUsersCommand.php index 38da1daf4..99bd17d8f 100644 --- a/Command/ChillImportUsersCommand.php +++ b/Command/ChillImportUsersCommand.php @@ -2,7 +2,7 @@ namespace Chill\MainBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -21,7 +21,7 @@ use Chill\MainBundle\Entity\PermissionsGroup; use Symfony\Component\Console\Question\ChoiceQuestion; use League\Csv\Writer; -class ChillImportUsersCommand extends ContainerAwareCommand +class ChillImportUsersCommand extends Command { /** diff --git a/Command/ChillUserSendRenewPasswordCodeCommand.php b/Command/ChillUserSendRenewPasswordCodeCommand.php index 69089ecf4..bea2fa1a5 100644 --- a/Command/ChillUserSendRenewPasswordCodeCommand.php +++ b/Command/ChillUserSendRenewPasswordCodeCommand.php @@ -2,7 +2,7 @@ namespace Chill\MainBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -16,7 +16,12 @@ use Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper; use Chill\MainBundle\Security\PasswordRecover\PasswordRecoverEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -class ChillUserSendRenewPasswordCodeCommand extends ContainerAwareCommand +/** + * Class ChillUserSendRenewPasswordCodeCommand + * + * @package Chill\MainBundle\Command + */ +class ChillUserSendRenewPasswordCodeCommand extends Command { /** * @@ -66,8 +71,8 @@ class ChillUserSendRenewPasswordCodeCommand extends ContainerAwareCommand LoggerInterface $logger, EntityManagerInterface $em, RecoverPasswordHelper $recoverPasswordHelper, - EventDispatcherInterface $eventDispatcher) - { + EventDispatcherInterface $eventDispatcher + ) { $this->logger = $logger; $this->em = $em; $this->recoverPasswordHelper = $recoverPasswordHelper; diff --git a/Command/LoadAndUpdateLanguagesCommand.php b/Command/LoadAndUpdateLanguagesCommand.php index aa01085da..a5c53a551 100644 --- a/Command/LoadAndUpdateLanguagesCommand.php +++ b/Command/LoadAndUpdateLanguagesCommand.php @@ -19,7 +19,8 @@ namespace Chill\MainBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Doctrine\ORM\EntityManager; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Intl\Intl; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -28,8 +29,16 @@ use Symfony\Component\Console\Output\OutputInterface; /* * Load or update the languages entities command */ -class LoadAndUpdateLanguagesCommand extends ContainerAwareCommand +class LoadAndUpdateLanguagesCommand extends Command { + + /** + * @var EntityManager + */ + private $entityManager; + + private $availableLanguages; + // The regional version of language are language with _ in the code // This array contains regional code to not exclude private $regionalVersionToInclude = ["ro_MD"]; @@ -40,7 +49,21 @@ class LoadAndUpdateLanguagesCommand extends ContainerAwareCommand const INCLUDE_REGIONAL_VERSION = 'include_regional'; const INCLUDE_ANCIENT = 'include_ancient'; - + + /** + * LoadCountriesCommand constructor. + * + * @param EntityManager $entityManager + * @param $availableLanguages + */ + public function __construct(EntityManager $entityManager, $availableLanguages) + { + $this->entityManager=$entityManager; + $this->availableLanguages=$availableLanguages; + parent::__construct(); + } + + /* * (non-PHPdoc) * @see \Symfony\Component\Console\Command\Command::configure() @@ -72,8 +95,8 @@ class LoadAndUpdateLanguagesCommand extends ContainerAwareCommand */ protected function execute(InputInterface $input, OutputInterface $output) { - $em = $this->getContainer()->get('doctrine.orm.entity_manager'); - $chillAvailableLanguages = $this->getContainer()->getParameter('chill_main.available_languages'); + $em = $this->entityManager; + $chillAvailableLanguages = $this->availableLanguages; $languageBundle = Intl::getLanguageBundle(); $languages = array(); diff --git a/Command/LoadCountriesCommand.php b/Command/LoadCountriesCommand.php index 2062c5667..036ad1de7 100644 --- a/Command/LoadCountriesCommand.php +++ b/Command/LoadCountriesCommand.php @@ -1,7 +1,8 @@ entityManager=$entityManager; + $this->availableLanguages=$availableLanguages; + parent::__construct(); + } + /* * (non-PHPdoc) * @see \Symfony\Component\Console\Command\Command::configure() @@ -31,8 +52,8 @@ class LoadCountriesCommand extends ContainerAwareCommand */ protected function execute(InputInterface $input, OutputInterface $output) { - $countries = static::prepareCountryList($this->getContainer()->getParameter('chill_main.available_languages')); - $em = $this->getContainer()->get('doctrine.orm.entity_manager'); + $countries = static::prepareCountryList($this->availableLanguages); + $em = $this->entityManager; foreach($countries as $country) { $countryStored = $em->getRepository('ChillMainBundle:Country') diff --git a/Command/LoadPostalCodesCommand.php b/Command/LoadPostalCodesCommand.php index 2fa96d32e..ba51db779 100644 --- a/Command/LoadPostalCodesCommand.php +++ b/Command/LoadPostalCodesCommand.php @@ -19,21 +19,48 @@ namespace Chill\MainBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Doctrine\ORM\EntityManager; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Filesystem\Filesystem; use Chill\MainBundle\Entity\PostalCode; +use Symfony\Component\Validator\Validator\ValidatorInterface; /** - * + * Class LoadPostalCodesCommand * + * @package Chill\MainBundle\Command * @author Julien Fastré */ -class LoadPostalCodesCommand extends ContainerAwareCommand +class LoadPostalCodesCommand extends Command { + + /** + * @var EntityManager + */ + private $entityManager; + + /** + * @var ValidatorInterface + */ + private $validator; + + /** + * LoadPostalCodesCommand constructor. + * + * @param EntityManager $entityManager + * @param ValidatorInterface $validator + */ + public function __construct(EntityManager $entityManager, ValidatorInterface $validator) + { + $this->entityManager = $entityManager; + $this->validator = $validator; + parent::__construct(); + } + protected function configure() { $this->setName('chill:main:postal-code:populate') @@ -105,7 +132,7 @@ class LoadPostalCodesCommand extends ContainerAwareCommand $line ++; } - $this->getContainer()->get('doctrine.orm.entity_manager')->flush(); + $this->entityManager->flush(); $output->writeln(''.$num.' were added !'); } @@ -134,7 +161,7 @@ class LoadPostalCodesCommand extends ContainerAwareCommand if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $output->writeln('handling row: '. $row[0].' | '. $row[1].' | '. $row[2]); } - $em = $this->getContainer()->get('doctrine.orm.entity_manager'); + $em = $this->entityManager; $country = $em ->getRepository('ChillMainBundle:Country') ->findOneBy(array('countryCode' => $row[2])); @@ -160,7 +187,7 @@ class LoadPostalCodesCommand extends ContainerAwareCommand ->setCountry($country) ; - $errors = $this->getContainer()->get('validator')->validate($postalCode); + $errors = $this->validator->validate($postalCode); if ($errors->count() == 0) { $em->persist($postalCode); diff --git a/Command/SetPasswordCommand.php b/Command/SetPasswordCommand.php index c6741b524..9e00bd58f 100644 --- a/Command/SetPasswordCommand.php +++ b/Command/SetPasswordCommand.php @@ -20,19 +20,41 @@ namespace Chill\MainBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Doctrine\ORM\EntityManager; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Chill\MainBundle\Entity\User; +use Symfony\Component\Security\Core\Encoder\EncoderFactory; +use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; +use Symfony\Component\Security\Core\Security; /** - * Description of SetPasswordCommand + * Class SetPasswordCommand * + * @package Chill\MainBundle\Command * @author Julien Fastré */ -class SetPasswordCommand extends ContainerAwareCommand +class SetPasswordCommand extends Command { + + /** + * @var EntityManager + */ + private $entityManager; + + /** + * SetPasswordCommand constructor. + * + * @param EntityManager $entityManager + */ + public function __construct(EntityManager $entityManager) + { + $this->entityManager = $entityManager; + parent::__construct(); + } + public function configure() { $this->setName('chill:user:set_password') @@ -64,17 +86,21 @@ class SetPasswordCommand extends ContainerAwareCommand public function _getUser($username) { - $em = $this->getContainer()->get('doctrine.orm.entity_manager'); - return $em->getRepository('ChillMainBundle:User') - ->findOneBy(array('username' => $username)); + return $this->entityManager + ->getRepository('ChillMainBundle:User') + ->findOneBy(array('username' => $username)); } public function _setPassword(User $user, $password) { - $encoder = $this->getContainer()->get('security.encoder_factory') - ->getEncoder($user); - $user->setPassword($encoder->encodePassword($password, $user->getSalt())); - $this->getContainer()->get('doctrine.orm.entity_manager') - ->flush($user); + $defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000); + $encoders = [ + User::class => $defaultEncoder + ]; + $encoderFactory = new EncoderFactory($encoders); + $user->setPassword( + $encoderFactory->getEncoder($user)->encodePassword($password, $user->getSalt()) + ); + $this->entityManager->flush($user); } } diff --git a/Controller/PermissionsGroupController.php b/Controller/PermissionsGroupController.php index 6c1d0e5d3..cf8230e09 100644 --- a/Controller/PermissionsGroupController.php +++ b/Controller/PermissionsGroupController.php @@ -176,6 +176,7 @@ class PermissionsGroupController extends Controller foreach($roleScopes as $roleScope) { /* @var $roleScope RoleScope */ $title = $roleProvider->getRoleTitle($roleScope->getRole()); + var_dump($title); $roleScopesSorted[$title][] = $roleScope; } ksort($roleScopesSorted); diff --git a/config/services/command.yaml b/config/services/command.yaml index d06ce2cc4..223449bfa 100644 --- a/config/services/command.yaml +++ b/config/services/command.yaml @@ -5,11 +5,42 @@ services: $logger: '@Psr\Log\LoggerInterface' $passwordEncoder: '@Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface' $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface' - tags: - { name: console.command } Chill\MainBundle\Command\ChillUserSendRenewPasswordCodeCommand: - autowire: true + arguments: + $logger: '@Psr\Log\LoggerInterface' + $em: '@Doctrine\ORM\EntityManagerInterface' + $recoverPasswordHelper: '@Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper' + $eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' + tags: + - { name: console.command } + + Chill\MainBundle\Command\LoadAndUpdateLanguagesCommand: + arguments: + $entityManager: '@doctrine.orm.entity_manager' + $availableLanguages: '%chill_main.available_languages%' + tags: + - { name: console.command } + + Chill\MainBundle\Command\LoadCountriesCommand: + arguments: + $entityManager: '@doctrine.orm.entity_manager' + $availableLanguages: '%chill_main.available_languages%' + tags: + - { name: console.command } + + + Chill\MainBundle\Command\LoadPostalCodesCommand: + arguments: + $entityManager: '@doctrine.orm.entity_manager' + $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface' + tags: + - { name: console.command } + + Chill\MainBundle\Command\SetPasswordCommand: + arguments: + $entityManager: '@doctrine.orm.entity_manager' tags: - { name: console.command }