Beispiel #1
3
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $schoolId = $input->getOption('schoolId');
     if (!$schoolId) {
         $schoolTitles = [];
         foreach ($this->schoolManager->findBy([], ['title' => 'ASC']) as $school) {
             $schoolTitles[$school->getTitle()] = $school->getId();
         }
         $helper = $this->getHelper('question');
         $question = new ChoiceQuestion("What is this user's primary school?", array_keys($schoolTitles));
         $question->setErrorMessage('School %s is invalid.');
         $schoolTitle = $helper->ask($input, $output, $question);
         $schoolId = $schoolTitles[$schoolTitle];
     }
     $school = $this->schoolManager->findOneBy(['id' => $schoolId]);
     if (!$school) {
         throw new \Exception("School with id {$schoolId} could not be found.");
     }
     $userRecord = ['firstName' => $input->getOption('firstName'), 'lastName' => $input->getOption('lastName'), 'email' => $input->getOption('email'), 'telephoneNumber' => $input->getOption('telephoneNumber'), 'campusId' => $input->getOption('campusId'), 'username' => $input->getOption('username'), 'password' => $input->getOption('password')];
     $userRecord = $this->fillUserRecord($userRecord, $input, $output);
     $user = $this->userManager->findOneBy(['campusId' => $userRecord['campusId']]);
     if ($user) {
         throw new \Exception('User #' . $user->getId() . " with campus id {$userRecord['campusId']} already exists.");
     }
     $user = $this->userManager->findOneBy(['email' => $userRecord['email']]);
     if ($user) {
         throw new \Exception('User #' . $user->getId() . " with email address {$userRecord['email']} already exists.");
     }
     $table = new Table($output);
     $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email', 'Username', 'Phone Number'))->setRows(array([$userRecord['campusId'], $userRecord['firstName'], $userRecord['lastName'], $userRecord['email'], $userRecord['username'], $userRecord['telephoneNumber']]));
     $table->render();
     $helper = $this->getHelper('question');
     $output->writeln('');
     $question = new ConfirmationQuestion("<question>Do you wish to add this user to Ilios in {$school->getTitle()}?</question>\n", true);
     if ($helper->ask($input, $output, $question)) {
         $user = $this->userManager->create();
         $user->setFirstName($userRecord['firstName']);
         $user->setLastName($userRecord['lastName']);
         $user->setEmail($userRecord['email']);
         $user->setCampusId($userRecord['campusId']);
         $user->setAddedViaIlios(true);
         $user->setEnabled(true);
         $user->setSchool($school);
         $user->setUserSyncIgnore(false);
         $this->userManager->update($user);
         $authentication = $this->authenticationManager->create();
         $authentication->setUsername($userRecord['username']);
         $user->setAuthentication($authentication);
         $encodedPassword = $this->encoder->encodePassword($user, $userRecord['password']);
         $authentication->setPasswordBcrypt($encodedPassword);
         $this->authenticationManager->update($authentication);
         $output->writeln('<info>Success! New user #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' created.</info>');
     } else {
         $output->writeln('<comment>Canceled.</comment>');
     }
 }
 private function encodePassword(User $user)
 {
     if (!$user->getRawPassword()) {
         return;
     }
     $password = $this->passwordEncoder->encodePassword($user, $user->getRawPassword());
     $user->setPassword($password);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($object, Constraint $constraint)
 {
     $methodPassword = '******' . ucfirst($constraint->fieldPassword);
     $methodUser = '******' . ucfirst($constraint->fieldUser);
     $user = $object->{$methodUser}();
     $currentPassword = $user->getPassword();
     $encodedPassword = $this->encoder->encodePassword($user, $object->{$methodPassword}());
     if ($currentPassword !== $encodedPassword) {
         $this->context->buildViolation($constraint->message)->addViolation();
     }
 }
Beispiel #4
0
 public function addUser(User $user)
 {
     $user->setSalt();
     $password = $this->passwordEncoder->encodePassword($user, $user->getPassword());
     $user->setPassword($password);
     $roleUser = $this->getUserRole();
     $user->addRole($roleUser);
     $user = $this->userManager->addUser($user);
     $this->setUserApiKey($user);
     return $user;
 }
 /**
  * @param LifecycleEventArgs $event
  */
 public function processPasswordEncodingIfNeeded(LifecycleEventArgs $event)
 {
     $entity = $event->getEntity();
     if (!$entity instanceof AdminUser) {
         return;
     }
     $plainPassword = $entity->getPlainPassword();
     if ($plainPassword === null) {
         return;
     }
     $entity->setPassword($this->passwordEncoder->encodePassword($entity, $plainPassword));
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $userRecords = [['username' => 'raphy', 'password' => 'test']];
     foreach ($userRecords as $userRecord) {
         $user = new User();
         $user->setUsername($userRecord['username']);
         $password = $this->encoder->encodePassword($user, $userRecord['password']);
         $user->setPassword($password);
         $manager->persist($user);
     }
     $manager->flush();
 }
Beispiel #7
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->add('name', TextType::class, ['constraints' => [new NotBlank(['groups' => ['create']])]]);
     $builder->add('email', EmailType::class, ['constraints' => [new NotBlank(['groups' => ['create']])]]);
     $builder->add('plainPassword', PasswordType::class, ['constraints' => [new NotBlank(['groups' => ['create']]), new Length(['max' => 72])], 'required' => false]);
     $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
         /** @var User $user */
         $user = $event->getData();
         if ($user->getPlainPassword() === null) {
             return;
         }
         $password = $this->passwordEncoder->encodePassword($user, $user->getPlainPassword());
         $user->setPassword($password);
     }, 10);
     // Priority before the validation.
 }
Beispiel #8
0
 public function updateUser(User $user, $userData)
 {
     if ($userData['username'] != $user->getUsername()) {
         $user->setUsername($userData['username']);
     }
     if ($userData['firstName'] != $user->getUserFirstName()) {
         $user->setUserFirstName($userData['firstName']);
     }
     if ($userData['lastName'] != $user->getUserLastName()) {
         $user->setUserLastName($userData['lastName']);
     }
     if ($userData['password'] != "") {
         $password = $this->passwordEncoder->encodePassword($user, $userData['password']);
         $user->setPassword($password);
     }
     if ($userData['email'] != $user->getEmail()) {
         $user->setEmail($userData['email']);
     }
     $roles = $this->getRoles();
     $this->assignAppropriateRoles($user, $userData['roleId'], $roles);
     $errors = $this->validator->validate($user, null, array('registration'));
     if (count($errors) > 0) {
         throw new ValidatorException($errors);
     }
     $this->userManager->saveChanges();
     return $user;
 }
Beispiel #9
0
 /**
  * @param $email
  * @param $password
  * @param $firstName
  * @param $lastName
  */
 public function create($email, $password, $firstName, $lastName)
 {
     $role = User::ROLE_ADMIN;
     $em = $this->doctrine->getManager();
     $user = $em->getRepository('AppBundle:User')->findBy(['email' => $email]);
     if (!$user) {
         $user = new User();
     } else {
         $user = $user[0];
     }
     $pass = $this->userPasswordEncoder->encodePassword($user, $password);
     $user->setEmail($email);
     $user->setFirstName($firstName);
     $user->setLastName($lastName);
     $user->setPassword($pass);
     $user->setRole($role);
     $user->setIsActive(true);
     $em->persist($user);
     $em->flush();
 }
Beispiel #10
0
 /**
  * Update users to the new password encoding when they login
  * @param  AuthenticationEntityInterface $authEntity
  * @param  string         $password
  */
 protected function updateLegacyPassword(AuthenticationEntityInterface $authEntity, $password)
 {
     if ($authEntity->isLegacyAccount()) {
         //we have to have a valid token to update the user because the audit log requires it
         $authenticatedToken = new PreAuthenticatedToken($authEntity->getUser(), 'fakekey', 'fakeProvider');
         $authenticatedToken->setAuthenticated(true);
         $this->tokenStorage->setToken($authenticatedToken);
         $authEntity->setPasswordSha256(null);
         $encodedPassword = $this->encoder->encodePassword($authEntity->getUser(), $password);
         $authEntity->setPasswordBcrypt($encodedPassword);
         $this->authManager->updateAuthentication($authEntity);
     }
 }
Beispiel #11
0
 /**
  * @param string                       $plainPassword
  * @param UserPasswordEncoderInterface $encoder
  */
 private function updatePassword(string $plainPassword, UserPasswordEncoderInterface $encoder)
 {
     $this->password = $encoder->encodePassword($this, $plainPassword);
 }
 public function updateUser(User $user)
 {
     $user->setPassword($this->encoder->encodePassword($user, $this->password));
     return $user;
 }
Beispiel #13
0
 public function updateUser(User $user)
 {
     $user->setEmail($this->email);
     $user->setName($this->name);
     $user->setPassword($this->passwordEncoder->encodePassword($user, $this->password));
 }
 public function setNewPassword($password)
 {
     $this->newPassword = $this->passwordEncoder->encodePassword($this->user, $password);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // prevent this command to run on a non-empty user store.
     $existingUser = $this->userManager->findOneBy([]);
     if (!empty($existingUser)) {
         throw new \Exception('Sorry, at least one user record already exists. Cannot create a "first" user account.');
     }
     $schools = $this->schoolManager->findBy([], ['title' => 'ASC']);
     // check if any school data is present before invoking the form helper
     // to prevent the form from breaking on missing school data further downstream.
     if (empty($schools)) {
         throw new \Exception('No schools found. Please load schools into this Ilios instance first.');
     }
     $schoolId = $input->getOption('school');
     if (!$schoolId) {
         $schoolTitles = [];
         /* @var SchoolInterface $school */
         foreach ($schools as $school) {
             $schoolTitles[$school->getTitle()] = $school->getId();
         }
         $helper = $this->getHelper('question');
         $question = new ChoiceQuestion("What is this user's primary school?", array_keys($schoolTitles));
         $question->setErrorMessage('School %s is invalid.');
         $schoolTitle = $helper->ask($input, $output, $question);
         $schoolId = $schoolTitles[$schoolTitle];
     }
     $school = $this->schoolManager->findOneBy(['id' => $schoolId]);
     if (!$school) {
         throw new \Exception("School with id {$schoolId} could not be found.");
     }
     $email = $input->getOption('email');
     if (!$email) {
         $question = new Question("What is the user's Email Address? ");
         $question->setValidator(function ($answer) {
             if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
                 throw new \RuntimeException("Email is not valid");
             }
             return $answer;
         });
         $email = $this->getHelper('question')->ask($input, $output, $question);
     }
     $user = $this->userManager->create();
     $user->setFirstName(self::FIRST_NAME);
     $user->setMiddleName(date('Y-m-d_h.i.s'));
     $user->setLastName(self::LAST_NAME);
     $user->setEmail($email);
     $user->setAddedViaIlios(true);
     $user->setEnabled(true);
     $user->setUserSyncIgnore(false);
     $user->addRole($this->userRoleManager->findOneBy(['title' => 'Developer']));
     $user->addRole($this->userRoleManager->findOneBy(['title' => 'Course Director']));
     $user->setSchool($school);
     $this->userManager->update($user);
     $authentication = $this->authenticationManager->create();
     $authentication->setUser($user);
     $user->setAuthentication($authentication);
     $encodedPassword = $this->passwordEncoder->encodePassword($user, self::PASSWORD);
     $authentication->setUsername(self::USERNAME);
     $authentication->setPasswordBcrypt($encodedPassword);
     $this->authenticationManager->update($authentication);
     $output->writeln('Success!');
     $output->writeln('A user account has been created.');
     $output->writeln(sprintf("You may now log in as '%s' with the password '%s'.", self::USERNAME, self::PASSWORD));
     $output->writeln('Please change this password as soon as possible.');
 }