Created
September 29, 2012 16:26
-
-
Save norrs/3804511 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Tiny LDAP Wrapper module""" | |
import simpleldap | |
class LDAP(object): | |
def __init__(self): | |
self.connection = None | |
self.search_base = None | |
self.protocol = None | |
self.encryption = None | |
def connect(self, hostname, port, search_base='', protocol='ldaps'): | |
"""Connect to LDAP server | |
""" | |
if self.connection: | |
return self.connection | |
if not hostname: | |
raise ValueError('Requires a hostname to connect to') | |
if not port or (port and (port <= 0 or port > 65535)): | |
raise ValueError('Requires a valid port number.') | |
if port == 636: | |
self.protocol = 'ldaps' | |
elif protocol: | |
self.protocol = protocol | |
self.hostname = hostname | |
self.port = port | |
self.search_base = search_base | |
if self.protocol == 'ldapi': | |
self.encryption = None | |
elif self.protocol == 'ldaps': | |
self.encryption = 'ssl' | |
else: | |
self.encryption = 'tls' | |
self.connect = simpleldap.Connection(self.hostname, self.port, '', '', self.encryption ) | |
return self.connect | |
def is_connected(self): | |
"""are we connected to LDAP server? | |
:returns true if we are. | |
""" | |
return self.connect is not None | |
def get_connection(self): | |
return self.connect | |
def _get_search_results(self, filter, base_dn, attributes): | |
return self.get_connection().search(filter, base_dn, attributes) | |
def get_users(self, search_options=None): | |
"""Get users from LDAP | |
:search_options may contain search parameters as: | |
BaseDN, isUserSubTree?, ObjectClass required, UserFilter | |
UserIDAttribute, RealNameAttribute, EmailAttribute | |
""" | |
#base_dn_users = 'ou=people' | |
base_dn_users = self.search_base | |
#filter_object_class = '(objectClass=inetOrgPerson)' | |
filter_object_class = '(objectClass=posixAccount)' | |
attribute_user_id = 'uid' | |
attribute_real_name = 'displayName' | |
attribute_email = 'mail' | |
if self.is_connected(): | |
return self._get_search_results(filter_object_class, base_dn_users, None) | |
else: | |
raise simpleldap.ConnectionException('You need to be connected') | |
def get_groups(self, username, search_options=None): | |
"""Get group given user name | |
:username groupmembership for given username | |
:search_options search meta for group fetching: | |
GroupTye (Static|Dynamic), BaseDN, Group Subtree, | |
ObjectClass, GroupIDAttribute, GroupMemberAttribute, | |
GroupMemeberFormat""" | |
is_group_type_static = True | |
base_dn_group = 'ou=groups' | |
filter_object_class = 'posixGroup' | |
attribute_group_id = 'cn' | |
attribute_member = 'memberUid' | |
if self.is_connected(): | |
return self.get_connection().search(filter_object_class, base_dn_group, | |
[attribute_group_id, attribute_member]) | |
else: | |
raise simpleldap.ConnectionException('You need to be connected') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment