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

fix python2 support

parent 89b1793e
...@@ -43,7 +43,7 @@ class auth(): ...@@ -43,7 +43,7 @@ class auth():
password=self.config['password'] password=self.config['password']
) )
else: 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.protocol_version = self.config['version']
self.ld.set_option(ldap.OPT_REFERRALS, 0) self.ld.set_option(ldap.OPT_REFERRALS, 0)
self.ld.simple_bind_s(self.config['user'], self.config['password']) self.ld.simple_bind_s(self.config['user'], self.config['password'])
...@@ -57,7 +57,6 @@ class auth(): ...@@ -57,7 +57,6 @@ class auth():
def __getresults(self, filter): def __getresults(self, filter):
if sys.version_info >= (3,0): if sys.version_info >= (3,0):
pyrmin.log.debug(filter)
self.connection.search( self.connection.search(
search_base=self.config['basedn'], search_base=self.config['basedn'],
search_filter=filter, search_filter=filter,
...@@ -86,27 +85,56 @@ class auth(): ...@@ -86,27 +85,56 @@ class auth():
def getusersfromgroup(self, group): def getusersfromgroup(self, group):
users = [] users = []
for entry in self.__getresults(group): if sys.version_info >= (3,0):
if "attributes" in entry: for entry in self.__getresults(group):
attrs = entry['attributes'] 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] }) users.append({ "name": attrs.get('sAMAccountName', [ 'Guest' ])[0], "uid": attrs.get('uidNumber', [ 'None' ])[0] })
return users return users
def get(self, name): def get(self, name):
#filter = "(&(cn=" + name + ")(!(objectClass=computer)))" if sys.version_info >= (3,0):
filter = "(&(samAccountName=" + name + "))" filter = "(&(samAccountName=" + name + "))"
for entry in self.__getresults(filter): for entry in self.__getresults(filter):
if "attributes" in entry: if "attributes" in entry:
attrs = entry['attributes'] attrs = entry['attributes']
name = attrs.get('sAMAccountName', [ 'Guest' ])[0] name = attrs.get('sAMAccountName', [ 'Guest' ])[0]
if sys.version_info >= (3,0):
mail = str(attrs.get('mail')[0]).lower() mail = str(attrs.get('mail')[0]).lower()
gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(mail.encode('utf-8')).hexdigest() + "?" gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(mail.encode('utf-8')).hexdigest() + "?"
gravatar_url += urllib.parse.urlencode({'d':"identicon", 's':str(50)}) gravatar_url += urllib.parse.urlencode({'d':"identicon", 's':str(50)})
division = attrs.get('division', [ 'Guest' ])[0] division = attrs.get('division', [ 'Guest' ])[0]
displayname = attrs.get('displayName', [ 'Guest' ])[0] displayname = attrs.get('displayName', [ 'Guest' ])[0]
company = attrs.get('company', [ '' ])[0] company = attrs.get('company', [ '' ])[0]
else:
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() mail = str(attrs.get('mail')[0]).decode('utf-8').lower()
gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(mail).hexdigest() + "?" gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(mail).hexdigest() + "?"
gravatar_url += urllib.urlencode({'d':"identicon", 's':str(50)}) gravatar_url += urllib.urlencode({'d':"identicon", 's':str(50)})
...@@ -114,26 +142,25 @@ class auth(): ...@@ -114,26 +142,25 @@ class auth():
displayname = attrs.get('displayName', [ 'Guest' ])[0].decode("utf-8") displayname = attrs.get('displayName', [ 'Guest' ])[0].decode("utf-8")
company = attrs.get('company', [ '' ])[0].decode("utf-8") company = attrs.get('company', [ '' ])[0].decode("utf-8")
isadmin = False isadmin = False
admins = [] admins = []
for group in self.config['admin']: for group in self.config['admin']:
admins = self.getusersfromgroup("(&(memberOf=" + group + "))") admins = self.getusersfromgroup("(&(memberOf=" + group + "))")
for admin in admins: for admin in admins:
pyrmin.log.debug(admin) pyrmin.log.debug(admin)
if 'name' in admin and name.lower() == admin['name'].lower(): if 'name' in admin and name.lower() == admin['name'].lower():
isadmin = True isadmin = True
break break
if isadmin: if isadmin:
break break
return {
return { "username": name,
"username": name, "auth": True,
"auth": True, "admin": isadmin,
"admin": isadmin, "division": division,
"division": division, "displayname": displayname,
"displayname": displayname, "mobile": attrs.get('otherMobile', [ '' ])[0],
"mobile": attrs.get('otherMobile', [ '' ])[0], "company": company,
"company": company, "mail": mail,
"mail": mail, "img": gravatar_url
"img": gravatar_url }
} \ No newline at end of file
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