From acb77016f29742594a56fdd27ef885a04aa3317b Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Sun, 16 Feb 2014 00:13:25 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20du=20clone=20d'=C3=A9l=C3=A9ment=20LDAP?= =?UTF-8?q?=20+=20d=C3=A9m=C3=A9nagement=20inter-r=C3=A9sidence=20-=20TEST?= =?UTF-8?q?E=20MAIS=20PAS=20PRODUCTION-PROOF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Brie/brie/controllers/members.py | 114 ++++++++++++++++++++++++++----- Brie/brie/controllers/root.py | 2 +- Brie/brie/controllers/search.py | 93 +++++++++++++++++++++++++ Brie/brie/lib/ldap_helper.py | 37 ++++++++++ Brie/brie/model/ldap.py | 5 ++ Brie/brie/templates/index.html | 18 +++++ 6 files changed, 250 insertions(+), 19 deletions(-) diff --git a/Brie/brie/controllers/members.py b/Brie/brie/controllers/members.py index 554444b..d9196ba 100644 --- a/Brie/brie/controllers/members.py +++ b/Brie/brie/controllers/members.py @@ -9,7 +9,7 @@ from brie.lib.ldap_helper import * from brie.lib.aurore_helper import * from brie.model.ldap import * -import datetime +from datetime import datetime from brie.controllers import auth from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController @@ -17,10 +17,16 @@ from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRest class MembersController(AuthenticatedBaseController): require_group = groups_enum.admin - @staticmethod + member_edit_controller = None + + def __init__(self, member_edit_controller): + self.member_edit_controller = member_edit_controller + + @staticmethod def sort_name(name_items): - return sorted(name_items, key=lambda t:t.sn.first()) - + return sorted(name_items, key=lambda t:t.sn.first()) + #end if + @expose("brie.templates.search.member") def index(self, residence_name): residence_dn = Residences.get_dn_by_name(self.user, residence_name) @@ -29,22 +35,22 @@ class MembersController(AuthenticatedBaseController): #end if members = Member.get_all(self.user, residence_dn) - members = MembersController.sort_name(members) + members = MembersController.sort_name(members) - members_rooms = [ - (member, Room.get_by_member_dn(self.user, residence_dn, member.dn)) - for member in members - ] + members_rooms = [ + (member, Room.get_by_member_dn(self.user, residence_dn, member.dn)) + for member in members + ] - # machines = Machine.get_machine_tuples_of_member(self.user, member.dn) - - # groups = Groupes.get_by_user_dn(self.user, residence_dn, member.dn) + # machines = Machine.get_machine_tuples_of_member(self.user, member.dn) - return { - "residence" : residence_name, - "user" : self.user, - "members_rooms" : members_rooms - } + # groups = Groupes.get_by_user_dn(self.user, residence_dn, member.dn) + + return { + "residence" : residence_name, + "user" : self.user, + "members_rooms" : members_rooms + } #end def @@ -53,6 +59,78 @@ class MembersController(AuthenticatedBaseController): # responses = index(self, residence_name) # retu - + @expose() + def demenageResidence(self, member_dn): + # Initialisation des Users des Controllers Existant appelles + self.member_edit_controller.machine.add.user = self.user + self.member_edit_controller.add.user = self.user + self.member_edit_controller.cotisation.add.user = self.user + + residence_dn = self.user.residence_dn + residence_name = Residences.get_name_by_dn(self.user, residence_dn) + + member = Member.get_by_dn(self.user, member_dn) + + machines = Machine.get_machine_tuples_of_member(self.user, member_dn) + + current_year = CotisationComputes.current_year() + cotisations = Cotisation.cotisations_of_member(self.user, member_dn, current_year) + + now = datetime.now() + registration_year = 0 + + if now.month >= 8: + registration_year = now.year + else: + registration_year = now.year - 1 + #endif + + member_dn = "uid=" + member.uid.first() + ",ou=" + str(registration_year) + "," + ldap_config.username_base_dn + residence_dn + #phone = ' ' + #if member.has('mobile'): + # phone = member.mobile.first() + #member_uid = self.member_edit_controller.add.post(residence_name, member.givenName.first(), member.sn.first(), member.mail.first(), phone, False) + + self.user.ldap_bind.clone_entry(member_dn, member) + member = Member.get_by_uid(self.user, self.user.residence_dn, member.uid.first()) + + folder_dn = ldap_config.cotisation_member_base_dn + member.dn + year_dn = "cn=" + str(current_year) + "," + folder_dn + + try: + folder = Cotisation.folder_attr() + self.user.ldap_bind.add_entry(folder_dn, folder) + except ldap.ALREADY_EXISTS: + pass # OKAY + #end try + + try: + year = Cotisation.year_attr(current_year) + self.user.ldap_bind.add_entry(year_dn, year) + except ldap.ALREADY_EXISTS: + pass # OKAY + #end try + + for cotisation in cotisations: + cotisation_dn = "" + if cotisation.has("description") and cotisation.description.first() == "cotisation": + cotisation_dn = "cn=cotisation-" + else: + cotisation_dn = "cn=extra-" + #end if + + cotisation_dn = cotisation_dn + cotisation.get("x-time").first() + "," + year_dn + self.user.ldap_bind.clone_entry(cotisation_dn, cotisation) + #end for + + for machine_name, machine_mac, machine_ip, machine_disabled in machines: + self.member_edit_controller.machine.add.post(residence_name, member.uid.first(), machine_name, machine_mac, go_redirect = False) + #end for + + + redirect("/show/member/" + residence_name + "/" + member.uid.first()) + + #end def +#end class \ No newline at end of file diff --git a/Brie/brie/controllers/root.py b/Brie/brie/controllers/root.py index 9fe694d..eb57ad8 100644 --- a/Brie/brie/controllers/root.py +++ b/Brie/brie/controllers/root.py @@ -50,9 +50,9 @@ class RootController(BaseController): auth = AuthRestController() rooms = RoomsController() - members = MembersController() show = ShowController() edit = EditController(show) + members = MembersController(edit) administration = AdministrationController() stats = StatsController() getemails = GetEmailsController() diff --git a/Brie/brie/controllers/search.py b/Brie/brie/controllers/search.py index ff5e282..6c052a0 100644 --- a/Brie/brie/controllers/search.py +++ b/Brie/brie/controllers/search.py @@ -49,6 +49,99 @@ class SearchController(AuthenticatedBaseController): } #end def + + @expose("brie.templates.search.member") + def email(self, residence, email): + residence_dn = Residences.get_dn_by_name(self.user, residence) + members = Member.get_by_email(self.user, residence_dn, email) + + if members is None: + return self.error_no_entry() + + members = MembersController.sort_name(members) + + members_rooms = [ + (member, Room.get_by_member_dn(self.user, residence_dn, member.dn)) + for member in members + ] + + # machines = Machine.get_machine_tuples_of_member(self.user, member.dn) + + # groups = Groupes.get_by_user_dn(self.user, residence_dn, member.dn) + return { + "residence" : residence, + "user" : self.user, + "members_rooms" : members_rooms + } + #end def + + + @expose("brie.templates.search.member_global") + def member_global(self, myresidence, name): + residences = Residences.get_residences(self.user) + members = [] + for residence in residences: + residence_dn = Residences.get_dn_by_name(self.user, residence.cn.first()) + members_res = Member.get_by_name(self.user, residence_dn, name) + for member in members_res: + members.append((member, residence_dn)) + #end for + #end for + + if members is None: + return self.error_no_entry() + +# members = MembersController.sort_name(members) + + members_rooms = [ + (member, Room.get_by_member_dn(self.user, residence_dn, member.dn), Residences.get_name_by_dn(self.user, residence_dn)) + for member, residence_dn in members + ] + + # machines = Machine.get_machine_tuples_of_member(self.user, member.dn) + + # groups = Groupes.get_by_user_dn(self.user, residence_dn, member.dn) + return { + "residence" : myresidence, + "user" : self.user, + "members_rooms" : members_rooms + } + #end def + + + @expose("brie.templates.search.member_global") + def email_global(self, myresidence, email): + residences = Residences.get_residences(self.user) + members = [] + for residence in residences: + residence_dn = Residences.get_dn_by_name(self.user, residence.cn.first()) + members_res = Member.get_by_email(self.user, residence_dn, email) + for member in members_res: + members.append((member, residence_dn)) + #end for + #end for + + if members is None: + return self.error_no_entry() + +# members = MembersController.sort_name(members) + + members_rooms = [ + (member, Room.get_by_member_dn(self.user, residence_dn, member.dn), Residences.get_name_by_dn(self.user, residence_dn)) + for member, residence_dn in members + ] + + # machines = Machine.get_machine_tuples_of_member(self.user, member.dn) + + # groups = Groupes.get_by_user_dn(self.user, residence_dn, member.dn) + return { + "residence" : myresidence, + "user" : self.user, + "members_rooms" : members_rooms + } + #end def + + @expose("brie.templates.show.room") def room(self, residence, room_id): residence_dn = Residences.get_dn_by_name(self.user, residence) diff --git a/Brie/brie/lib/ldap_helper.py b/Brie/brie/lib/ldap_helper.py index b7e7895..c8b590e 100644 --- a/Brie/brie/lib/ldap_helper.py +++ b/Brie/brie/lib/ldap_helper.py @@ -143,6 +143,24 @@ class Ldap(object): ]) #end def + @staticmethod + def str_attributes_list(attributes): + def str_value(value): + if isinstance(value, list): + return [Ldap.str_attribute(subval) for subval in value] + elif isinstance(value, LdapAttribute): + return [Ldap.str_attribute(subval) for subval in value.all()] + #end if + + return Ldap.str_attribute(value) + #end def + + return dict([ + (keyval, str_value(attributes[keyval])) + for keyval in attributes + ]) + #end def + @staticmethod def str_attribute(value): if isinstance(value, str): @@ -214,7 +232,26 @@ class Ldap(object): for attribute in attributes.iteritems(): modlist.append((attribute[0], attribute[1])) #end for + + ##try: + self.__connection.add_s(dn, modlist) + ##except: + ## pass + #end def + """ Clone un élément + dn : adresse du nouvelle élément + attributes : l'élément à cloner + """ + def clone_entry(self, dn, ldap_entry): + attributes = Ldap.str_attributes_list(ldap_entry.__dict__) + del attributes['dn'] + + modlist = [] + for attribute in attributes.iteritems(): + modlist.append((attribute[0], attribute[1])) + #end for + ##try: self.__connection.add_s(dn, modlist) ##except: diff --git a/Brie/brie/model/ldap.py b/Brie/brie/model/ldap.py index d03720f..e7e1478 100644 --- a/Brie/brie/model/ldap.py +++ b/Brie/brie/model/ldap.py @@ -44,6 +44,11 @@ class Member(object): def get_by_name(user_session, residence_dn, name): return user_session.ldap_bind.search(ldap_config.username_base_dn + residence_dn, "(&(objectClass=pacatnetMember)(cn~=" + name + "))") #end def + + @staticmethod + def get_by_email(user_session, residence_dn, email): + return user_session.ldap_bind.search(ldap_config.username_base_dn + residence_dn, "(&(objectClass=pacatnetMember)(mail=" + email + "))") + #end def #end class diff --git a/Brie/brie/templates/index.html b/Brie/brie/templates/index.html index a720ca1..c33a8e1 100644 --- a/Brie/brie/templates/index.html +++ b/Brie/brie/templates/index.html @@ -36,6 +36,24 @@ +
+ + + +
+ +
+ Email +
+ + + +
+
+ + + +