Commit c54931c4 authored by Valentin Samir's avatar Valentin Samir

[conn_pool] Methode pour récupérer un user ldap ou une machine.

On utilise ces methodes dans les applications.
parent 4e300b42
......@@ -7,15 +7,7 @@ conn_pool = import_module('conn_pool', 'intranet')
# Create your models here.
get_user = conn_pool.get_user
def get_machine(user, mid, mode='ro'):
if mode == 'ro' or conn_pool.LDAP_USERS[user.username].mode == mode:
machine = [ m for m in get_user(user).machines() if m['mid'][0].value == int(mid) ]
else:
machine = conn_pool.get_conn(user).search('mid=%s' % mid, mode=mode)
if machine:
return machine[0]
else:
return None
get_machine = conn_pool.get_machine
def create_machine(user, type, mac, host):
ldif = {
......
......@@ -156,7 +156,6 @@ def edit(request, mid=None):
except ValueError as error:
return render(request, 'machines/add.html', {'machine' : machine, 'error' : 'Erreur : %s' % str(error).replace('"','\\"')})
conn_pool.LDAP_USERS[request.user.username] = conn_pool.CONNS[request.user.username].search('uid=%s' % request.user.username)[0]
return redirect('machines:detail', mid=mid)
......@@ -77,8 +77,8 @@ def prise_autorise(request):
ret = redirect_to_view(request)
if ret: return ret
aid = conn_pool.LDAP_USERS[request.user.username]['aid'][0].value
chbre = conn_pool.LDAP_USERS[request.user.username]['chbre'][0].value
aid = conn_pool.get_user(request.user)['aid'][0].value
chbre = conn_pool.get_user(request.user)['chbre'][0].value
if request.method == "POST":
form = AutoriseForm(request.POST, aid=aid)
if form.is_valid():
......
......@@ -146,7 +146,7 @@ class Profile(models.Model):
le mot de passe, qui devra être redéfini."""
self.user = new_user
try:
aid = conn_pool.LDAP_USERS[new_user.username]['aid'][0].value
aid = conn_pool.get_user(new_user)['aid'][0].value
sip_ext = '1%04d' % int(str(aid)[:4])
except KeyError:
if not settings.DEBUG:
......
......@@ -12,16 +12,9 @@ conn_pool = import_module('conn_pool', 'intranet')
class WikiName(object):
@classmethod
def _get_ldap_from_user(self, user, mode='ro'):
def _get_ldap_from_user(self, user, mode='ro', refresh=False):
""" Get fresh ldap user object """
# TODO faire un truc générique, dans conn_pool, car ça sera probablement
# utilisé à plusieurs endroits
if mode == 'ro' or conn_pool.LDAP_USERS[user.username].mode == mode:
return conn_pool.LDAP_USERS[user.username]
else:
[luser] = conn_pool.CONNS[user.username].search('uid=%s' % user.username, mode=mode)
conn_pool.LDAP_USERS[user.username] = luser
return luser
return conn_pool.get_user(user, mode=mode, refresh=refresh)
@classmethod
def get_value_from_user(self, user):
......@@ -43,5 +36,3 @@ class WikiName(object):
@classmethod
def set_canonical_from_user(self, user):
self.set_value_from_user(user.first_name.capitalize() + user.last_name.capitalize())
......@@ -54,16 +54,12 @@ class CransCASBackend(CASBackend):
django_username = user.username
try:
conn = lc_ldap.shortcuts.lc_ldap_admin(user=user.username)
ldap_user = conn.search('uid=%s' % django_username,\
scope = ldap.SCOPE_ONELEVEL)[0]
ldap_user = conn_pool.get_user(user)
except IndexError:
return None
except ldap.INVALID_CREDENTIALS:
return None
conn_pool.CONNS[django_username] = conn
conn_pool.LDAP_USERS[django_username] = ldap_user
self.refresh_droits(user, ldap_user)
self.refresh_fields(user, ldap_user)
return user
......
......@@ -21,6 +21,9 @@
#
import time
import sys
import ldap
import settings
sys.path.append("/usr/scripts/")
import lc_ldap.shortcuts
......@@ -43,7 +46,15 @@ def get_user(user, mode='ro', refresh=False):
(mode == 'ro' or LDAP_USERS[user.username].mode == mode):
return LDAP_USERS[user.username]
else:
[luser] = get_conn(user).search('uid=%s' % user.username, mode=mode)
[luser] = get_conn(user).search('uid=%s' % user.username, mode=mode, scope=ldap.SCOPE_ONELEVEL)
LDAP_USERS[user.username] = luser
LDAP_USER_LAST_UPDATE[user.username] = time.time()
return luser
def get_machine(user, mid, mode='ro', refresh=False):
luser = get_user(user, mode=mode, refresh=False)
machine = [ m for m in get_user(user).machines() if m['mid'][0].value == int(mid) ]
if machine:
return machine[0]
else:
return None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment