Commit 0014084a authored by Recteur LP's avatar Recteur LP

Update Plugin

parents
__all__ = ['ldapauth2']
\ No newline at end of file
#!/usr/bin/python
# -*- coding: utf-8 -*-
import ldap
import ldif
import sys
import urllib
import hashlib
######## Custom LDAP / AD module #########
__name__ = "LDAP Authentification"
__description__ = "Module to authenticate user to ldap for python 2.x"
__version__ = "0.1"
__pyrmin_version__ = "0.2"
__namespace__ = "ldapauth2"
###################################
class ldapauth2():
"LDAP auth module"
def __init__(self, config):
# Get Auth method in config file if none set go to simgle user mode
if 'ldap' in config:
if 'server' not in config['ldap']:
returnerror("No ldap server in config")
elif 'port' not in config['ldap']:
returnerror("No ldap port in config")
self.config = config
self.ld = ldap.initialize('ldap://' + self.config['ldap']['server'] + ':' + self.config['ldap']['port'])
self.ld.protocol_version = config['ldap']['version']
self.ld.set_option(ldap.OPT_REFERRALS, 0)
self.ld.simple_bind_s(self.config['ldap']['user'], self.config['ldap']['password'])
def __del__(self):
self.ld.unbind_s()
def __getresults(self, filter):
results = self.ld.search_s(self.config['ldap']['basedn'],ldap.SCOPE_SUBTREE,filter)
return results
def __getgroup(self, group):
results = self.ld.search_s(group,ldap.SCOPE_SUBTREE)
return results
def getusersfromgroup(self, group):
users = []
for dn,attrs in self.__getresults(group):
if dn and attrs:
users.append({ "name": attrs.get('sAMAccountName', [ 'Guest' ])[0], "uid": attrs.get('uidNumber', [ 'None' ])[0] })
return users
def getuserinfo(self, name):
filter = "(&(cn=" + name + ")(!(objectClass=computer)))"
for dn,attrs in self.__getresults(filter):
if dn and attrs:
name = attrs.get('sAMAccountName', [ 'Guest' ])[0]
mail = str(attrs.get('mail')[0]).decode('utf-8').lower()
gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(mail).hexdigest() + "?"
gravatar_url += urllib.urlencode({'d':"identicon", 's':str(50)})
isadmin = False
admins = self.getusersfromgroup("memberOf=" + self.config['ldap']['admin'])
for admin in admins:
if name.lower() == admin['name'].lower():
isadmin = True
break
return {
"name": name,
"isauth": True,
"isadmin": isadmin,
"division": unicode(attrs.get('division', [ 'Guest' ])[0], "utf-8"),
"displayName": attrs.get('displayName', [ 'Guest' ])[0],
"mobile": attrs.get('otherMobile', [ '' ])[0],
"company": attrs.get('company', [ '' ])[0],
"mail": mail,
"img_url": gravatar_url
}
\ No newline at end of file
<div class="form-group">
<label for="exampleInputEmail1">Authentification module</label>
<input type="text" class="form-control" id="auth" value="{{ config.auth_module }}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">LDAP Username</label>
<input type="text" class="form-control" id="username" value="{{ config.ldap.user }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">LDAP Password</label>
<input type="password" class="form-control" id="password" value="{{ config.ldap.password }}">
</div>
<div class="form-group">
<label for="exampleInputFile">Admin Group</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
<p class="help-block">Example block-level help text here.</p>
</div>
\ 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