protected function injectUser() { $this->user = new User(); //$admin = 'tester'; // MWAHAHAHA //$departmentId = 5; // UiB $role = $this->em->getRepository('AppBundle:Role')->findOneByRole('ROLE_USER'); $this->user->addRole($role); $this->user->setGender(1); $this->user->setFirstName("Shi"); $this->user->setLastName("LaLong"); $emd = $this->em->getRepository('AppBundle:FieldOfStudy')->find(40); // Tuple "Andre" from UiB, id 40 for fos $this->user->setFieldOfStudy($emd); $this->user->setUserName("shiluib"); // Weird typo? $this->user->setEmail("*****@*****.**"); $this->user->setPhone("47661674"); $this->user->setPicturePath("images/defaultProfile.png"); $this->user->setPassword("pimwrneil"); $createNewUserCode = bin2hex(openssl_random_pseudo_bytes(16)); $hashedNewUserCode = hash('sha512', $createNewUserCode, false); $this->user->setNewUserCode($hashedNewUserCode); // Persist the user $this->user->setIsActive(1); $this->em->persist($this->user); $this->em->flush(); return $this; }
/** * Creates an unactivated user for the given application. * This method is intended to be called by an Ajax request. * TODO: FIll in description * * @param $id * @return JsonResponse */ public function createUnactivatedUserAction($id) { try { $em = $this->getDoctrine()->getManager(); $application = $em->getRepository('AppBundle:Application')->findApplicantById($id); $role = $em->getRepository('AppBundle:Role')->findOneByName(AdmissionAdminController::NEW_USER_ROLE); // Create the hash $createNewUserCode = bin2hex(openssl_random_pseudo_bytes(16)); $hashedNewUserCode = hash('sha512', $createNewUserCode, false); // Copy information from the given application to a new user $user = new User(); $user->setLastName($application->getLastName()); $user->setFirstName($application->getFirstName()); $user->setGender($application->getStatistic()->getGender()); $user->setPhone($application->getPhone()); $user->setFieldOfStudy($application->getStatistic()->getFieldOfStudy()); $user->setEmail($application->getEmail()); // Create Username from email, and make sure it's unique $new_username = explode("@", $application->getEmail())[0]; $user_rep = $em->getRepository('AppBundle:User'); $violator = $user_rep->findOneBy(array('user_name' => $new_username)); $postfix = 0; while ($violator) { $postfix++; $violator = $user_rep->findOneBy(array('user_name' => $new_username . $postfix)); } if ($postfix) { $new_username = $new_username . $postfix; } $user->setUserName($new_username); $user->setPassword($new_username); $user->setIsActive('0'); $user->setNewUserCode($hashedNewUserCode); // Give the new user the default role $user->addRole($role); // Update the application $application->setUserCreated(true); // Update application statistic $application->getStatistic()->setAccepted(true); // Persist $em->persist($application); $em->persist($user); $em->flush(); //Sends a email with the url for resetting the password //echo('127.0.0.1:8000/opprettbruker/'.$createNewUserCode.''); $this->sendNewUserEmail($createNewUserCode, $user->getEmail()); return new JsonResponse(['success' => true]); } catch (\Exception $e) { // If it is a integrity violation constraint (i.e a user with the email already exists) if ($e->getPrevious()) { //If the error occurred when sending email, $e->getPrevious() will be null if ($e->getPrevious()->getCode() == 23000) { $message = 'En bruker med denne E-posten eksisterer allerede.'; } } else { $message = 'En feil oppstod. Kontakt IT ansvarlig.'; } return new JsonResponse(['success' => false, 'cause' => $message]); } }
/** * Sets newUserCode and sends activation email to user. * * @param User user */ public function sendActivationEmail(User $user) { $em = $this->getDoctrine()->getManager(); $createNewUserCode = bin2hex(openssl_random_pseudo_bytes(16)); $hashedNewUserCode = hash('sha512', $createNewUserCode, false); $user->setNewUserCode($hashedNewUserCode); $em->persist($user); $em->flush(); $emailMessage = \Swift_Message::newInstance()->setSubject('Velkommen til vektorprogrammet')->setFrom($this->container->getParameter('no_reply_email_user_creation'))->setTo($user->getEmail())->setBody($this->renderView('new_user/create_new_user_email.txt.twig', array('newUserURL' => $this->generateURL('useradmin_activate_user', array('newUserCode' => $createNewUserCode), true)))); $this->get('mailer')->send($emailMessage); }
public function testSetNewUserCode() { // new entity $user = new User(); // Use the setNewUserCode method $user->setNewUserCode("secret"); // Assert the result $this->assertEquals("secret", $user->getNewUserCode()); }