Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check password and create user manually with FOSUserBundle

Tags:

I'm using FOSUserBundle in my application. I would like to do two things via HTTP services:

  1. Check password. The service could look like this (the password wouldn't be encrypted):

    public function checkPasswordValidity($userId, $password) {     $user = $this->getDoctrine()         ->getRepository('MyCompany\UserBundle\Entity\User')         ->find($userId);      if (specialFunction($user , $password))         echo 'Valid Password';     else         echo 'Invalid Password'; } 
  2. Create a new user via another HTTP service. The parameters would be the username and the password.

like image 220
Jade Jaber Avatar asked Feb 28 '12 09:02

Jade Jaber


Video Answer


2 Answers

  1. Check password:

    $encoder_service = $this->get('security.encoder_factory'); $encoder = $encoder_service->getEncoder($user); $encoded_pass = $encoder->encodePassword($password, $user->getSalt());  //then compare    $user->getPassword() and $encoded_pass 
  2. Create a new user:

    $userManager = $this->get('fos_user.user_manager'); $user = $userManager->createUser(); $user->setUsername($login); $user->setPlainPassword($pass); ... $userManager->updateUser($user); 
like image 76
user1288434 Avatar answered Sep 20 '22 04:09

user1288434


For me works:

$encoder_service = $this->get('security.encoder_factory'); $encoder = $encoder_service->getEncoder($user); if ($encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt()) {} 

I did not test second question, but I think it is answered already.

like image 31
Tomas Avatar answered Sep 24 '22 04:09

Tomas