Browse Source

Fix sync_ldap "first name" update and amend other confusing code

Fix #3088 (https://colab.interlegis.leg.br/ticket/3088)
producao
Marcio Mazza 10 years ago
parent
commit
0f6f8b2ca5
  1. 65
      sigi/apps/servidores/management/commands/sync_ldap.py

65
sigi/apps/servidores/management/commands/sync_ldap.py

@ -53,24 +53,17 @@ class Command(BaseCommand):
def sync_users(self): def sync_users(self):
ldap_users = self.get_ldap_users() ldap_users = self.get_ldap_users()
def get_ldap_property(ldap_user, property_name, default_value=None):
value = ldap_user[1].get(property_name, None)
return value[0] if value else default_value
for ldap_user in ldap_users: for ldap_user in ldap_users:
try: username = get_ldap_property(ldap_user, 'sAMAccountName')
username = ldap_user[1]['sAMAccountName'][0] if username:
except: email = get_ldap_property(ldap_user, 'userPrincipalName', '')
pass first_name = get_ldap_property(ldap_user, 'givenName', username)
else: last_name = get_ldap_property(ldap_user, 'sn', '')[:30]
try:
email = ldap_user[1]['userPrincipalName'][0]
except:
email = ''
try:
first_name = ldap_user[1]['givenName'][0]
except:
first_name = username
try:
last_name = ldap_user[1]['sn'][0][:30]
except:
last_name = ''
try: try:
user = User.objects.get(username=username) user = User.objects.get(username=username)
except User.DoesNotExist: except User.DoesNotExist:
@ -82,34 +75,34 @@ class Command(BaseCommand):
username=username, username=username,
email=email email=email
) )
user.first_name = first_name
user.last_name = last_name
print "User '%s' created." % username print "User '%s' created." % username
try: user.first_name = first_name
nome_completo = ldap_user[1]['cn'][0] user.last_name = last_name
except:
nome_completo = '' if not user.email == email:
user.email = email
print "User '%s' email updated." % username
if not user.first_name == first_name:
user.first_name = first_name
print "User '%s' first name updated." % username
if not user.last_name == last_name:
user.last_name = last_name
print "User '%s' last name updated." % username
nome_completo = get_ldap_property(ldap_user, 'cn', '').decode('utf8')
try: try:
servidor = user.servidor servidor = user.servidor
if not servidor.nome_completo == nome_completo.decode('utf8'):
servidor.nome_completo = nome_completo
print "Servidor '%s' updated." % nome_completo
except Servidor.DoesNotExist: except Servidor.DoesNotExist:
try: try:
servidor = Servidor.objects.get(nome_completo=nome_completo) servidor = Servidor.objects.get(nome_completo=nome_completo)
except Servidor.DoesNotExist: except Servidor.DoesNotExist:
servidor = user.servidor_set.create(nome_completo=nome_completo) servidor = user.servidor_set.create(nome_completo=nome_completo)
print "Servidor '%s' created." % nome_completo print "Servidor '%s' created." % nome_completo
else: else:
if not user.email == email.decode('utf8'): if not servidor.nome_completo == nome_completo:
user.email = email servidor.nome_completo = nome_completo
print "User '%s' email updated." % username print "Full name of Servidor '%s' updated." % nome_completo
if not user.first_name == first_name.decode('utf8'):
user.first_name = first_name
print "User '%s' first name updated." % username
if not user.last_name == last_name.decode('utf8'):
user.last_name = last_name
print "User '%s' last name updated." % username
servidor.user = user servidor.user = user
servidor.save() servidor.save()
user.save() user.save()

Loading…
Cancel
Save