Migrate passwords from Drupal 7 to Django

前端 未结 4 1015
南方客
南方客 2021-02-08 10:36

I am migrating a site from Drupal 7 to Django 1.4, including the current users. How can I work with the passwords that were hashed by Drupal?

According to this, Drupal 7

4条回答
  •  温柔的废话
    2021-02-08 10:52

    Here's an update to David's excellent answer for python 3, since hashlib no longer accepts strings. Also, this includes support for the odd "U$S$*" hashes, which apparently are from an update and I found a bunch of them in my drupal database.

    https://gist.github.com/skulegirl/bec420b5272b87d9e4dbd39e947062fc

    And as a bonus, here's the code that I used to import my xml file of user data in. (I just created the xml file via an sql export after doing a query on the users table.)

    import xml.etree.ElementTree as ET
    from django.contrib.auth.models import User
    
    tree = ET.parse('/PATH/TO/Users.xml')
    root = tree.getroot()
    
    for row in root:
        user_dict = {}
        for field in row:
            user_dict[field.attrib['name']] = field.text
        user = User.objects.create_user(user_dict['name'], user_dict['mail'])
        if user_dict['pass'][0] == '$':
            user_dict['pass'] = user_dict['pass'][1:]
        user.password = user_dict['pass']
        user.save()
    

提交回复
热议问题