22

我在我的应用程序中使用 FOSUserBundle。我想通过 HTTP 服务做两件事:

  1. 检查密码。该服务可能如下所示(密码不会被加密):

    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. 通过另一个 HTTP 服务创建一个新用户。参数将是用户名和密码。

4

3 回答 3

63
  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);
    
于 2012-03-23T14:40:23.267 回答
6

对我来说工作:

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

我没有测试第二个问题,但我认为它已经回答了。

于 2014-09-11T12:27:16.357 回答
1

手动添加新用户:

登录phpmyadmin,访问fos_user_user表,单击插入 > 填写字段、用户名、电子邮件、角色等。

使用这个 php 脚本生成盐和密码:

<?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/>";


?>

享受 !

于 2016-12-28T03:42:05.707 回答