diff --git a/Command/ChillImportUsersCommand.php b/Command/ChillImportUsersCommand.php index db05cc250..38da1daf4 100644 --- a/Command/ChillImportUsersCommand.php +++ b/Command/ChillImportUsersCommand.php @@ -19,6 +19,7 @@ use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\PermissionsGroup; use Symfony\Component\Console\Question\ChoiceQuestion; +use League\Csv\Writer; class ChillImportUsersCommand extends ContainerAwareCommand { @@ -92,6 +93,12 @@ class ChillImportUsersCommand extends ContainerAwareCommand */ protected $groupCenters = []; + /** + * + * @var Writer + */ + protected $output = null; + public function __construct( EntityManagerInterface $em, LoggerInterface $logger, @@ -118,7 +125,8 @@ class ChillImportUsersCommand extends ContainerAwareCommand ->setHelp("Import users from a csv file. Users are added to centers contained in the file. Headers are used to detect columns. Adding to multiple centers can be done by using a `grouping centers` file, which will group multiple centers into a signle alias, used in 'centers' column.") ->addArgument('csvfile', InputArgument::REQUIRED, 'Path to the csv file. Columns are: `username`, `email`, `center` (can contain alias), `permission group`') ->addOption('grouping-centers', null, InputOption::VALUE_OPTIONAL, 'Path to a csv file to aggregate multiple centers into a single alias') - ->addOption('dry-run', null, InputOption::VALUE_OPTIONAL, 'Do not commit the changes') + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Do not commit the changes') + ->addOption('csv-dump', null, InputOption::VALUE_REQUIRED, 'A path to dump a summary of the created file') ; } @@ -127,15 +135,43 @@ class ChillImportUsersCommand extends ContainerAwareCommand $this->tempOutput = $output; $this->tempInput = $input; - if ($input->hasOption('dry-run')) { + if ($input->getOption('dry-run')) { $this->doChanges = false; } + + $this->prepareWriter(); if ($input->hasOption('grouping-centers')) { $this->prepareGroupingCenters(); } - $this->loadUsers(); + try { + $this->loadUsers(); + } + catch(\Exception $e) { + throw $e; + } + } + + protected function prepareWriter() + { + $this->output = $output = Writer::createFromPath($this->tempInput + ->getOption('csv-dump'), 'a+'); + + $output->insertOne([ + 'email', + 'username', + 'id' + ]); + } + + protected function appendUserToFile(User $user) + { + $this->output->insertOne( [ + $user->getEmail(), + $user->getUsername(), + $user->getId() + ]); } protected function loadUsers() @@ -150,7 +186,7 @@ class ChillImportUsersCommand extends ContainerAwareCommand if ($this->doesUserExists($r)) { $this->tempOutput->writeln(sprintf("User with username '%s' already " - . "exists, skipping")); + . "exists, skipping", $r["username"])); $this->logger->info("One user already exists, skipping creation", [ 'username_in_file' => $r['username'], @@ -161,7 +197,8 @@ class ChillImportUsersCommand extends ContainerAwareCommand continue; } - $this->createUser($line, $r); + $user = $this->createUser($line, $r); + $this->appendUserToFile($user); } } @@ -182,8 +219,8 @@ class ChillImportUsersCommand extends ContainerAwareCommand { $user = new User(); $user - ->setEmail($data['email']) - ->setUsername($data['username']) + ->setEmail(\trim($data['email'])) + ->setUsername(\trim($data['username'])) ->setEnabled(true) ->setPassword($this->passwordEncoder->encodePassword($user, \bin2hex(\random_bytes(32)))) @@ -206,14 +243,18 @@ class ChillImportUsersCommand extends ContainerAwareCommand foreach($pgs as $pg) { foreach ($centers as $center) { - $user->addGroupCenter($this->createOrGetGroupCenter($center, $pg)); + $groupcenter = $this->createOrGetGroupCenter($center, $pg); + + if (FALSE === $user->getGroupCenters()->contains($groupcenter)) { + $user->addGroupCenter($groupcenter); + } } } if ($this->doChanges) { $this->em->persist($user); - $this->flush(); + $this->em->flush(); } $this->logger->notice("Create user", [ @@ -222,6 +263,7 @@ class ChillImportUsersCommand extends ContainerAwareCommand 'nb_of_groupCenters' => $user->getGroupCenters()->count() ]); + return $user; } protected function getPermissionGroup($alias) @@ -343,6 +385,9 @@ class ChillImportUsersCommand extends ContainerAwareCommand */ protected function getCenters($name) { + // sanitize + $name = \trim($name); + if (\array_key_exists($name, $this->centers)) { return $this->centers[$name]; }