ajout des restrict mac + dns-name + debut changement room membre

This commit is contained in:
Romain Beuque 2013-03-30 20:55:55 +01:00
parent 9c147d6f51
commit f5221581ec
6 changed files with 109 additions and 18 deletions

View File

@ -17,7 +17,7 @@ from operator import itemgetter
from datetime import datetime from datetime import datetime
import uuid import uuid
import re
#root = tg.config['application_root_module'].RootController #root = tg.config['application_root_module'].RootController
@ -42,7 +42,37 @@ class EditController(AuthenticatedBaseController):
""" Affiche les détails éditables du membre et de la chambre """ """ Affiche les détails éditables du membre et de la chambre """
@expose("brie.templates.edit.member") @expose("brie.templates.edit.member")
def member(self, residence, uid): def member(self, residence, uid):
return self.show.member(residence, uid)
residence_dn = Residences.get_dn_by_name(self.user, residence)
if residence_dn is None:
raise Exception("unknown residence")
#end if
member = Member.get_by_uid(self.user, residence_dn, uid)
if member is None:
return self.error_no_entry()
room = Room.get_by_member_dn(self.user, residence_dn, member.dn)
machines = Machine.get_machine_tuples_of_member(self.user, member.dn)
groups = Groupes.get_by_user_dn(self.user, residence_dn, member.dn)
rooms = Room.get_rooms(self.user, residence_dn)
if rooms is None:
raise Exception("unable to retrieve rooms")
#end if
rooms = sorted(rooms, key=lambda t:t.cn.first())
return {
"residence" : residence,
"user" : self.user,
"member_ldap" : member,
"room_ldap" : room,
"machines" : machines,
"groups" : groups,
"rooms" : rooms
}
#end def #end def
""" Affiche les détails éditables de la chambre """ """ Affiche les détails éditables de la chambre """
@ -79,24 +109,53 @@ class MachineAddController(AuthenticatedRestController):
def post(self, residence, member_uid, name, mac): def post(self, residence, member_uid, name, mac):
residence_dn = Residences.get_dn_by_name(self.user, residence) residence_dn = Residences.get_dn_by_name(self.user, residence)
#TODO : néttoyer mac (utiliser deux-points) et vérifier (regex) #Vérification que l'adresse mac soit correcte
# XX:XX:XX:XX:XX mac_match = re.match('^([0-9A-Fa-f]{2}[:-]?){5}([0-9A-Fa-f]{2})$', mac)
if mac_match is None:
#TODO : changer l'exception en une page d'erreur
raise Exception("mac non valide")
#endif
#Remplacement de l'adresse mac non séparée
mac_match = re.match('^([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$', mac)
if mac_match is not None:
mac = mac_match.group(1) + ":" + mac_match.group(2) + ":" + mac_match.group(3) + ":" + mac_match.group(4) + ":" + mac_match.group(5) + ":" + mac_match.group(6)
#endif
#Remplacement de l'adresse mac séparée par des tirets
mac_match = re.match('^([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})$', mac)
if mac_match is not None:
mac = mac_match.group(1) + ":" + mac_match.group(2) + ":" + mac_match.group(3) + ":" + mac_match.group(4) + ":" + mac_match.group(5) + ":" + mac_match.group(6)
#endif
#Passage au format lowercase
mac = mac.lower()
# Vérification que le membre existe # Vérification que le membre existe
member = Member.get_by_uid(self.user, residence_dn, member_uid) member = Member.get_by_uid(self.user, residence_dn, member_uid)
if member is None: if member is None:
#TODO : membre inexistant #TODO : membre inexistant
pass pass
#endif
# Vérification que l'adresse mac de la machine n'existe pas déjà
# Note : on cherche sur toute la résidence (residence_dn)
machine = Machine.get_dhcp_by_mac(self.user, residence_dn, mac)
if machine is not None:
#TODO : gérer l'exception
raise Exception("mac address already exist")
#endif
# Vérification que le nom de machine n'existe pas déjà # Vérification que le nom de machine n'existe pas déjà
# Note : on cherche sur toute la résidence (residence_dn) # Note : on cherche sur toute la résidence (residence_dn)
machine = Machine.get_dns_by_name(self.user, residence_dn, name)
# TODO :
# machine = Machine.get_machine_by_name(self.user, residence_dn, name)
machine = None
if machine is not None: if machine is not None:
#TODO : erreur machine existe déjà #TODO : gérer l'exception
pass raise Exception("dns name already exist")
#endif
# Génération de l'id de la machine et recherche d'une ip libre # Génération de l'id de la machine et recherche d'une ip libre
machine_id = str(uuid.uuid4()) machine_id = str(uuid.uuid4())
ip = IpReservation.get_first_free(self.user, residence_dn) ip = IpReservation.get_first_free(self.user, residence_dn)

View File

@ -179,6 +179,15 @@ class Machine(object):
return user_session.ldap_bind.search_first(machine_dn, "(objectClass=dlzAbstractRecord)") return user_session.ldap_bind.search_first(machine_dn, "(objectClass=dlzAbstractRecord)")
#end def #end def
@staticmethod
def get_dhcp_by_mac(user_session, residence_dn, mac):
return user_session.ldap_bind.search_first(residence_dn, "(dhcpHWAddress=ethernet "+mac+")")
#end def
@staticmethod
def get_dns_by_name(user_session, residence_dn, name):
return user_session.ldap_bind.search_first(residence_dn, "(dlzHostName="+name+")")
#end def
#end class #end class
class Groupes(object): class Groupes(object):

View File

@ -15,7 +15,7 @@
</div> </div>
<div py:choose="room_ldap"> <div py:choose="room_ldap">
<span class="section_name" py:when="None">Pas de chambre associée</span> <span class="section_name" py:when="None">Pas de chambre associée</span>
<div py:otherwise="">${room_view(room_ldap)}</div> <div py:otherwise="">${room_view(room_ldap,rooms)}</div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -14,6 +14,14 @@
<div class="section"> <div class="section">
<span class="section_name show_section_name">${member_ldap.cn.first()}</span> <span class="section_name show_section_name">${member_ldap.cn.first()}</span>
<div> <div>
<div>
<span class="item_name">Prénom</span>
<input type="text" value="${member_ldap.givenName.first('')}" />
</div>
<div>
<span class="item_name">Nom</span>
<input type="text" value="${member_ldap.sn.first()}" />
</div>
<div> <div>
<span class="item_name">Fin de connexion</span> <span class="item_name">Fin de connexion</span>
<span py:choose="member_ldap.get('x-connectionEnd')"> <span py:choose="member_ldap.get('x-connectionEnd')">
@ -66,8 +74,8 @@
</div> </div>
<div> <div>
<form action="/edit/machine/add/" method="post"> <form action="/edit/machine/add/" method="post">
<input type="text" name="name" class="item_name"/> <input type="text" name="name" placeholder="nom de la machine" class="item_name"/>
<input type="text" name="mac"/> <input type="text" name="mac" placeholder="adresse mac" />
<input type="hidden" name="residence" value="${residence}"/> <input type="hidden" name="residence" value="${residence}"/>
<input type="hidden" name="member_uid" value="${member_ldap.uid.first()}"/> <input type="hidden" name="member_uid" value="${member_ldap.uid.first()}"/>
<input type="submit" value="ajouter" class="button"/> <input type="submit" value="ajouter" class="button"/>
@ -76,14 +84,29 @@
</div> </div>
</py:def> </py:def>
<py:def function="room_view(room, member_in = None)"> <py:def function="room_view(room, rooms, member_in = None)">
<div class="section"> <div class="section">
<span class="section_name show_section_name">CHAMBRE ${room.cn.first()}</span> <span class="section_name show_section_name">CHAMBRE ${room.cn.first()}</span>
<div> <div>
<div py:if="member_in is not None"> <div py:if="member_in is not None">
<span class="item_name">Membre</span> <span class="item_name">Membre</span>
<span> <span>
<a href="/show/member/${member_in.uid.first()}">${member_in.cn.first()}</a> <a href="/show/member/${residence}/${member_in.uid.first()}">${member_in.cn.first()}</a>
</span>
</div>
<div>
<span class="item_name">Changer de chambre</span>
<select name="rooms">
<option value="">&lt;vide&gt;</option>
<py:for each="target_room in rooms">
<py:choose test="target_room.cn.first() == room.cn.first()">
<option py:when="True" selected="selected" value="${target_room.uid.first()}">${target_room.cn.first()}</option>
<option py:when="False" value="${target_room.uid.first()}">${target_room.cn.first()}</option>
</py:choose>
</py:for>
</select>
<span>
<a href="#" class="button">Sauvegarder le changement de chambre</a>
</span> </span>
</div> </div>
<div> <div>

View File

@ -9,7 +9,7 @@
</head> </head>
<body> <body>
${edit_banner()} ${edit_banner()}
${back_show("room", room_ldap.uid.first())} ${back_show("room", None, room_ldap.uid.first())}
<div py:choose="room_ldap"> <div py:choose="room_ldap">
<span class="section_name" py:when="None">Pas de chambre associée</span> <span class="section_name" py:when="None">Pas de chambre associée</span>
<div py:otherwise="">${room_view(room_ldap, member_ldap)}</div> <div py:otherwise="">${room_view(room_ldap, member_ldap)}</div>

View File

@ -5,7 +5,7 @@
<py:def function="back_rooms(preview_room_ldap = None)"> <py:def function="back_rooms(preview_room_ldap = None)">
<div py:choose="preview_room_ldap"> <div py:choose="preview_room_ldap">
<a py:when="None" href="/rooms/index/${residence}/" class="button">RETOUR LIST</a> <a py:when="None" href="/rooms/index/${residence}/" class="button">RETOUR LIST</a>
<a py:otherwise="" href="/rooms/preview/${residence}/${preview_room_ldap.cn.first()}" class="button">RETOUR PREVIEW</a> <a py:otherwise="" href="/rooms/preview/${residence}/${preview_room_ldap.uid.first()}" class="button">RETOUR PREVIEW</a>
</div> </div>
</py:def> </py:def>
<py:def function="member_view(member_ldap)"> <py:def function="member_view(member_ldap)">