Commit 5aa56fe1 authored by Recteur LP's avatar Recteur LP

fix python2 support

parent 89b1793e
......@@ -43,7 +43,7 @@ class auth():
password=self.config['password']
)
else:
self.ld = ldap.initialize('ldap://' + self.config['server'] + ':' + self.config['port'])
self.ld = ldap.initialize('ldap://' + self.config['server'] + ':' + str(self.config['port']))
self.ld.protocol_version = self.config['version']
self.ld.set_option(ldap.OPT_REFERRALS, 0)
self.ld.simple_bind_s(self.config['user'], self.config['password'])
......@@ -57,7 +57,6 @@ class auth():
def __getresults(self, filter):
if sys.version_info >= (3,0):
pyrmin.log.debug(filter)
self.connection.search(
search_base=self.config['basedn'],
search_filter=filter,
......@@ -86,27 +85,56 @@ class auth():
def getusersfromgroup(self, group):
users = []
if sys.version_info >= (3,0):
for entry in self.__getresults(group):
if "attributes" in entry:
attrs = entry['attributes']
users.append({ "name": attrs.get('sAMAccountName', [ 'Guest' ])[0], "uid": attrs.get('uidNumber', [ 'None' ])[0] })
else:
for dn, attrs in self.__getresults(group):
users.append({ "name": attrs.get('sAMAccountName', [ 'Guest' ])[0], "uid": attrs.get('uidNumber', [ 'None' ])[0] })
return users
def get(self, name):
#filter = "(&(cn=" + name + ")(!(objectClass=computer)))"
if sys.version_info >= (3,0):
filter = "(&(samAccountName=" + name + "))"
for entry in self.__getresults(filter):
if "attributes" in entry:
attrs = entry['attributes']
name = attrs.get('sAMAccountName', [ 'Guest' ])[0]
if sys.version_info >= (3,0):
mail = str(attrs.get('mail')[0]).lower()
gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(mail.encode('utf-8')).hexdigest() + "?"
gravatar_url += urllib.parse.urlencode({'d':"identicon", 's':str(50)})
division = attrs.get('division', [ 'Guest' ])[0]
displayname = attrs.get('displayName', [ 'Guest' ])[0]
company = attrs.get('company', [ '' ])[0]
isadmin = False
admins = []
for group in self.config['admin']:
admins = self.getusersfromgroup("(&(memberOf=" + group + "))")
for admin in admins:
pyrmin.log.debug(admin)
if 'name' in admin and name.lower() == admin['name'].lower():
isadmin = True
break
if isadmin:
break
return {
"username": name,
"auth": True,
"admin": isadmin,
"division": division,
"displayname": displayname,
"mobile": attrs.get('otherMobile', [ '' ])[0],
"company": company,
"mail": mail,
"img": gravatar_url
}
else:
filter = "(&(cn=" + name + ")(!(objectClass=computer)))"
for dn, attrs in self.__getresults(filter):
name = attrs.get('sAMAccountName', [ 'Guest' ])[0]
mail = str(attrs.get('mail')[0]).decode('utf-8').lower()
gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(mail).hexdigest() + "?"
gravatar_url += urllib.urlencode({'d':"identicon", 's':str(50)})
......@@ -125,7 +153,6 @@ class auth():
break
if isadmin:
break
return {
"username": name,
"auth": True,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment