Esthtisme.
darcs-hash:20060309235447-68412-3733c8be4ceec74ea69f9cae948181293fabac74.gz
This commit is contained in:
parent
95a6fe21bd
commit
aea8585794
1 changed files with 51 additions and 46 deletions
|
@ -506,18 +506,19 @@ class crans_ldap:
|
||||||
def search(self, expression, mode=''):
|
def search(self, expression, mode=''):
|
||||||
"""
|
"""
|
||||||
Recherche dans la base LDAP, expression est une chaîne :
|
Recherche dans la base LDAP, expression est une chaîne :
|
||||||
une expression : champ1=expr1 champ2=expr2 champ3!=expr3....
|
* soit une expression : champ1=expr1&champ2=expr2&champ3!=expr3...
|
||||||
soit un seul terme, dans ce cas cherche sur les champs de auto_search_champs
|
* soit un seul terme, dans ce cas cherche sur les champs de
|
||||||
Si mode ='w' les instances crées seront en mode d'écriture
|
auto_search_champs
|
||||||
|
Si mode='w', les instances crées seront en mode d'écriture
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if type(expression)==str:
|
if type(expression) == str:
|
||||||
# Transformation de l'expression en utf-8
|
# Transformation de l'expression en utf-8
|
||||||
expression = unicode(expression,'iso-8859-15').encode('utf-8')
|
expression = unicode(expression, 'iso-8859-15').encode('utf-8')
|
||||||
elif type(expression)==unicode:
|
elif type(expression) == unicode:
|
||||||
expression = expression.encode('utf-8')
|
expression = expression.encode('utf-8')
|
||||||
else:
|
else:
|
||||||
raise TypeError(u'Chaine attendue')
|
raise TypeError(u'Chaîne attendue')
|
||||||
|
|
||||||
if not expression:
|
if not expression:
|
||||||
return []
|
return []
|
||||||
|
@ -527,34 +528,36 @@ class crans_ldap:
|
||||||
|
|
||||||
# Il faut un filtre par type d'objet de la base
|
# Il faut un filtre par type d'objet de la base
|
||||||
filtres = self.auto_search_champs.keys()
|
filtres = self.auto_search_champs.keys()
|
||||||
result={'adherent': [], 'machine': [], 'club': []}
|
result = {'adherent': [], 'machine': [], 'club': []}
|
||||||
|
|
||||||
# Fonction utile
|
# Fonction utile
|
||||||
def build_filtre(champ,expr,neg=0):
|
def build_filtre(champ, expr, neg=0):
|
||||||
""" Retourne une chaine pour recherche dans la base LDAP
|
"""
|
||||||
du style (champ=expr) en adaptant les valeurs de expr au champ
|
Retourne une chaine pour recherche dans la base LDAP
|
||||||
si neg = 1: retourne le négatif : (!(champ=expr))"""
|
du style (champ=expr) en adaptant les valeurs de expr au champ.
|
||||||
|
Si neg = 1, retourne le négatif : (!(champ=expr))
|
||||||
|
"""
|
||||||
el = ''
|
el = ''
|
||||||
if champ in [ 'host', 'hostAlias' ]:
|
if champ in ['host', 'hostAlias']:
|
||||||
if expr[-1] == '*':
|
if expr[-1] == '*':
|
||||||
el = '(%s=%s)' % (champ, expr)
|
el = '(%s=%s)' % (champ, expr)
|
||||||
elif expr.find('.')==-1:
|
elif expr.find('.') == -1:
|
||||||
el = '(%s=%s.*)' % ( champ, expr)
|
el = '(%s=%s.*)' % (champ, expr)
|
||||||
else:
|
else:
|
||||||
el = '(%s=%s*)' % ( champ, expr)
|
el = '(%s=%s*)' % (champ, expr)
|
||||||
elif champ == 'macAddress':
|
elif champ == 'macAddress':
|
||||||
# Formatage adresse mac
|
# Formatage adresse mac
|
||||||
try: el = '(macAddress=%s)' % format_mac(expr)
|
try: el = '(macAddress=%s)' % format_mac(expr)
|
||||||
except: pass
|
except: pass
|
||||||
elif champ == 'paiement' and expr == 'ok':
|
elif champ == 'paiement' and expr == 'ok':
|
||||||
# Paiement donnant droit à une connexion maintenant ?
|
# Paiement donnant droit à une connexion maintenant ?
|
||||||
# il doit avoir payé pour
|
# (il doit avoir payé pour l'année en cours ou pour
|
||||||
# l'année en cours ou pour l'année précédente
|
# l'année précédente si on est en septembre
|
||||||
# si on est en septembre
|
|
||||||
#
|
#
|
||||||
# Dans tous les cas, pour un adhérent, le paiement est considéré non ok
|
# Dans tous les cas, pour un adhérent, le paiement est
|
||||||
# s'il n'a pas fourni sa carte d'etudiant alors que l'on est desormais
|
# considéré non ok s'il n'a pas fourni sa carte d'etudiant
|
||||||
# en periode de bloquage definifif (cf config.py).
|
# alors que l'on est desormais en periode de bloquage
|
||||||
|
# définifif (cf config.py).
|
||||||
if localtime()[1] == 9:
|
if localtime()[1] == 9:
|
||||||
# Pour septembre paiement année précédente ok
|
# Pour septembre paiement année précédente ok
|
||||||
el = "(|(paiement=%s)(paiement=%s))" % (int(ann_scol), int(ann_scol)-1)
|
el = "(|(paiement=%s)(paiement=%s))" % (int(ann_scol), int(ann_scol)-1)
|
||||||
|
@ -571,17 +574,17 @@ class crans_ldap:
|
||||||
if neg: el = '(!%s)' % el
|
if neg: el = '(!%s)' % el
|
||||||
return el
|
return el
|
||||||
|
|
||||||
if expression.find('=')!=-1:
|
if expression.find('=') != -1:
|
||||||
#### Recherche avec conditions explicites
|
#### Recherche avec conditions explicites
|
||||||
## Construction des filtres
|
## Construction des filtres
|
||||||
|
|
||||||
# initialisation
|
# initialisation
|
||||||
filtre={}
|
filtre = {}
|
||||||
filtre_cond={} # Stokage si filtre avec condition (ne comporte pas que des *)
|
filtre_cond = {} # Stokage si filtre avec condition (ne comporte pas que des *)
|
||||||
filtre_tout=1 # Passse à 0 si au moins une condition
|
filtre_tout = 1 # Passse à 0 si au moins une condition
|
||||||
|
|
||||||
for i in filtres:
|
for i in filtres:
|
||||||
filtre[i]='(&'
|
filtre[i] = '(&'
|
||||||
filtre_cond[i] = 0
|
filtre_cond[i] = 0
|
||||||
conds = expression.split('&')
|
conds = expression.split('&')
|
||||||
|
|
||||||
|
@ -598,31 +601,31 @@ class crans_ldap:
|
||||||
raise ValueError(u'Syntaxe de recherche invalide.')
|
raise ValueError(u'Syntaxe de recherche invalide.')
|
||||||
|
|
||||||
# transformation de certains champs
|
# transformation de certains champs
|
||||||
champ = self.trans.get(champ,champ)
|
champ = self.trans.get(champ, champ)
|
||||||
|
|
||||||
if expr=='': expr='*'
|
if expr == '': expr='*'
|
||||||
|
|
||||||
ok = 0
|
ok = 0
|
||||||
|
|
||||||
# Construction du filtre
|
# Construction du filtre
|
||||||
for i in filtres:
|
for i in filtres:
|
||||||
if champ in self.auto_search_champs[i] + self.non_auto_search_champs[i]:
|
if champ in self.auto_search_champs[i] + self.non_auto_search_champs[i]:
|
||||||
filtre[i] += build_filtre(champ,expr,neg)
|
filtre[i] += build_filtre(champ, expr, neg)
|
||||||
ok = 1
|
ok = 1
|
||||||
if expr!='*' or neg:
|
if expr != '*' or neg:
|
||||||
filtre_cond[i] = 1
|
filtre_cond[i] = 1
|
||||||
filtre_tout=0
|
filtre_tout = 0
|
||||||
if not ok:
|
if not ok:
|
||||||
raise ValueError(u'Champ de recherche inconnu (%s)'% champ )
|
raise ValueError(u'Champ de recherche inconnu (%s)'% champ )
|
||||||
|
|
||||||
## Recherche avec chacun des filtres
|
## Recherche avec chacun des filtres
|
||||||
r={} # contiendra les réponses par filtre
|
r={} # contiendra les réponses par filtre
|
||||||
for i in filtres:
|
for i in filtres:
|
||||||
if (filtre_tout and filtre[i]!='(&' ) or filtre_cond[i]:
|
if (filtre_tout and filtre[i] != '(&' ) or filtre_cond[i]:
|
||||||
# Filtre valide
|
# Filtre valide
|
||||||
#filtre[i] += ')'
|
#filtre[i] += ')'
|
||||||
filtre[i] += '(objectClass=%s))' % i
|
filtre[i] += '(objectClass=%s))' % i
|
||||||
r[i] = self.conn.search_s(self.base_dn,self.scope[i],filtre[i])
|
r[i] = self.conn.search_s(self.base_dn, self.scope[i], filtre[i])
|
||||||
else:
|
else:
|
||||||
r[i] = None
|
r[i] = None
|
||||||
|
|
||||||
|
@ -637,7 +640,8 @@ class crans_ldap:
|
||||||
# Filtre sur un seul champ
|
# Filtre sur un seul champ
|
||||||
# => on retourne tout
|
# => on retourne tout
|
||||||
for i in filtres:
|
for i in filtres:
|
||||||
if not r[i]: continue
|
if not r[i]:
|
||||||
|
continue
|
||||||
for res in r[i]:
|
for res in r[i]:
|
||||||
if i == "machine":
|
if i == "machine":
|
||||||
if res[1].has_key('puissance'):
|
if res[1].has_key('puissance'):
|
||||||
|
@ -659,17 +663,18 @@ class crans_ldap:
|
||||||
# => on retourne uniquement les adhérents
|
# => on retourne uniquement les adhérents
|
||||||
if r['adherent']:
|
if r['adherent']:
|
||||||
for a in r['adherent']:
|
for a in r['adherent']:
|
||||||
result['adherent'].append(adherent(a,mode,self.conn) )
|
result['adherent'].append(adherent(a, mode, self.conn))
|
||||||
if r['club']:
|
if r['club']:
|
||||||
for a in r['club']:
|
for a in r['club']:
|
||||||
result['club'].append(club(a,mode,self.conn) )
|
result['club'].append(club(a, mode, self.conn))
|
||||||
else:
|
else:
|
||||||
# Il faut croiser les résultats machine et propriétaire
|
# Il faut croiser les résultats machine et propriétaire
|
||||||
# Traitement des machines
|
# Traitement des machines
|
||||||
mach_adh = [] # liste de dn d'adhérents et de clubs
|
mach_adh = [] # liste de dn d'adhérents et de clubs
|
||||||
for res in r['machine']:
|
for res in r['machine']:
|
||||||
dn = string.join(res[0].split(',')[-4:],',')
|
dn = ','.join(res[0].split(',')[-4:])
|
||||||
if dn[:3] != 'aid' and dn[:3] != 'cid': continue
|
if dn[:3] != 'aid' and dn[:3] != 'cid':
|
||||||
|
continue
|
||||||
if dn not in mach_adh:
|
if dn not in mach_adh:
|
||||||
mach_adh.append(dn)
|
mach_adh.append(dn)
|
||||||
|
|
||||||
|
@ -678,16 +683,16 @@ class crans_ldap:
|
||||||
for a in r['adherent']:
|
for a in r['adherent']:
|
||||||
if a[0] in mach_adh and not a[0] in bons_dn:
|
if a[0] in mach_adh and not a[0] in bons_dn:
|
||||||
bons_dn.append(a[0])
|
bons_dn.append(a[0])
|
||||||
result['adherent'].append(adherent(a,mode,self.conn) )
|
result['adherent'].append(adherent(a, mode, self.conn))
|
||||||
for a in r['club']:
|
for a in r['club']:
|
||||||
if a[0] in mach_adh and not a[0] in bons_dn:
|
if a[0] in mach_adh and not a[0] in bons_dn:
|
||||||
bons_dn.append(a[0])
|
bons_dn.append(a[0])
|
||||||
result['club'].append(club(a,mode,self.conn) )
|
result['club'].append(club(a, mode, self.conn))
|
||||||
|
|
||||||
# Maintenant c'est au tour des bonnes machines
|
# Maintenant c'est au tour des bonnes machines
|
||||||
bons_dn2 = []
|
bons_dn2 = []
|
||||||
for a in r['machine']:
|
for a in r['machine']:
|
||||||
dn = string.join(a[0].split(',')[-4:],',')
|
dn = string.join(a[0].split(',')[-4:], ',')
|
||||||
if dn in bons_dn and not a[0] in bons_dn2:
|
if dn in bons_dn and not a[0] in bons_dn2:
|
||||||
bons_dn2.append(dn)
|
bons_dn2.append(dn)
|
||||||
if a[1].has_key('puissance'):
|
if a[1].has_key('puissance'):
|
||||||
|
@ -705,11 +710,11 @@ class crans_ldap:
|
||||||
# Construction du filtre
|
# Construction du filtre
|
||||||
filtre = '(&(|'
|
filtre = '(&(|'
|
||||||
for champ in self.auto_search_champs[i]:
|
for champ in self.auto_search_champs[i]:
|
||||||
filtre += build_filtre(champ,expression)
|
filtre += build_filtre(champ, expression)
|
||||||
filtre+=')(objectClass=%s))' %i
|
filtre += ')(objectClass=%s))' %i
|
||||||
|
|
||||||
# Recherche
|
# Recherche
|
||||||
for r in self.conn.search_s(self.base_dn,self.scope[i],filtre):
|
for r in self.conn.search_s(self.base_dn, self.scope[i], filtre):
|
||||||
if i == 'machine':
|
if i == 'machine':
|
||||||
if r[1].has_key('puissance'):
|
if r[1].has_key('puissance'):
|
||||||
result['machine'].append(BorneWifi(r, mode, self.conn))
|
result['machine'].append(BorneWifi(r, mode, self.conn))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue