Пример #1
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $creationDate = new \DateTime();
     $creationDate->setTimezone(self::timezoneUTC());
     $arrays_of_user_data = array(array('qa_user', 'password', 'Quality', 'Assurance User', '*****@*****.**', 'ROLE_ADMINISTRATOR', '8f45fa58-4664-b11b-82ee-7a737df7afb7'), array('admin', 'admin123', 'Admin', 'Admin123', '*****@*****.**', 'ROLE_ADMINISTRATOR', '12345678-4664-b11b-82ee-7a737df7afb7'));
     $count = 0;
     foreach ($arrays_of_user_data as $array_of_user_data) {
         //Generate an aPI KEY//
         $count += 1;
         $rolename = $array_of_user_data[5];
         $role = $manager->getRepository('UserBundle:Role')->findOneByName($rolename);
         // $api_key = Uuid::uuid4()->toString();
         $originalUser = new User();
         $originalUser->setCreatedAt($creationDate);
         $originalUser->setUpdatedAt($creationDate);
         $originalUser->setEnabled(1);
         $originalUser->setLocked(0);
         $originalUser->setExpired(0);
         $originalUser->setCredentialsExpired(0);
         $originalUser->setOffice('New York');
         $originalUser->setTitle('noTitleInformationyet');
         $originalUser->setConfirmationToken('GeneratedOriginalUser');
         $originalUser->addRole($role->getName());
         $originalUser->setUsername($array_of_user_data[0]);
         $originalUser->setEmail($array_of_user_data[4]);
         $originalUser->setUsernameCanonical($array_of_user_data[0]);
         $originalUser->setEmailCanonical($array_of_user_data[4]);
         $originalUser->setPassword(md5($array_of_user_data[1]));
         $originalUser->setApiKey($array_of_user_data[6]);
         $originalUser->setFirstname($array_of_user_data[2]);
         $originalUser->setLastname($array_of_user_data[3]);
         $originalUser->setPhone('2126057000');
         $manager->persist($originalUser);
         //    THIS MUST BE DISABLED.
         //            //ADD USERACCESS TO TEMP CLIENT FOR EACH OF THIS USERS
         //
         //            $temp_client = $manager->getRepository('CampaignBundle:Client')->findOneByName('temp_client');
         //            $global_region = $manager->getRepository('CampaignBundle:Region')->find(1);
         //            $temp_useraccess = new Useraccess();
         //            $temp_useraccess->setUser($originalUser);
         //            $temp_useraccess->setClient($temp_client);
         //            $temp_useraccess->setRegion($global_region);
         //            $temp_useraccess->setAllCountries(true);
         //            $manager->persist($temp_useraccess);
         //END ADD USERACCESS
     }
     echo 'Loaded ' . $count . ' Original Users Successfully.';
     $manager->flush();
     //Set all the default users to have the "created by" set as John Gladysz's Username.
     //        $john = $manager->getRepository('UserBundle:User')->findOneByUsername('jgladysz');
     //        if ($john) {
     //            foreach ($arrays_of_user_data as $userdata) {
     //
     //                $user = $manager->getRepository('UserBundle:User')->findOneByUsername($userdata[0]);
     //                if ($user) {
     //                    $user->setCreatedBy($john);
     //                    $manager->persist($user);
     //                    $manager->flush();
     //                }
     //            }
     //        }
 }
Пример #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $manager = $this->getContainer()->get('doctrine')->getManager();
     $creationDate = new \DateTime();
     $creationDate->setTimezone(new \DateTimeZone('UTC'));
     if ($input->getOption('add')) {
         $existing_users = $manager->getRepository('UserBundle:User')->findByConfirmationToken('deletable');
         if (count($existing_users) > 0) {
             $output->writeln('Found test users in database. Will delete ' . count($existing_users) . ' test users from database.');
             sleep(1);
             foreach ($existing_users as $user) {
                 if ($user->hasRole('ROLE_ADMINISTRATOR')) {
                     $client = $manager->getRepository('CampaignBundle:Client')->find(1);
                     $region = $manager->getRepository('CampaignBundle:Region')->find(1);
                     $useraccess = $manager->getRepository('CampaignBundle:Useraccess')->findOneBy(['user' => $user, 'client' => $client, 'region' => $region]);
                     if ($useraccess) {
                         $manager->remove($useraccess);
                     }
                 }
                 $manager->remove($user);
                 $temp_client = $manager->getRepository('CampaignBundle:Client')->find(2);
                 $temp_useraccess = $manager->getRepository('CampaignBundle:Useraccess')->findOneBy(['user' => $user, 'client' => $temp_client]);
                 $manager->remove($temp_useraccess);
             }
             $manager->flush();
             sleep(1);
             $output->writeln('Succesfully deleted ' . count($existing_users) . ' test users from database.');
         }
         $number = $input->getArgument('number');
         for ($i = 1; $i <= $number; $i++) {
             $user = new User();
             $role = 'ROLE_VIEWER';
             if ($i % 4 == 0) {
                 $role = 'ROLE_CONTRIBUTOR';
             }
             if ($i % 20 == 0) {
                 $role = 'ROLE_ADMINISTRATOR';
             }
             $user->setCreatedAt($creationDate);
             $user->setUpdatedAt($creationDate);
             $user->setUsername('testuser' . $i);
             $user->setEmail('testuser' . $i . '*****@*****.**');
             $user->setUsernameCanonical('testuser' . $i);
             $user->setEmailCanonical('testuser' . $i . '*****@*****.**');
             $user->setEnabled(1);
             $user->setPassword(md5('password' . $i));
             $user->setLocked(0);
             $user->addRole($role);
             $user->setExpired(0);
             $user->setConfirmationToken('deletable');
             $user->setCredentialsExpired(0);
             $user->setApiKey($i);
             $user->setFirstname('testuser' . $i . '_FN');
             $user->setLastname('testuser' . $i . '_LN');
             $user->setPhone('01234567890');
             $user->setOffice('testuser' . $i);
             $user->setTitle('testuser' . $i);
             $manager->persist($user);
             if ($i % 20 == 0) {
                 $all_clients = $manager->getRepository('CampaignBundle:Client')->find(1);
                 $global_region = $manager->getRepository('CampaignBundle:Region')->find(1);
                 $useraccess = new Useraccess();
                 $useraccess->setUser($user);
                 $useraccess->setClient($all_clients);
                 $useraccess->setRegion($global_region);
                 $useraccess->setAllCountries(true);
                 $manager->persist($useraccess);
             }
             //ADD USERACCESS TO TEMP CLIENT REGARDLESS OF THE USER ROLE
             $temp_client = $manager->getRepository('CampaignBundle:Client')->findOneByName('temp_client');
             $global_region = $manager->getRepository('CampaignBundle:Region')->find(1);
             $temp_useraccess = new Useraccess();
             $temp_useraccess->setUser($user);
             $temp_useraccess->setClient($temp_client);
             $temp_useraccess->setRegion($global_region);
             $temp_useraccess->setAllCountries(true);
             $manager->persist($temp_useraccess);
             //
             $manager->flush();
             $output->writeln('Created user ' . $i . ' role : ' . $role . '     Userid:' . $user->getId());
         }
     } elseif ($input->getOption('remove')) {
         $existing_users = $manager->getRepository('UserBundle:User')->findByConfirmationToken('deletable');
         $count = 0;
         foreach ($existing_users as $user) {
             if ($user->hasRole('ROLE_ADMINISTRATOR')) {
                 $client = $manager->getRepository('CampaignBundle:Client')->find(1);
                 $region = $manager->getRepository('CampaignBundle:Region')->find(1);
                 $useraccess = $manager->getRepository('CampaignBundle:Useraccess')->findOneBy(['user' => $user, 'client' => $client, 'region' => $region]);
                 if ($useraccess) {
                     $manager->remove($useraccess);
                 }
             }
             $temp_client = $manager->getRepository('CampaignBundle:Client')->find(2);
             $temp_useraccess = $manager->getRepository('CampaignBundle:Useraccess')->findOneBy(['user' => $user, 'client' => $temp_client]);
             $manager->remove($temp_useraccess);
             $manager->remove($user);
             $count += 1;
         }
         $manager->flush();
         $output->writeln(' ');
         $output->writeln('Removed ' . $count . ' test users.');
         $output->writeln(' ');
     } else {
         $number = $input->getArgument('number');
         $output->writeln(' ');
         $output->writeln(' ');
         $output->writeln('You can use --add / --remove options to change what the command should do.The command is for testing purposes only.');
         $output->writeln(' ');
         $output->writeln(' ');
     }
 }