ajout du clone d'élément LDAP + déménagement inter-résidence - TESTE MAIS PAS PRODUCTION-PROOF

This commit is contained in:
Romain Beuque 2014-02-16 00:13:25 +01:00
parent fe3631a894
commit acb77016f2
6 changed files with 250 additions and 19 deletions

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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:

View File

@ -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

View File

@ -36,6 +36,24 @@
<input type="text" name="name"/>
<input type="submit" value="Search" class="button"/>
</form>
<form action="/search/member_global/" method="post" class="inline_block">
<input type="hidden" name="myresidence" value="${residence}"/>
<input type="text" name="name"/>
<input type="submit" value="Search global" class="button"/>
</form>
</div>
<div class="section">
<span class="section_name show_section_name">Email</span>
<form action="/search/email/" method="post" class="inline_block">
<input type="hidden" name="residence" value="${residence}"/>
<input type="text" name="email"/>
<input type="submit" value="Search" class="button"/>
</form>
<form action="/search/email_global/" method="post" class="inline_block">
<input type="hidden" name="myresidence" value="${residence}"/>
<input type="text" name="email"/>
<input type="submit" value="Search global" class="button"/>
</form>
</div>
<div class="section">
<form action="/edit/member/disconnectall/" method="post" class="inline_block">