0001-fix-crypted-password-importion-9588.patch
mandayejs/mandaye/management/commands/migrate-ldap-users.py | ||
---|---|---|
2 | 2 | |
3 | 3 |
import json |
4 | 4 |
import ldif |
5 |
import logging |
|
5 | 6 | |
6 | 7 |
from django.core.management.base import BaseCommand, CommandError |
7 | 8 |
from django.db import IntegrityError |
... | ... | |
9 | 10 | |
10 | 11 |
from mandayejs.mandaye.models import UserCredentials |
11 | 12 | |
13 |
logger = logging.getLogger(__name__) |
|
12 | 14 | |
13 | 15 |
class Command(BaseCommand): |
14 | 16 |
args = '<ldif_file>' |
... | ... | |
33 | 35 |
for data in parsed_data: |
34 | 36 |
data = { k : ''.join(v) for k,v in data.items()} |
35 | 37 |
try: |
36 |
user = User(username=data.get('idpUniqueID'), last_name=data.get('spLogin')) |
|
38 |
user, created = User.objects.get_or_create(username=data.get('idpUniqueID'), |
|
39 |
last_name=data.get('spLogin')) |
|
37 | 40 |
user.save() |
38 | 41 |
uc = UserCredentials(user=user, locators=json.loads(data.get('spPostValues'))) |
42 |
uc.decrypt() |
|
39 | 43 |
uc.save() |
40 | 44 |
self.stdout.write('{idpUniqueID} imported'.format(**data)) |
45 |
logger.debug('{idpUniqueID} imported'.format(**data)) |
|
41 | 46 |
except (IntegrityError,) as e: |
47 |
logger.debug(e) |
|
42 | 48 |
continue |
43 | 49 |
tests/ldap_users.txt | ||
---|---|---|
1 |
dn: uniqueID=928438,ou=mandaye,dc=entrouvert,dc=org |
|
2 |
objectClass: MandayeUser |
|
3 |
creationDate: 20140716164715Z |
|
4 |
lastConnectionDate: 20111021105200Z |
|
5 |
spName: duonet |
|
6 |
idpUniqueID: 10cdd3ab97600be4abe9764c4a52a7f794f79f7b94b80dcb8ab3973e24c8a6e7 |
|
7 |
idpName: default |
|
8 |
spLogin: ldap_user1 |
|
9 |
structuralObjectClass: MandayeUser |
|
10 |
uniqueID: 928438 |
|
11 |
entryUUID: 061ece3e-f3ac-1033-91e7-8926ba287cf0 |
|
12 |
creatorsName: cn=admin,dc=entrouvert,dc=org |
|
13 |
createTimestamp: 20141029113944Z |
|
14 |
spPostValues: {"txtCode": "Y4HL6cbGxRsoHQU97VzXgkqEdA==", "txtNomFoyer": "ldap_user1", "t |
|
15 |
xtDateNaissance": "23/04/1991"} |
|
16 |
entryCSN: 20141029133206.125440Z#000000#001#000000 |
|
17 |
modifiersName: cn=admin,dc=entrouvert,dc=org |
|
18 |
modifyTimestamp: 20141029133206Z |
|
19 | ||
20 |
dn: uniqueID=434365,ou=mandaye,dc=entrouvert,dc=org |
|
21 |
objectClass: MandayeUser |
|
22 |
creationDate: 20140716164715Z |
|
23 |
lastConnectionDate: 20111017155607Z |
|
24 |
spName: duonet |
|
25 |
idpUniqueID: b5833764a198882b90f28593432992f4030c4d9672e2d98ee6f2ffdea9f9e8d8 |
|
26 |
idpName: default |
|
27 |
spLogin: ldap_user2 |
|
28 |
structuralObjectClass: MandayeUser |
|
29 |
uniqueID: 434365 |
|
30 |
entryUUID: 061fe3c8-f3ac-1033-91ea-8926ba287cf0 |
|
31 |
creatorsName: cn=admin,dc=entrouvert,dc=org |
|
32 |
createTimestamp: 20141029113944Z |
|
33 |
spPostValues: {"txtCode": "Y4HL6cbGxRsoHQU97VzXgkqEdw==", "txtNomFoyer": "ldap_user2", "t |
|
34 |
xtDateNaissance": "23/04/1991"} |
|
35 |
entryCSN: 20141029133206.144171Z#000000#001#000000 |
|
36 |
modifiersName: cn=admin,dc=entrouvert,dc=org |
|
37 |
modifyTimestamp: 20141029133206Z |
|
38 | ||
39 |
dn: uniqueID=1179584,ou=mandaye,dc=entrouvert,dc=org |
|
40 |
objectClass: MandayeUser |
|
41 |
creationDate: 20140716164715Z |
|
42 |
lastConnectionDate: 20111028184745Z |
|
43 |
spName: duonet |
|
44 |
idpUniqueID: 0bb5457e510533c50429163843bc6f57c3582f7dea8661afab834377ac8dfa64 |
|
45 |
idpName: default |
|
46 |
spLogin: ldap_user3 |
|
47 |
structuralObjectClass: MandayeUser |
|
48 |
uniqueID: 1179584 |
|
49 |
entryUUID: 0620818e-f3ac-1033-91ec-8926ba287cf0 |
|
50 |
creatorsName: cn=admin,dc=entrouvert,dc=org |
|
51 |
createTimestamp: 20141029113944Z |
|
52 |
spPostValues: {"txtCode": "Y4HL6cbGxRsoHQU97VzXgkqEdg==", "txtNomFoyer": "ldap_user3", |
|
53 |
"txtDateNaissance": "19/08/1953"} |
|
54 |
entryCSN: 20141029133206.156443Z#000000#001#000000 |
|
55 |
modifiersName: cn=admin,dc=entrouvert,dc=org |
|
56 |
modifyTimestamp: 20141029133206Z |
tests/tests.py | ||
---|---|---|
2 | 2 | |
3 | 3 |
from django.conf import settings |
4 | 4 |
from django.contrib.auth.models import User |
5 |
from django.core.management import call_command |
|
5 | 6 | |
6 | 7 |
from mandayejs.mandaye.models import UserCredentials |
7 | 8 | |
8 | 9 |
pytestmark = pytest.mark.django_db |
9 | 10 | |
11 |
# Encryption/Decryption |
|
12 | ||
10 | 13 |
def create_user(**kwargs): |
11 | 14 |
password = kwargs.pop('password', None) or kwargs.get('username') |
12 | 15 |
user, created = User.objects.get_or_create(**kwargs) |
... | ... | |
41 | 44 |
assert decrypted.get('password') == 'john password' |
42 | 45 |
|
43 | 46 | |
47 |
# Migration |
|
48 | ||
49 | ||
50 | ||
44 | 51 |
def test_migrate_users_command(): |
45 |
pass |
|
52 | ||
53 |
args = ['tests/ldap_users.txt',] |
|
54 |
opts = {} |
|
55 |
call_command('migrate-ldap-users', *args, **opts) |
|
56 | ||
57 |
credentials = UserCredentials.objects.filter(user__last_name__in=[ |
|
58 |
'ldap_user1', |
|
59 |
'ldap_user2', |
|
60 |
'ldap_user3' |
|
61 |
]) |
|
62 | ||
63 |
assert len(credentials) == 3 |
|
64 | ||
65 |
for cred in credentials: |
|
66 |
assert cred.to_login_info(decrypt=True)['#txtCode'] == 'password_{}'.format(cred.user.last_name) |
|
67 | ||
46 |
- |