debut gestion des groupes

This commit is contained in:
Roven Gabriel 2012-10-28 14:06:01 +01:00
parent ac4c4f1a1e
commit 80e196ccaa
11 changed files with 101 additions and 40 deletions

View File

@ -7,6 +7,9 @@ base_dn = "dc=pacaterie,dc=u-psud,dc=fr"
username_base_dn = "ou=membres," + base_dn
room_base_dn = "ou=chambres," + base_dn
group_base_dn = "ou=groupes," + base_dn
area_filter = "(objectClass=pacateriearea)"
floor_filter = "(objectClass=pacateriefloor)"
room_filter = "(objectClass=pacaterieRoom)"

View File

@ -7,14 +7,37 @@ from tg.decorators import expose, validate
from brie.lib.base import BaseController
from brie.config import ldap_config
from brie.lib.ldap_helper import *
from brie.model.ldap import Groupes
class Groups(object):
__groups = list()
def __init__(self, groups):
self.__groups = groups
#end def
def __getattr__(self, name):
return name in self.__groups
#end def
def list(self):
return list(self.__groups)
#end def
#end class
class User(object):
ldap_bind = None
attrs = None
groups = None
def __init__(self, ldap_bind, attrs):
self.ldap_bind = ldap_bind
self.attrs = attrs
groups = Groupes.get_by_user_dn(self, self.attrs.dn)
self.groups = Groups(groups)
#end def
#end class

View File

@ -57,7 +57,7 @@ class EditController(AuthenticatedBaseController):
# message = str(ex)
#end try
return {"room_number" : room_number, "success" : success, "message" : message }
return { "room_number" : room_number, "success" : success, "message" : message }
#end def
#end class

View File

@ -49,14 +49,5 @@ class RootController(BaseController):
user = auth_handler.current.get_user()
return { "user" : user, "materiel" : materiel }
@expose()
def foobar(self):
redirect("http://172.17.22.10:9000/toto")
#end def
# @expose('brie.templates.index')
# @require(predicates.has_permission('manage', msg=l_('Only for managers')))
# def manage_permission_only(self, **kw):
# """Illustrate how a page for managers only works."""
# return dict(page='managers stuff')
#end class

View File

@ -33,15 +33,21 @@ class ShowController(AuthenticatedBaseController):
return self.error_no_entry()
room = Room.get_by_member_dn(self.user, member.dn)
interface = (
DBSession.query(Interface)
.filter(Interface.idinterface == room.get("x-switchInterface").first())
.first()
)
interface = None
if room is not None:
interface = (
DBSession.query(Interface)
.filter(Interface.idinterface == room.get("x-switchInterface").first())
.first()
)
#end if
machines = Machine.get_machines_of_member(self.user, member.dn)
groups = Groupes.get_by_user_dn(self.user, member.dn)
return { "member_ldap" : member, "interface" : interface, "room_ldap" : room, "machines" : machines}
return { "user" : self.user, "member_ldap" : member, "interface" : interface, "room_ldap" : room, "machines" : machines, "groups" : groups}
#end def
@expose("brie.templates.show.room")
@ -61,7 +67,7 @@ class ShowController(AuthenticatedBaseController):
if room.has("x-memberIn"):
member = Member.get_by_dn(self.user, room.get("x-memberIn").first())
return { "interface" : interface, "room_ldap" : room, "member_ldap" : member }
return { "user" : self.user, "interface" : interface, "room_ldap" : room, "member_ldap" : member }
#end def
@expose("brie.templates.show.interface")
@ -77,7 +83,7 @@ class ShowController(AuthenticatedBaseController):
room = Room.get_by_interface(self.user, interface.idinterface)
return { "interface" : interface, "room_ldap" : room }
return { "user" : self.user, "interface" : interface, "room_ldap" : room }
#end def
#end class

View File

@ -14,11 +14,11 @@ class Ldap(object):
@staticmethod
def connect(dn, password):
connection = None
try:
connection = ldap.initialize(ldap_config.uri)
connection.simple_bind_s(dn, password)
except:
return None
# try:
connection = ldap.initialize(ldap_config.uri)
connection.simple_bind_s(dn, password)
# except:
# return None
#end try
if connection is not None:
@ -29,10 +29,10 @@ class Ldap(object):
#end def
def search(self, dn, filter, scope = ldap.SCOPE_SUBTREE):
try:
results = self.__connection.search_s(dn, scope, filter)
except:
return None
# try:
results = self.__connection.search_s(dn, scope, filter)
# except:
# return None
#end try
ldap_results = []

View File

@ -134,7 +134,7 @@ class Machine(object):
dns = user_session.ldap_bind.search_first(result.dn, "(objectClass=dlzGenericRecord)")
if dhcp is not None and dns is not None:
mac = dhcp.dhcpHWAddress.first().replace("ethernet ", "")
machines.append((dhcp.cn.first(), mac, dns.dlzData.first()))
machines.append((dhcp.cn.first(), mac, dns.dlzData.first())) # tuple
#end if
#end for
@ -143,3 +143,19 @@ class Machine(object):
#end class
class Groupes(object):
@staticmethod
def get_by_user_dn(user_session, user_dn):
results = user_session.ldap_bind.search(ldap_config.group_base_dn, "(&(objectClass=groupOfUniqueNames)(uniqueMember=" + user_dn + "))")
groups = list()
for item in results:
groups.append(item.cn.first())
#end for
return groups
#end
#end class

View File

@ -50,9 +50,6 @@
</span>
</py:with>
</div>
<div>
<span class="item_name">Wifi</span>
</div>
</div>
</div>
<div class="section">

View File

@ -1,13 +1,33 @@
<html xmlns:py="http://genshi.edgewall.org/" xmlns:xi="http://www.w3.org/2001/XInclude">
<head>
<link rel="stylesheet" type="text/css" href="/css/common.css" />
<link rel="stylesheet" type="text/css" href="/css/show.css" />
</head>
<body>
<xi:include href="login_widget.html" />
<div>${login_widget(user)}</div>
<div>
<a href="/rooms/" class="button">CHAMBRES</a>
<div py:if="user is not None">
<div class="section">
<a py:if="user.groups.admin" class="section_name show_section_name button" href="/rooms/" >CHAMBRES</a>
</div>
<div class="section" >
<span class="section_name show_section_name">DEBUG</span>
<div>
<span class="item_name">user</span>
<span>${user.attrs.uid.first()}</span>
</div>
<div>
<span class="item_name">dn</span>
<span>${user.attrs.dn}</span>
</div>
<div>
<span class="item_name">groups</span>
<span>
<py:for each="group in user.groups.list()">${group} </py:for>
</span>
</div>
</div>
</div>
</body>
</html>

View File

@ -6,11 +6,11 @@
<py:def function="login_widget(user)">
<div py:choose="">
<div py:when="user is None">
<a href="${tg.url('/auth/login')}">Connection</a>
<a href="${tg.url('/auth/login')}">Connexion</a>
</div>
<div py:otherwise="">
<span>${user.attrs.cn.first()}</span>
<a href="${tg.url('/auth/logout')}">Deconnection</a>
<a href="${tg.url('/auth/logout')}">Deconnexion</a>
</div>
</div>
</py:def>

View File

@ -48,10 +48,6 @@
</span>
</py:with>
</div>
<div>
<span class="item_name">Groupes</span>
<span></span>
</div>
</div>
</div>
<div class="section">
@ -60,6 +56,15 @@
<span class="item_name">Identifiant</span>
<span>${member_ldap.uid.first()}</span>
</div>
<div>
<span class="item_name">Groupes</span>
<py:choose test="groups">
<span py:when="[]">aucun</span>
<span py:otherwise="">
<py:for each="group in groups">${group} </py:for>
</span>
</py:choose>
</div>
<div>
<span class="item_name">Uid unix</span>
<span>${member_ldap.uidNumber.first()}</span>