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(' '); } }
/** * {@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(); // } // } // } }
/** * @Route("/users/registration", name="_users_registration") * @Method("POST") * * @ApiDoc( * deprecated=TRUE, * description = "no longer used. Was used to create users with very limited data.", * section="Users", * statusCodes = { * 201 = "User added to the database.", * 403 = "Returned when parameters used for registration are not valid." * }, * parameters = { * {"name" = "active", "dataType"="boolean", "required"=true, "format"="true/false","description"="User is active or disabled."}, * {"name" = "username", "dataType"="text", "required"=true, "description"="username description"}, * {"name" = "lastname", "dataType"="text", "required"=true, "description"="lastname description"}, * {"name" = "firstname", "dataType"="text", "required"=true, "description"="firstname description"}, * {"name" = "email", "dataType"="text", "required"=true, "description"="email description"}, * {"name" = "phone", "dataType"="text", "required"=true, "description"="phone description"}, * {"name" = "title", "dataType"="text", "required"=true, "description"="title description"}, * {"name" = "office", "dataType"="text", "required"=true, "description"="office description"}, * {"name" = "profile_picture", "dataType"="text", "required"=true, "description"="profile_picture description"}, * {"name" = "role_id", "dataType"="text", "required"=true, "description"="role for this user (1 , 2 or 3 )"}, * {"name" = "password", "dataType"="text", "required"=true, "description"="password description"}, * * * } * ) * */ public function postUsersAction(Request $request) { $response = new Response(); $createDate = new \DateTime(); $createDate->setTimezone(self::timezoneUTC()); // Create User instance and set property values: $user = new User(); $user->setEnabled($request->get('active')); $user->setUsername($request->get('username')); $user->setLastname($request->get('lastname')); $user->setFirstname($request->get('firstname')); $user->setEmail($request->get('email')); $user->setPhone($request->get('phone')); $user->setTitle($request->get('title')); $user->setOffice($request->get('office')); $user->setProfilepicture($request->get('profile_picture')); $user->setPassword($request->get('password')); if ($request->get('role_id') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field role_id must have a value!'))); return $response; } $role_id = $request->get('role_id'); $role = $this->getDoctrine()->getRepository('UserBundle:Role')->findOneById($role_id); if (!$role) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'You provided an invalid role id / No role_id provided.'))); return $response; } if ($request->get('active') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field active must have a value!'))); return $response; } if ($request->get('username') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field username must have a value!'))); return $response; } $username_already_exists = $this->getDoctrine()->getRepository('UserBundle:User')->findOneByUsername($request->get('username')); if ($username_already_exists) { $response->setStatusCode(400); $response->setContent(json_encode(array('success' => false, 'message' => 'The username provided is already in use.'))); return $response; } if ($request->get('lastname') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field lastname must have a value!'))); return $response; } if ($request->get('firstname') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field firstname must have a value!'))); return $response; } if ($request->get('email') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field email must have a value!'))); return $response; } if ($request->get('phone') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field phone must have a value!'))); return $response; } if ($request->get('title') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field title must have a value!'))); return $response; } if ($request->get('office') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field office must have a value!'))); return $response; } if ($request->get('profile_picture') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field profile_picture must have a value!'))); return $response; } if ($request->get('password') == null) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Field password must have a value!'))); return $response; } $role_name = $role->getName(); $user->addRole($role_name); $user->setCreatedAt($createDate); $user->setUpdatedAt($createDate); // $response->headers->set('Content-Type', 'application/json'); // $serializer = $this->get('jms_serializer'); // Get validator service to check for errors: $validator = $this->get('validator'); $errors = $validator->validate($user); //// //IN ORDER TO BE A OK RESPONSE < HERE WE MUST RETURN A RESPONSE NOT A VIEW if (count($errors) > 0) { // Return $errors in JSON format: $view = $this->view($errors, 400); return $this->handleView($view); } // End of IF errors check. $user->setPassword(md5($request->request->get('password'))); $key = Uuid::uuid4()->toString(); $user->setApiKey($key); $em = $this->getDoctrine()->getManager(); $em->persist($user); ///SET THE USER TO HAVE USERACCESS TO TEMP_CLIENT $temp_client = $this->getDoctrine()->getRepository('CampaignBundle:Client')->findOneByName('temp_client'); $global_region = $this->getDoctrine()->getRepository('CampaignBundle:Region')->findOneByName('Global'); $temp_useraccess = new Useraccess(); $temp_useraccess->setClient($temp_client); $temp_useraccess->setRegion($global_region); $temp_useraccess->setAllCountries(true); $temp_useraccess->setUser($user); $em->persist($temp_useraccess); //END SET USER FOR USERACCESS $em->flush(); $response->setStatusCode(201); $response->setContent(json_encode(array('success' => true, 'message' => 'User added to the database.'))); return $response; }