private function encodePassword(User $user)
 {
     $plainPassword = $user->getPlainPassword();
     if (!is_null($plainPassword)) {
         $user->setPassword($this->passwordEncoder->encodePassword($user, $plainPassword));
     }
 }
Example #2
0
 /**
  * @param \Symfony\Component\Form\Form $form
  * @param \AppBundle\Entity\User $user
  * @return bool
  */
 public function createUser(Form $form, User $user) : bool
 {
     $return = false;
     if (!$this->checkUsername($user->getUsername())) {
         $return = true;
         $form->get('username')->addError(new FormError($this->translator->trans('users.registration.username_already_taken')));
     }
     if (!$this->checkEmail($user->getEmail())) {
         $return = true;
         $form->get('email')->addError(new FormError($this->translator->trans('users.registration.email_already_taken')));
     }
     if ($return) {
         return false;
     }
     $user->setSalt(uniqid('', true));
     $password = $this->encoder->encodePassword($user, $user->getPlainPassword());
     $user->setPassword($password);
     $user->addRole('ROLE_USER');
     $user->enable(false);
     $this->em->persist($user);
     $this->em->flush();
     $this->activationLinkManager->createActivationLink($user);
     $this->activationLinkManager->sendValidationMail($user);
     return true;
 }
 public function testIsPasswordValid()
 {
     $userMock = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\User\\UserInterface')->getMock();
     $userMock->expects($this->any())->method('getSalt')->will($this->returnValue('userSalt'));
     $userMock->expects($this->any())->method('getPassword')->will($this->returnValue('encodedPassword'));
     $mockEncoder = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface')->getMock();
     $mockEncoder->expects($this->any())->method('isPasswordValid')->with($this->equalTo('encodedPassword'), $this->equalTo('plainPassword'), $this->equalTo('userSalt'))->will($this->returnValue(true));
     $mockEncoderFactory = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface')->getMock();
     $mockEncoderFactory->expects($this->any())->method('getEncoder')->with($this->equalTo($userMock))->will($this->returnValue($mockEncoder));
     $passwordEncoder = new UserPasswordEncoder($mockEncoderFactory);
     $isValid = $passwordEncoder->isPasswordValid($userMock, 'plainPassword');
     $this->assertTrue($isValid);
 }
Example #4
0
 function it_can_register(UserPasswordEncoder $encoder)
 {
     $encoder->encodePassword(Argument::type('HMLB\\UserBundle\\User\\User'), '123456')->shouldBeCalled();
     $encoder->encodePassword(Argument::type('HMLB\\UserBundle\\User\\User'), '123456')->willReturn('654321');
     $this->beConstructedThrough('register', ['test', '*****@*****.**', '123456', $encoder, ['ROLE_USER']]);
     $this->shouldHaveType('HMLB\\UserBundle\\User\\User');
     $this->shouldHaveType('Symfony\\Component\\Security\\Core\\User\\AdvancedUserInterface');
     $this->getUsername()->shouldBe('test');
     $this->getEmail()->shouldBe('*****@*****.**');
     $this->getPassword()->shouldBe('654321');
     $this->recordedMessages()->shouldHaveCount(1);
     $this->getRoles()->shouldHaveCount(1);
     $this->recordedMessages()[0]->shouldHaveType('HMLB\\UserBundle\\Event\\UserRegistered');
     $role = $this->getRoles()[0];
     $role->shouldBe('ROLE_USER');
 }
Example #5
0
 /**
  * @param UserInterface $user
  * @param bool $flush
  * @return UserInterface
  */
 private function encodePassword(AbstractUser $user, $flush = false)
 {
     $plainPassword = $user->getPlainPassword();
     $user->setSalt(uniqid(mt_rand(), true));
     $user->setPassword($this->encoder->encodePassword($user, $plainPassword))->eraseCredentials();
     if ($flush) {
         $this->em->flush();
     }
     return $user;
 }
Example #6
0
 public function updateRegistrationUser(Request $request, User $user)
 {
     $em = $this->doctrine->getManager();
     $form = $this->formFactory->create(UpdateUserSocialNetType::class, $user);
     $originalPassword = $user->getPassword();
     $form->handleRequest($request);
     if ($form->isValid()) {
         if (!empty($user->getPlainPassword())) {
             $password = $this->passwordEncoder->encodePassword($user, $user->getPlainPassword());
             $user->setPassword($password);
         } else {
             $user->setPassword($originalPassword);
         }
         $user->setIsActive(true);
         $em->flush();
         return new RedirectResponse($this->router->generate('choice_modules'));
     }
     return ['form' => $form->createView()];
 }
Example #7
0
 /**
  * @param UserResponseInterface $response
  *
  * @return \FOS\UserBundle\Model\UserInterface|UserInterface
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $userInfo = $this->parseResponse($response);
     $user = $this->userManager->findUserBy(['communityId' => $userInfo['id']]);
     if ($user === null) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         /**
          * @var $user User
          */
         $user = $this->userManager->createUser();
         $user->{$setter_id}($userInfo['username']);
         $user->{$setter_token}($response->getAccessToken());
         $user->setUsername($userInfo['username']);
         $user->setEmail($userInfo['email']);
         $password = StringUtils::generateRandomString();
         $user->setPlainPassword($password);
         $password = $this->passwordEncoder->encodePassword($user, $user->getPlainPassword());
         $user->setPassword($password);
         $user->setEnabled(true);
         $user->setCommunityId($userInfo['id']);
         $user = $this->setGroups($user, $userInfo['groups']);
         $this->userManager->updateUser($user);
         if ($this->request->cookies->has(LoginRequestManager::KEY_COOKIE)) {
             $this->loginRequestManager->assignUserToKey($this->request->cookies->get(LoginRequestManager::KEY_COOKIE), $user);
         }
         return $user;
     } else {
         $user->setUsername($userInfo['username']);
         $user->setEmail($userInfo['email']);
         $user = $this->setGroups($user, $userInfo['groups']);
         if ($this->request->cookies->has(LoginRequestManager::KEY_COOKIE)) {
             $this->loginRequestManager->assignUserToKey($this->request->cookies->get(LoginRequestManager::KEY_COOKIE), $user);
         }
     }
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /** 
  * Hash the users password
  * @param User $user
  */
 public function hashUserPassword(User $user)
 {
    $hashedPassword =  $this->encoder->encodePassword($user, $user->getPassword());
    $user->setPassword($hashedPassword);
 }
 /**
  * @param UserEntity $user
  * @param string     $newPassword
  *
  * @return UserEntity
  */
 public function change(UserEntity $user, $newPassword)
 {
     $password = $this->userPasswordEncoder->encodePassword($user, $newPassword);
     $user->setPassword($password);
     return $user;
 }
 private function encodePassword(User $user)
 {
     if ($user->plainPassword) {
         $user->password = $this->encoder->encodePassword($user, $user->plainPassword);
     }
 }
Example #11
0
 /**
  * set and Encode Password.
  * The Password Encoder must be initialized!
  *
  * @param mixed $password
  *
  * @return $this
  * @throws \Excpetion
  */
 public function setPassword($password)
 {
     if ($this->Encoder === null) {
         throw new \Excpetion('Password encoder not set!');
     }
     if ($password !== null) {
         $this->password = $this->Encoder->encodePassword($this, $password);
     }
     return $this;
 }