2021-07-05 00:07:17 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import pdb
|
|
|
|
import re
|
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
import psycopg2.extras
|
|
|
|
|
|
|
|
from flask import current_app
|
2021-07-20 17:32:04 +02:00
|
|
|
from app import db
|
2021-07-05 00:07:17 +02:00
|
|
|
from app.auth.models import User, Role
|
|
|
|
|
|
|
|
|
|
|
|
def import_scodoc7_user_db(scodoc7_db="dbname=SCOUSERS"):
|
|
|
|
"""Create users from existing ScoDoc7 db (SCOUSERS)
|
|
|
|
The resulting users are in SCO8USERS,
|
|
|
|
handled via Flask/SQLAlchemy ORM.
|
|
|
|
"""
|
2021-08-18 19:36:30 +02:00
|
|
|
current_app.logger.info("Importation des utilisateurs...")
|
2021-08-14 18:54:32 +02:00
|
|
|
messages = []
|
2021-07-05 00:07:17 +02:00
|
|
|
cnx = psycopg2.connect(scodoc7_db)
|
|
|
|
cursor = cnx.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
|
|
|
cursor.execute("SELECT * FROM sco_users;")
|
|
|
|
for u7 in cursor:
|
2021-08-22 15:41:39 +02:00
|
|
|
user_name = u7["user_name"].strip().replace(" ", "_")
|
|
|
|
if User.query.filter_by(user_name=user_name).first():
|
2021-07-05 00:07:17 +02:00
|
|
|
# user with same name exists !
|
|
|
|
current_app.logger.warning(
|
2021-08-22 15:41:39 +02:00
|
|
|
f"""User {user_name} (de {u7["user_name"]}) exists and is left unchanged"""
|
2021-07-05 00:07:17 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
u = User(
|
|
|
|
user_name=u7["user_name"],
|
|
|
|
email=u7["email"],
|
|
|
|
date_modif_passwd=u7["date_modif_passwd"],
|
|
|
|
nom=u7["nom"],
|
|
|
|
prenom=u7["prenom"],
|
|
|
|
dept=u7["dept"],
|
|
|
|
passwd_temp=u7["passwd_temp"],
|
|
|
|
date_expiration=u7["date_expiration"],
|
|
|
|
password_scodoc7=u7["passwd"],
|
|
|
|
active=(u7["status"] == None),
|
|
|
|
)
|
|
|
|
# Set roles:
|
|
|
|
# ScoDoc7 roles are stored as 'AdminRT,EnsRT'
|
2021-07-27 16:07:03 +02:00
|
|
|
if u7["roles"]:
|
|
|
|
roles7 = u7["roles"].split(",")
|
|
|
|
else:
|
|
|
|
roles7 = []
|
|
|
|
for role_dept in roles7:
|
2021-08-14 18:54:32 +02:00
|
|
|
# Cas particulier RespPeRT
|
|
|
|
m = re.match(r"^(-?RespPe)([A-Z][A-Za-z0-9]*?)$", role_dept)
|
2021-07-05 00:07:17 +02:00
|
|
|
if not m:
|
2021-08-14 18:54:32 +02:00
|
|
|
# Cas général: eg EnsRT
|
|
|
|
m = re.match(r"^(-?[A-Za-z0-9]+?)([A-Z][A-Za-z0-9]*?)$", role_dept)
|
|
|
|
if not m:
|
|
|
|
msg = (
|
|
|
|
f"User {u7['user_name']}: invalid role '{role_dept}' (ignoring)"
|
2021-07-05 00:07:17 +02:00
|
|
|
)
|
2021-08-14 18:54:32 +02:00
|
|
|
current_app.logger.warning(msg)
|
|
|
|
messages.append(msg)
|
2021-07-05 00:07:17 +02:00
|
|
|
else:
|
|
|
|
role_name = m.group(1)
|
2021-07-27 16:07:03 +02:00
|
|
|
if role_name.startswith("-"):
|
|
|
|
# disabled users in ScoDoc7
|
|
|
|
role_name = role_name[1:]
|
|
|
|
assert not u.active
|
2021-08-14 18:54:32 +02:00
|
|
|
# silently ignore old (disabled) role
|
2021-07-05 00:07:17 +02:00
|
|
|
else:
|
2021-08-14 18:54:32 +02:00
|
|
|
dept = m.group(2)
|
|
|
|
role = Role.query.filter_by(name=role_name).first()
|
|
|
|
if not role:
|
|
|
|
msg = f"User {u7['user_name']}: ignoring role '{role_dept}'"
|
|
|
|
current_app.logger.warning(msg)
|
|
|
|
messages.append(msg)
|
|
|
|
else:
|
|
|
|
u.add_role(role, dept)
|
2021-07-05 00:07:17 +02:00
|
|
|
db.session.add(u)
|
|
|
|
current_app.logger.info("imported user {}".format(u))
|
|
|
|
db.session.commit()
|
2021-08-14 18:54:32 +02:00
|
|
|
return messages
|