mirror of https://github.com/interlegis/sigi.git
Eduardo Edson Batista Cordeiro Alves
9 years ago
committed by
Luciano Almeida
100 changed files with 424 additions and 315 deletions
@ -0,0 +1,17 @@ |
|||||
|
#!/bin/bash |
||||
|
|
||||
|
# Check if there's some debug breakpoint in codebase |
||||
|
me=`basename "$0"` |
||||
|
stmts=`grep --exclude=$me -r -l "ipdb.set_trace()" * | wc -l` |
||||
|
if [ $stmts != '0' ] |
||||
|
then |
||||
|
echo "==================================================================" |
||||
|
echo "ERROR: ipdb.set_trace() call in codebase! Remove, please." |
||||
|
grep --exclude=$me -r -n "ipdb.set_trace()" * |
||||
|
echo "==================================================================" |
||||
|
fi |
||||
|
|
||||
|
# QA checks: run this before every commit |
||||
|
./manage.py check |
||||
|
flake8 --exclude='ipython_log.py*,migrations,templates' . |
||||
|
isort --recursive --check-only --skip='migrations' --skip='templates' --skip='ipython_log.py' . |
@ -0,0 +1,11 @@ |
|||||
|
#!/bin/bash |
||||
|
|
||||
|
# QA fix: Use ese script para corrigir automaticamente vários |
||||
|
# problemas de estilo e boas práticas no código. |
||||
|
# |
||||
|
# Sempre guarde suas mudanças de alguma forma antes de aplicar esse script, |
||||
|
# de modo que possa revisar cada alteração que ele fez. |
||||
|
# Uma forma simples de fazer isso é adicionando antes suas mudanças à |
||||
|
# "staging area" do git, com `git add .` e após usar o script `git diff`. |
||||
|
|
||||
|
isort --recursive --skip='migrations' --skip='templates' --skip='ipython_log.py' . |
@ -1,113 +1,113 @@ |
|||||
# coding: utf-8 |
# coding: utf-8 |
||||
import ldap |
import ldap |
||||
from django.contrib.auth.models import User, Group |
from django.contrib.auth.models import Group, User |
||||
from django.core.management.base import BaseCommand |
from django.core.management.base import BaseCommand |
||||
|
|
||||
from sigi.apps.servidores.models import Servidor |
from sigi.apps.servidores.models import Servidor |
||||
from sigi.settings import * |
from sigi.settings import * |
||||
|
|
||||
|
|
||||
class Command(BaseCommand): |
class Command(BaseCommand): |
||||
help = u'Sincroniza Usuários e Servidores com o LDAP' |
help = u'Sincroniza Usuários e Servidores com o LDAP' |
||||
|
|
||||
def handle(self, *args, **options): |
def handle(self, *args, **options): |
||||
self.sync_groups() |
self.sync_groups() |
||||
self.sync_users() |
self.sync_users() |
||||
|
|
||||
def get_ldap_groups(self): |
def get_ldap_groups(self): |
||||
filter = "(&(objectclass=Group))" |
filter = "(&(objectclass=Group))" |
||||
values = ['cn', ] |
values = ['cn', ] |
||||
l = ldap.initialize(AUTH_LDAP_SERVER_URI) |
l = ldap.initialize(AUTH_LDAP_SERVER_URI) |
||||
l.protocol_version = ldap.VERSION3 |
l.protocol_version = ldap.VERSION3 |
||||
l.simple_bind_s(AUTH_LDAP_BIND_DN.encode('utf-8'), AUTH_LDAP_BIND_PASSWORD) |
l.simple_bind_s(AUTH_LDAP_BIND_DN.encode('utf-8'), AUTH_LDAP_BIND_PASSWORD) |
||||
result_id = l.search(AUTH_LDAP_GROUP, ldap.SCOPE_SUBTREE, filter, values) |
result_id = l.search(AUTH_LDAP_GROUP, ldap.SCOPE_SUBTREE, filter, values) |
||||
result_type, result_data = l.result(result_id, 1) |
result_type, result_data = l.result(result_id, 1) |
||||
l.unbind() |
l.unbind() |
||||
return result_data |
return result_data |
||||
|
|
||||
def get_ldap_users(self): |
def get_ldap_users(self): |
||||
filter = "(&(objectclass=user))" |
filter = "(&(objectclass=user))" |
||||
values = ['sAMAccountName', 'userPrincipalName', 'givenName', 'sn', 'cn'] |
values = ['sAMAccountName', 'userPrincipalName', 'givenName', 'sn', 'cn'] |
||||
l = ldap.initialize(AUTH_LDAP_SERVER_URI) |
l = ldap.initialize(AUTH_LDAP_SERVER_URI) |
||||
l.protocol_version = ldap.VERSION3 |
l.protocol_version = ldap.VERSION3 |
||||
l.simple_bind_s(AUTH_LDAP_BIND_DN.encode('utf-8'), AUTH_LDAP_BIND_PASSWORD) |
l.simple_bind_s(AUTH_LDAP_BIND_DN.encode('utf-8'), AUTH_LDAP_BIND_PASSWORD) |
||||
result_id = l.search(AUTH_LDAP_USER.encode('utf-8'), ldap.SCOPE_SUBTREE, filter, values) |
result_id = l.search(AUTH_LDAP_USER.encode('utf-8'), ldap.SCOPE_SUBTREE, filter, values) |
||||
result_type, result_data = l.result(result_id, 1) |
result_type, result_data = l.result(result_id, 1) |
||||
l.unbind() |
l.unbind() |
||||
return result_data |
return result_data |
||||
|
|
||||
def sync_groups(self): |
def sync_groups(self): |
||||
ldap_groups = self.get_ldap_groups() |
ldap_groups = self.get_ldap_groups() |
||||
for ldap_group in ldap_groups: |
for ldap_group in ldap_groups: |
||||
try: |
try: |
||||
group_name = ldap_group[1]['cn'][0] |
group_name = ldap_group[1]['cn'][0] |
||||
except: |
except: |
||||
pass |
pass |
||||
else: |
else: |
||||
try: |
try: |
||||
group = Group.objects.get(name=group_name) |
group = Group.objects.get(name=group_name) |
||||
except Group.DoesNotExist: |
except Group.DoesNotExist: |
||||
group = Group(name=group_name) |
group = Group(name=group_name) |
||||
group.save() |
group.save() |
||||
print "Group '%s' created." % group_name |
print "Group '%s' created." % group_name |
||||
print "Groups are synchronized." |
print "Groups are synchronized." |
||||
|
|
||||
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): |
def get_ldap_property(ldap_user, property_name, default_value=None): |
||||
value = ldap_user[1].get(property_name, None) |
value = ldap_user[1].get(property_name, None) |
||||
return value[0].decode('utf8') if value else default_value |
return value[0].decode('utf8') if value else default_value |
||||
|
|
||||
for ldap_user in ldap_users: |
for ldap_user in ldap_users: |
||||
username = get_ldap_property(ldap_user, 'sAMAccountName') |
username = get_ldap_property(ldap_user, 'sAMAccountName') |
||||
if username: |
if username: |
||||
email = get_ldap_property(ldap_user, 'userPrincipalName', '') |
email = get_ldap_property(ldap_user, 'userPrincipalName', '') |
||||
first_name = get_ldap_property(ldap_user, 'givenName', username) |
first_name = get_ldap_property(ldap_user, 'givenName', username) |
||||
last_name = get_ldap_property(ldap_user, 'sn', '')[:30] |
last_name = get_ldap_property(ldap_user, 'sn', '')[:30] |
||||
try: |
try: |
||||
user = User.objects.get(username=username) |
user = User.objects.get(username=username) |
||||
except User.DoesNotExist: |
except User.DoesNotExist: |
||||
try: |
try: |
||||
user = User.objects.get(email=email) |
user = User.objects.get(email=email) |
||||
old_username = user.username |
old_username = user.username |
||||
user.username = username |
user.username = username |
||||
print "User with email '%s' had his/her username updated from [%s] to [%s]." % ( |
print "User with email '%s' had his/her username updated from [%s] to [%s]." % ( |
||||
email, old_username, username) |
email, old_username, username) |
||||
except User.DoesNotExist: |
except User.DoesNotExist: |
||||
user = User.objects.create_user( |
user = User.objects.create_user( |
||||
username=username, |
username=username, |
||||
first_name=first_name, |
first_name=first_name, |
||||
last_name=last_name, |
last_name=last_name, |
||||
email=email, |
email=email, |
||||
) |
) |
||||
print "User '%s' created." % username |
print "User '%s' created." % username |
||||
|
|
||||
if not user.first_name == first_name: |
if not user.first_name == first_name: |
||||
user.first_name = first_name |
user.first_name = first_name |
||||
print "User '%s' first name updated." % username |
print "User '%s' first name updated." % username |
||||
if not user.last_name == last_name: |
if not user.last_name == last_name: |
||||
user.last_name = last_name |
user.last_name = last_name |
||||
print "User '%s' last name updated." % username |
print "User '%s' last name updated." % username |
||||
if not user.email == email: |
if not user.email == email: |
||||
user.email = email |
user.email = email |
||||
print "User '%s' email updated." % username |
print "User '%s' email updated." % username |
||||
|
|
||||
nome_completo = get_ldap_property(ldap_user, 'cn', '') |
nome_completo = get_ldap_property(ldap_user, 'cn', '') |
||||
try: |
try: |
||||
servidor = user.servidor |
servidor = user.servidor |
||||
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 servidor.nome_completo == nome_completo: |
if not servidor.nome_completo == nome_completo: |
||||
servidor.nome_completo = nome_completo |
servidor.nome_completo = nome_completo |
||||
print "Full name of Servidor '%s' updated." % nome_completo |
print "Full name of Servidor '%s' updated." % nome_completo |
||||
|
|
||||
servidor.user = user |
servidor.user = user |
||||
servidor.save() |
servidor.save() |
||||
user.save() |
user.save() |
||||
print "Users are synchronized." |
print "Users are synchronized." |
||||
|
Loading…
Reference in new issue