correction des bugs

This commit is contained in:
Roven Gabriel 2013-09-11 22:18:43 +02:00
parent b1cb14405c
commit 66e808f40f
6 changed files with 100 additions and 12 deletions

View File

@ -1,9 +1,13 @@
from brie.plugins.wifi.controller import *
from brie.plugins.unix.controller import *
from brie.plugins.macauth.controller import *
mappings = {
"brie.controllers.show.member" : [
("wifi", Wifi.show),
("unix", Unix.show)
],
"brie.controllers.edit.machine.post" : [
("macauth", Mac_auth.add_machine)
]
}

View File

@ -7,6 +7,7 @@ from tg.decorators import expose, validate
from brie.config import ldap_config
from brie.config import groups_enum
from brie.lib.ldap_helper import *
from brie.lib.plugins import *
from brie.lib.aurore_helper import *
from brie.model.ldap import *
from brie.lib.name_translation_helpers import Translations
@ -230,8 +231,11 @@ class MachineAddController(AuthenticatedRestController):
""" Fonction de gestion de requete post sur le controller d'ajout """
@expose()
def post(self, residence, member_uid, name, mac, go_redirect = True):
@plugin_action("brie.controllers.edit.machine.post")
def post(self, residence, member_uid, name, mac, go_redirect = True, plugin_action = None):
residence_dn = Residences.get_dn_by_name(self.user, residence)
member_base_dn = ldap_config.username_base_dn + residence_dn
member = Member.get_by_uid(self.user, member_base_dn, member_uid)
#Vérification que l'adresse mac soit correcte
mac_match = re.match('^([0-9A-Fa-f]{2}[:-]?){5}([0-9A-Fa-f]{2})$', mac)
@ -257,7 +261,6 @@ class MachineAddController(AuthenticatedRestController):
# Vérification que le membre existe
member = Member.get_by_uid(self.user, residence_dn, member_uid)
if member is None:
#TODO : membre inexistant
pass
@ -266,7 +269,7 @@ class MachineAddController(AuthenticatedRestController):
# 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)
machine = Machine.get_dhcp_by_mac(self.user, member_base_dn, mac)
if machine is not None:
#TODO : gérer l'exception
raise Exception("mac address already exist")
@ -283,7 +286,7 @@ class MachineAddController(AuthenticatedRestController):
actual_name = name + "-" + str(number)
#end if
machine = Machine.get_dns_by_name(self.user, residence_dn, actual_name)
machine = Machine.get_dns_by_name(self.user, member_base_dn, actual_name)
if machine is not None:
return try_name(name, number + 1)
else:
@ -322,13 +325,21 @@ class MachineAddController(AuthenticatedRestController):
# Construction du dn et ajout de l'objet dhcp
# en fils de la machine (machine_dn)
dhcp_dn = "cn=" + name + "," + machine_dn
dhcp_dn = "cn=dhcp," + machine_dn
self.user.ldap_bind.add_entry(dhcp_dn, machine_dhcp)
# Construction du dn et ajout de l'objet dns
dns_dn = "dlzHostName=" + name + "," + machine_dn
dns_dn = "cn=dns," + machine_dn
self.user.ldap_bind.add_entry(dns_dn, machine_dns)
plugin_vars = {
"machine_dn" : machine_dn,
"name" : name,
"ip" : ip,
"mac" : mac
}
plugin_action(self.user, residence, plugin_vars)
if go_redirect:
redirect("/edit/member/" + residence + "/" + member_uid)

View File

@ -84,3 +84,52 @@ def plugins(scope):
return decorator(plugin)
#end def
""" Decorateur plugin action, passe en paramettre de la fonction d'un plugin """
def plugin_action(scope):
def plugin(f, *args, **kw):
user = args[0].user
plugins_functions = []
# un plugin est defini pour le scope et residence est defini
if scope in plugins_config.mappings:
residence_dn = user.residence_dn
scope_mappings = plugins_config.mappings[scope]
for plugin_name, function in scope_mappings:
plugin_activated = Plugins.get_by_name(user, residence_dn, plugin_name)
if plugin_activated is None:
continue
#end if
# constuire le nom de regroupement des variable de ce plugin
method_name = function.__name__
plugin_section = str.lower(plugin_name + "_" + method_name)
# ajout du groupe au dictionnaire de la methode du controlleur
plugins_functions.append((plugin_name, lambda user, resid, models: PluginVars(function(user, resid, models))))
#end for
#end if
# ajout des templates dans un champs spécial du dictionnaire pour le rendu
def plugin_action(user, resid, models):
result_dict = dict()
for plugin_name, plugin_function in plugins_functions:
result_dict[plugin_name] = plugin_function(user, resid, models)
#end for
#end def
# on remplace la dernière variable par un plugin_action
new_args = args[:-1] + (plugin_action,)
return f(*new_args, **kw)
#end def
return decorator(plugin)
#end def

View File

@ -30,8 +30,8 @@ class Member(object):
#end def
@staticmethod
def get_by_uid(user_session, residence_dn, uid):
return user_session.ldap_bind.search_first(ldap_config.username_base_dn + residence_dn, "(uid=" + uid + ")")
def get_by_uid(user_session, member_base_dn, uid):
return user_session.ldap_bind.search_first(member_base_dn, "(uid=" + uid + ")")
#end def
@staticmethod
@ -206,13 +206,13 @@ class Machine(object):
#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+")")
def get_dhcp_by_mac(user_session, member_dn, mac):
return user_session.ldap_bind.search_first(member_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+")")
def get_dns_by_name(user_session, member_dn, name):
return user_session.ldap_bind.search_first(member_dn, "(dlzHostName="+name+")")
#end def
#end class

View File

View File

@ -0,0 +1,24 @@
from tg.decorators import expose
from brie.model.ldap import Machine
class Mac_auth:
@staticmethod
@expose("")
def add_machine(user, residence, models):
print user
print residence
print models
machine_dn = models["machine_dn"]
mac = models["mac"].replace(":", "")
name = models["name"]
mac_dn = "cn=mac_auth," + machine_dn
mac_attr = Machine.auth_attr(mac)
user.ldap_bind.add_entry(mac_dn, mac_attr)
return {}
#end def
#end class