[lc_ldap] corrections
This commit is contained in:
parent
a36bafa021
commit
5f3c3d56fb
3 changed files with 81 additions and 24 deletions
61
attributs.py
61
attributs.py
|
@ -33,6 +33,12 @@
|
|||
import re
|
||||
from unicodedata import normalize
|
||||
|
||||
def normalize_and_validate(attr, vals, ctxt):
|
||||
"""Vérifie que attr peut se voir attribuer les valeurs vals"""
|
||||
a = eval('%s()' % attr)
|
||||
new_vals = a.normalize(vals, ctxt)
|
||||
a.validate(new_vals, ctxt)
|
||||
return new_vals
|
||||
|
||||
def validate_name(value, more_chars=""):
|
||||
"""Valide un nom: ie un unicode qui contient lettres, espaces et
|
||||
|
@ -49,6 +55,9 @@ class Attr:
|
|||
singlevalue = None
|
||||
optional = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def validate(self, values, uldif):
|
||||
"validates"
|
||||
self._check_cardinality(values)
|
||||
|
@ -94,6 +103,12 @@ class Attr:
|
|||
"""Vérifie si l'attribut est modifiable"""
|
||||
return False
|
||||
|
||||
|
||||
class objectClass(Attr):
|
||||
singlevalue = False
|
||||
optional = False
|
||||
legend = "entité"
|
||||
|
||||
class nom(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
|
@ -166,6 +181,23 @@ class solde(Attr):
|
|||
legend = u"Solde d'impression"
|
||||
|
||||
|
||||
class host(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
hname = legend = u"Nom d'hôte"
|
||||
|
||||
class macAddress(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
legend = u"Adresse physique de la carte réseau"
|
||||
hname = "Adresse MAC"
|
||||
|
||||
class ipHostNumber(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
legend = u"Adresse IPv4 de la machine"
|
||||
hname = "IPv4"
|
||||
|
||||
class mid(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
|
@ -238,3 +270,32 @@ class historique(Attr):
|
|||
legend = u"Historique de l'objet"
|
||||
|
||||
|
||||
CRANS_ATTRIBUTES= {
|
||||
'objectClass' : objectClass(),
|
||||
'nom' : nom(),
|
||||
'prenom' : prenom(),
|
||||
'tel' : tel(),
|
||||
'paiement' : paiement(),
|
||||
'carteEtudiant' : carteEtudiant(),
|
||||
'mailAlias' : mailAlias(),
|
||||
'canonicalAlias' : canonicalAlias(),
|
||||
'etudes' : etudes(),
|
||||
'chbre' : chbre(),
|
||||
'droits' : droits(),
|
||||
'solde' : solde(),
|
||||
'mid' : mid(),
|
||||
'hostAlias' : hostAlias(),
|
||||
'ipsec' : ipsec(),
|
||||
'puissance' : puissance(),
|
||||
'canal' : canal(),
|
||||
'portTCPout' : portTCPout(),
|
||||
'portTCPin' : portTCPin(),
|
||||
'portUDPout' : portUDPout(),
|
||||
'portUDPin' : portUDPin(),
|
||||
'prise' : prise(),
|
||||
'cid' : cid(),
|
||||
'responsable' : responsable(),
|
||||
'blacklist' : blacklist(),
|
||||
'historique' : historique()
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ def ip_of_mid(mid):
|
|||
else:
|
||||
raise ValueError("Mid dans aucune plage: %d" % mid)
|
||||
|
||||
return netaddr.IPAddress(netaddr.IPNetwork(config.NETs[net]).first + mid - plage[0])
|
||||
return netaddr.IPAddress(netaddr.IPNetwork(config.NETs[net][0]).first + mid - plage[0])
|
||||
|
||||
def strip_accents(a):
|
||||
""" Supression des accents de la chaîne fournie"""
|
||||
|
|
42
lc_ldap.py
42
lc_ldap.py
|
@ -123,7 +123,8 @@ class lc_ldap(ldap.ldapobject.LDAPObject):
|
|||
|
||||
def newMachine(self, parent, realm, uldif):
|
||||
"""Crée une nouvelle machine: realm peut être:
|
||||
adm, serveurs, bornes, wifi, adherents, gratuit ou personnel-ens"""
|
||||
fil, fil-v6, wifi, wifi-v6, adm, gratuit, personnel-ens, special"""
|
||||
#adm, serveurs, bornes, wifi, adherents, gratuit ou personnel-ens"""
|
||||
owner = self.search('objectClass=*', dn=parent, scope=0)[0]
|
||||
|
||||
if realm in ["adm", "serveurs"]:
|
||||
|
@ -136,21 +137,21 @@ class lc_ldap(ldap.ldapobject.LDAPObject):
|
|||
assert isinstance(owner, AssociationCrans)
|
||||
# XXX - Vérifier les droits
|
||||
|
||||
elif realm in ["wifi", "wifi-adh"]:
|
||||
elif realm in ["wifi", "wifi-v6"]:
|
||||
uldif['objectClass'] = [u'machineWifi']
|
||||
realm = "wifi-adh"
|
||||
assert isinstance(owner, adherent) or isinstance(owner, club)
|
||||
# XXX - Vérifier les droits (owner.type_connexion)
|
||||
|
||||
elif realm in ["adherent", "gratuit", "personnel-ens"]:
|
||||
elif realm in ["fil", "fil-v6", "gratuit", "personnel-ens"]:
|
||||
uldif['objectClass'] = [u'machineFixe']
|
||||
assert isinstance(owner, adherent) or isinstance(owner, club)
|
||||
# XXX - Vérifier les droits
|
||||
|
||||
else: raise ValueError("Realm inconnu: %s" % realm)
|
||||
|
||||
plage = config.NETs[realm]
|
||||
plage = xrange( *(config.mid[realm]))
|
||||
mid = uldif.setdefault('mid', [ unicode(self._find_id('mid', plage)) ])
|
||||
uldif['ipHostNumber'] = [ unicode(crans_utils.ip_of_mid(int (mid[0]))) ]
|
||||
return self._create_entity('mid=%s,%s' % (mid[0], parent), uldif)
|
||||
|
||||
def newAdherent(self, uldif):
|
||||
|
@ -170,19 +171,21 @@ class lc_ldap(ldap.ldapobject.LDAPObject):
|
|||
def _create_entity(self, dn, uldif):
|
||||
'''Crée une nouvelle entité ldap en dn, avec attributs ldif:
|
||||
uniquement en unicode'''
|
||||
for attr, vals in uldif:
|
||||
nuldif = self.normalize_and_validate(attr, vals)
|
||||
lock = CransLock(self)
|
||||
nuldif = {}
|
||||
for attr, vals in uldif.items() :
|
||||
nuldif[attr] = attributs.normalize_and_validate(attr, vals, uldif)
|
||||
#lock = CransLock(self)
|
||||
for item in ['aid', 'uid', 'chbre', 'mailAlias', 'canonicalAlias',
|
||||
'fid', 'cid', 'mid', 'macAddress', 'host', 'hostAlias' ]:
|
||||
for val in nuldif.get(item, []):
|
||||
lock.add(item, val)
|
||||
pass #lock.add(item, val)
|
||||
#uldif['historique'] = [ self._hist('Création')]
|
||||
ldif = uldif_to_ldif(nuldif)
|
||||
modlist = addModlist(ldif)
|
||||
with lock:
|
||||
#with lock:
|
||||
# print dn, modlist
|
||||
self.add_s(dn, modlist)
|
||||
#
|
||||
self.add_s(dn, modlist)
|
||||
return CransLdapObject(self, dn, mode='w')
|
||||
|
||||
def _find_id(self, attr, plage = xrange(1, 32000)):
|
||||
|
@ -200,7 +203,7 @@ class lc_ldap(ldap.ldapobject.LDAPObject):
|
|||
else:
|
||||
raise EnvironmentError(u'Aucun %s libre dans la plage [%d, %d]' %
|
||||
(attr, plage[0], i))
|
||||
return id
|
||||
return i
|
||||
|
||||
def _hist(self, msg):
|
||||
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M : ')
|
||||
|
@ -250,10 +253,10 @@ class CransLdapObject:
|
|||
raise EnvironmentError(u"Objet en lecture seule, réessayer en lecture/écriture")
|
||||
|
||||
# Vérifications et Historique
|
||||
histo = self._gen_hist(self._modifs)
|
||||
self._modifs['historique'] += histo
|
||||
#histo = self._gen_hist(self._modifs)
|
||||
#self._modifs['historique'] += histo
|
||||
for attr, vals in self._modifs.items:
|
||||
self._modifs[attr] = self.normalize_and_validate(attr, vals)
|
||||
self._modifs[attr] = attributs.normalize_and_validate(attr, vals, self._modifs)
|
||||
|
||||
# On récupère la liste des modifications
|
||||
modlist = self.get_modlist()
|
||||
|
@ -288,7 +291,7 @@ class CransLdapObject:
|
|||
for val in new_vals: assert isinstance(val, unicode)
|
||||
|
||||
# On vérifie les nouvelles valeurs données à l'attribut
|
||||
new_vals = self.normalize_and_validate(attr, new_vals)
|
||||
new_vals = attributs.normalize_and_validate(attr, new_vals, self._modifs)
|
||||
|
||||
# Si ça passe, on effectue les modifications
|
||||
old_vals = [ unicode.encode(val, 'utf-8') for val in self.attrs.get(attr, []) ]
|
||||
|
@ -324,13 +327,6 @@ class CransLdapObject:
|
|||
new_vals.append(new_val)
|
||||
return self.set_ldapattr(attr, new_vals)
|
||||
|
||||
def normalize_and_validate(self, attr, vals):
|
||||
"""Vérifie que attr peut se voir attribuer les valeurs vals"""
|
||||
a = eval('attributs.%s()' % attr)
|
||||
new_vals = a.normalize(vals, self._modifs if self._modifs else self.attrs)
|
||||
a.validate(new_vals, self._modifs if self._modifs else self.attrs)
|
||||
return new_vals
|
||||
|
||||
def _gen_hist(self, modifs):
|
||||
# XXX - Kill it! l'historique devrait être généré par ldap
|
||||
"""Genère l'historique des modifications apportées. Cette
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue