nanterre: ajout de la création d'un fichier d'import pour authentic

Les utilisateurs sont importés depuis la table users provenant de l'export de la
base swarm, sont repris
- id, qui est transformé en uuid en lui préfixant la chaîne "swarnid#"
- username,
- password, le hash bcrypt est repris tel quel en préfixant avec la chaîne
  "bcrypt_sha356$", il reste à vérifier que sha256 est bien appliqué au mot de
  passe préalablement à l'application de bcrypt,
- firstname,
- lastname,
- email,
- last_login (converti dans la timezone Europe/Paris, la donnée RSUv1 étant
  naïve, i.e. sans timezone).

Une fixture Django est produite dans le fichier authentic_users.json dans le
répertoire courant, il pourra être chargé via:

  authentic2-multitenant-ctl tenant_command loaddata -d <hostname> ./authentic2_users.json
This commit is contained in:
Benjamin Dauvergne 2017-04-07 12:57:11 +02:00
parent b756d0bf54
commit 72a4859412
1 changed files with 63 additions and 4 deletions

View File

@ -1,13 +1,50 @@
# -*- coding: utf-8 -*-
import sys
import json
import psycopg2
from django import db
from django.utils.timezone import now, make_aware
from zoo.models import Entity, Relation, EntitySchema, RelationSchema, Transaction
from zoo.zoo_nanterre.utils import UNION_REL, RESPONSABILITE_LEGALE_REL, HABITE_REL
class UserFixture(object):
def __init__(self, path):
self.stream = open(path, 'w')
self.stream.write('[\n')
self.first = True
def write_user(self, swarmid, username, password, first_name, last_name, email, last_login):
uid = 'swarmid#%s' % swarmid
date_joined = now().isoformat()
d = {
'model': 'custom_user.user',
'fields': {
'email': email,
'first_name': first_name,
'last_name': last_name,
'last_login': make_aware(last_login).isoformat(),
'username': username,
'uuid': uid,
'password': 'bcrypt_sha256$' + password,
'modified': date_joined,
'date_joined': date_joined,
}
}
if not self.first:
self.stream.write(',\n')
self.stream.write(json.dumps(d))
self.first = False
return uid
def close(self):
self.stream.write(']')
self.stream.flush()
self.stream.close()
Relation.objects.all().delete()
Entity.objects.all().delete()
@ -22,7 +59,26 @@ cursor = connection.cursor()
tr = Transaction.objects.create(meta="initial import")
cursor.execute('''SELECT id, gender, firstname, lastname, nameofuse, email, phones::json,
user_fixture = UserFixture('authentic_users.json')
cursor.execute("""SELECT id, username, password, firstname, lastname, email, last_login FROM users
WHERE status = 'active'""")
swarmid_mapping = {}
for (swarmid, username, password, first_name, last_name, email, last_login) in cursor.fetchall():
uid = user_fixture.write_user(
swarmid=swarmid,
username=username,
password=password,
first_name=first_name,
last_name=last_name,
email=email,
last_login=last_login)
swarmid_mapping[swarmid] = uid
user_fixture.close()
cursor.execute('''SELECT id, swarmid, gender, firstname, lastname, nameofuse, email, phones::json,
legalstatus, birthdate, mappings::json
FROM individual''')
@ -37,8 +93,8 @@ union_schema = RelationSchema.objects.get(slug=UNION_REL)
individu_mapping = {}
for (individualid, gender, firstname, lastname, nameofuse, email, phones, legalstatus, birthdate,
mappings) in cursor.fetchall():
for (individualid, swarmid, gender, firstname, lastname, nameofuse, email, phones, legalstatus,
birthdate, mappings) in cursor.fetchall():
if gender == 'Female':
genre = 'femme'
elif gender == 'Male':
@ -71,6 +127,9 @@ for (individualid, gender, firstname, lastname, nameofuse, email, phones, legals
raise NotImplementedError('unknown phoneType: %s' % phone['phoneType'])
telephones.append({'type': kind, 'numero': phone['number']})
mappings = mappings or {}
if swarmid in swarmid_mapping:
mappings['authentic'] = swarmid_mapping[swarmid]
content = {
'genre': genre,
'prenoms': firstname.upper(),
@ -78,7 +137,7 @@ for (individualid, gender, firstname, lastname, nameofuse, email, phones, legals
'nom_d_usage': nameofuse.upper(),
'statut_legal': statut_legal,
'date_de_naissance': birthdate.isoformat(),
'cles_de_federation': mappings or {},
'cles_de_federation': mappings,
}
if telephones:
content['telephones'] = telephones