FOSUserBundle:自定义密码/从旧数据库结构迁移 [英] FOSUserBundle: Custom password / Migration from old DB structure

查看:71
本文介绍了FOSUserBundle:自定义密码/从旧数据库结构迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转到Symfony2,因为它的现代性和出色的编程给我留下了深刻的印象.

I want to move to Symfony2, because I am totally impressed by its modernity and good programming.

现在我要从拥有10,000个用户的旧系统中获取一个用户表,并且我不想让他们设置新密码来激怒他们……所以我希望他们能够使用他们的登录名旧密码

Now I am taking a users table from my old system, with 10,000 users, and I don't want to anger them by making them set a new password....so I want them to be able to login with their old password

以下是有关我的用户表的伪代码,其中包含有关登录/注册的3个主要字段:

Here is pseudo-code of how my users table looks like with 3 major fields concerning login/signup:

id, int(10) unsigned NOT NULL
username varchar(40) NOT NULL
passhash varchar(32) NOT NULL
secret varchar(20) NOT NULL

注册时,数据是通过以下方式生成的:

on signup, the data gets generated this way:

$secret = mksecret ();
$passhash = md5 ($secret . $password_formfield . $secret);

登录上,通过以下方式检查数据:

on login, the data gets checked the following way:

if ($row['passhash'] != md5 ($row['secret'] . $password_formfield . $row['secret']))
{
//show login error
}

那么如何在FOSUserBundle中最好地处理它,而不必编辑太多文件?

So how do I handle it best in FOSUserBundle, without having to edit too many files?

推荐答案

您需要创建自定义密码编码器:

You need to create a custom password encoder:

<?php

use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;

class MyPasswordEncoder extends BasePasswordEncoder
{
    public function encodePassword($raw, $salt)
    {
        return md5($salt.$raw.$salt);
    }

    public function isPasswordValid($encoded, $raw, $salt)
    {
        return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
    }
}

并在security.yml中对其进行配置:

services:
    my_password_encoder:
        class: MyPasswordEncoder

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: { id: my_password_encoder }

只要User::getSalt()返回secret,并且User::getPassword()返回passhash,您应该会很好.

As long as User::getSalt() returns secret and User::getPassword() returns passhash you should be good to go.

这篇关于FOSUserBundle:自定义密码/从旧数据库结构迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆