Your IP : 216.73.217.117


Current Path : /var/www/v3.cesa.co.za/src/UserBundle/Command/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/UserBundle/Command/UserListSyncCommand.php

<?php

namespace App\UserBundle\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use App\UserBundle\Entity\UserList;

/**
 * SF Guard User Command
 *
 * @author Otto Saayman <otto@igotafrica.com>
 * @package UserBundle
 * @subpackage Controller
 * @version 0.0.1
 */
#[AsCommand(name: 'iga:user:sync')]
class UserListSyncCommand extends Command {

    protected static $defaultDescription = 'Synchronise users with old users table';

    protected function configure() {
        $this
                ->setName('iga:user:sync')
                ->setDescription('Synchronise users with old users table')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output) {

        try {

            $oldUsers = $this->getApplication()->getKernel()->getContainer()->get('user_list_old.manager')->findAll();

            foreach ($oldUsers as $oldUser) {

                $user = $this->getApplication()->getKernel()->getContainer()->get('user_list.manager')->findOneBy(array('username' => $oldUser->getUsername()));

                if (!is_object($user)) {
                    $user = new UserList();
                }

                $user->setUsername($oldUser->getUsername());
                $user->setPlainPassword($oldUser->getPassword());
                $user->setIsSuperAdmin(false);
                $user->setFirstNameString($oldUser->getUsername());
                $user->setIsActive($oldUser->isActivated());
                $user->setEMailAddressString($oldUser->getEmailAddress());
                $user->setEMailAddressSubscribed(true);
                $user->setDeleted(false);
                $this->getApplication()->getKernel()->getContainer()->get('user_list.manager')->updatePassword($user);

                if (!is_null($oldUser->getAccessLevel()) && $oldUser->getAccessLevel() < 0) {
                    //Internal staff
                    $user->setIsSuperAdmin(true);
                }

                $this->getApplication()->getKernel()->getContainer()->get('user_list.manager')->save($user);

                $output->writeln(sprintf('Set %s user with password %s', $oldUser->getUsername(), $oldUser->getPassword()));
            } // foreach
        } catch (\Throwable $ex) {
            $output->writeln($ex->getMessage());
            $output->writeln($ex->getTraceAsString());
        }

        return Command::SUCCESS;
    }
}