Jade Jaber
Jade Jaber

Reputation: 221

Check password and create user manually with FOSUserBundle

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.

Upvotes: 22

Views: 20354

Answers (3)

Alin Razvan
Alin Razvan

Reputation: 1523

Manually add new user :

Login to phpmyadmin , access fos_user_user table , click insert > fill fields , username , email,roles etc.

Generate salt and password using this php script:

<?php 

$salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);

echo "Salt used: " . $salt ."<br/>";

echo "<br/>";

$password = 'adminpasswordhere';
$salted = $password.'{'.$salt.'}';
$digest = hash('sha512', $salted, true);

for ($i=1; $i<5000; $i++) {
    $digest = hash('sha512', $digest.$salted, true);
}

$encodedPassword = base64_encode($digest);

echo "Password used: " . $password ."<br/>";

echo "<br/>";

echo "Encrypted Password: " . $encodedPassword ."<br/>";


?>

Enjoy !

Upvotes: 1

Tomas
Tomas

Reputation: 1694

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.

Upvotes: 6

user1288434
user1288434

Reputation: 730

  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);
    

Upvotes: 63

Related Questions