2017-10-22 10:26:37 +02:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
from authentic2.a2_rbac.models import OrganizationalUnit
|
|
|
|
from authentic2.a2_rbac.utils import get_default_ou
|
|
|
|
from authentic2_auth_oidc.models import OIDCProvider, OIDCAccount
|
|
|
|
|
|
|
|
ou_mapping = {
|
|
|
|
'gl-guichet-numerique': get_default_ou(),
|
2017-10-22 11:06:04 +02:00
|
|
|
'bron': OrganizationalUnit.objects.get(slug='hobo-bron'),
|
|
|
|
'dardilly': OrganizationalUnit.objects.get(slug='hobo-dardilly'),
|
|
|
|
'oullins': OrganizationalUnit.objects.get(slug='hobo-oullins'),
|
|
|
|
'vaulx-en-velin': OrganizationalUnit.objects.get(slug='hobo-vaulx-en-velin'),
|
2017-10-22 10:26:37 +02:00
|
|
|
#'villeurbanne': OrganizationalUnit.objects.get(slug='hobo-villeurbanne'),
|
2017-12-01 12:39:36 +01:00
|
|
|
|
|
|
|
# new names
|
2017-12-01 14:02:22 +01:00
|
|
|
'gnm': get_default_ou(),
|
2017-12-01 12:39:36 +01:00
|
|
|
'gnm-bron': OrganizationalUnit.objects.get(slug='hobo-bron'),
|
|
|
|
'gnm-dardilly': OrganizationalUnit.objects.get(slug='hobo-dardilly'),
|
|
|
|
'gnm-oullins': OrganizationalUnit.objects.get(slug='hobo-oullins'),
|
|
|
|
'gnm-vaulx-en-velin': OrganizationalUnit.objects.get(slug='hobo-vaulx-en-velin'),
|
2017-12-04 16:48:21 +01:00
|
|
|
|
|
|
|
# new new names
|
|
|
|
'gl-guichet-numerique-bron': OrganizationalUnit.objects.get(slug='hobo-bron'),
|
|
|
|
'gl-guichet-numerique-dardilly': OrganizationalUnit.objects.get(slug='hobo-dardilly'),
|
|
|
|
'gl-guichet-numerique-oullins': OrganizationalUnit.objects.get(slug='hobo-oullins'),
|
|
|
|
'gl-guichet-numerique-vaulx-en-velin': OrganizationalUnit.objects.get(slug='hobo-vaulx-en-velin'),
|
2018-02-23 17:20:03 +01:00
|
|
|
|
|
|
|
'territoire': get_default_ou(),
|
2017-10-22 10:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cut_agents = OIDCProvider.objects.get(name='cut-agents')
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
for ou_slug, ou in ou_mapping.items():
|
2017-12-01 12:41:18 +01:00
|
|
|
url = 'https://admin-cut-rec.grandlyon.com/api/users/?ou__slug=%s' % ou_slug
|
2017-10-22 10:26:37 +02:00
|
|
|
for cut_user_data in requests.get(url, auth=settings.CUT_API_CREDENTIALS).json()['results']:
|
|
|
|
try:
|
|
|
|
user = User.objects.get(oidc_account__provider=cut_agents,
|
2017-10-27 12:37:44 +02:00
|
|
|
oidc_account__sub=cut_user_data['sub'])
|
2017-10-22 10:26:37 +02:00
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
user = User.objects.get(email=cut_user_data['email'])
|
|
|
|
except User.DoesNotExist:
|
|
|
|
user = User(first_name=cut_user_data['first_name'],
|
|
|
|
last_name=cut_user_data['last_name'],
|
|
|
|
email=cut_user_data['email'],
|
2017-10-22 13:41:29 +02:00
|
|
|
ou=ou)
|
2017-10-22 11:08:24 +02:00
|
|
|
user.uuid = cut_user_data['sub']
|
2017-11-10 14:24:18 +01:00
|
|
|
user.ou = ou
|
|
|
|
user.first_name = cut_user_data['first_name']
|
|
|
|
user.last_name = cut_user_data['last_name']
|
2017-10-22 10:26:37 +02:00
|
|
|
user.save()
|
2017-10-23 00:15:00 +02:00
|
|
|
OIDCAccount.objects.get_or_create(provider=cut_agents, user=user, sub=cut_user_data['sub'])
|