Added LDAP (AD) address book support

This commit is contained in:
svk28 2017-11-08 11:39:25 +03:00
parent 97a8bbacb7
commit e342936402

View File

@ -3,7 +3,7 @@ import os, sys, pymysql, re, time
from ldap3 import Server, Connection, NTLM from ldap3 import Server, Connection, NTLM
def application(environ, start_response): def application(environ, start_response):
#sys.stdout._encoding = 'utf-8' #sys.stdout._encoding = 'utf-8'замена сцепления патриот стоимость работ смоленск
status = '200 OK' status = '200 OK'
# проверяем наличие параметров в запросе и всяко-разно реагируем # проверяем наличие параметров в запросе и всяко-разно реагируем
if environ['QUERY_STRING'] == '': if environ['QUERY_STRING'] == '':
@ -17,7 +17,9 @@ def application(environ, start_response):
elif paramDict.get('query_type') == 'report': elif paramDict.get('query_type') == 'report':
output = bytes((header() + body() + ReportForm() + ReportData(environ) + footer()).encode('utf8')) output = bytes((header() + body() + ReportForm() + ReportData(environ) + footer()).encode('utf8'))
elif paramDict.get('query_type') == 'ldap': elif paramDict.get('query_type') == 'ldap':
output = bytes((header() + body() + getLDAPusers() + footer()).encode('utf8')) domain = paramDict.get('domain')
group = paramDict.get('group')
output = bytes((header() + body() + getLDAPusers(domain, group) + footer()).encode('utf8'))
else: else:
output = bytes((header() + body() + footer()).encode('utf8')) output = bytes((header() + body() + footer()).encode('utf8'))
@ -48,14 +50,15 @@ def menu():
txtMenu = '<div class="container"><ul id="nav"></li>\n' \ txtMenu = '<div class="container"><ul id="nav"></li>\n' \
'<li><a class="hsubs" href="#">Справочник</a><ul class="subs">\n' \ '<li><a class="hsubs" href="#">Справочник</a><ul class="subs">\n' \
'<li><a href="?query_type=external">Городские телефоны</a></li>\n' \ '<li><a href="?query_type=external">Городские телефоны</a></li>\n' \
'<li><a href="?query_type=internal">Внутренние</a></li></ul></li>\n' \ '<li><a href="?query_type=ldap&domain=domain1">Список абонентов 1</a></li>\n' \
'<li><a href="?query_type=ldap">Внутренние (AD)</a></li></ul></li>\n' \ '<li><a href="?query_type=ldap&domain=domain2">Список абонентов 2</a></li>\n' \
'<li><a href="?query_type=ldap&domain=domain3">Список абонентов 3</a></li></ul></li>\n' \
'<li><a class="hsubs" href="">Отчёты</a><ul class="subs">' \ '<li><a class="hsubs" href="">Отчёты</a><ul class="subs">' \
'<li><a href="?query_type=report&report_type=int">Список звонков по номеру</a></li>\n' \ '<li><a href="?query_type=report&report_type=int">Список звонков по номеру</a></li>\n' \
'<li><a href="?query_type=report&report_type=dep">Звонки по отделам</a></li>\n' \ '<li><a href="?query_type=report&report_type=dep">Звонки по отделам</a></li>\n' \
'</ul></div>\n' '</ul></div>\n'
return txtMenu return txtMenu
# Список внутренних номеров из СУБД
def getInternalNumbers(): def getInternalNumbers():
conn = connectDB() conn = connectDB()
c = conn.cursor() c = conn.cursor()
@ -66,33 +69,65 @@ def getInternalNumbers():
listHeader = '<table class="table_dark"><tr><th>№ п/п</th>\n' \ listHeader = '<table class="table_dark"><tr><th>№ п/п</th>\n' \
'<th><a href=index.py?query_type=internal&order=int_number>Телефон</a></th>\n' \ '<th><a href=index.py?query_type=internal&order=int_number>Телефон</a></th>\n' \
'<th><a href=index.py?query_type=internal&order=fio>ФИО</a></th>\n' \ '<th><a href=index.py?query_type=internal&order=fio>ФИО</a></th>\n' \
'<th>Описание<th>Эл.почта</th></tr>\n' '<th>Описание<th>Электро-почта</th></tr>\n'
result = result + listHeader result = result + listHeader
for row in c.fetchall(): for row in c.fetchall():
rowData = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href=\"mailto:%s\">%s</a></td></tr>\n" % (row[0], row[1], row[2], row[4], row[3], row[3]) rowData = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n" % (row[0], row[1], row[2], row[4], row[3])
result = result + rowData result = result + rowData
return (result + "</table>\n") return (result + "</table>\n")
# Список пользователей (ФИО, телефон, почта, отдел) из LDAP (AD) # Список пользователей (ФИО, телефон, почта, отдел) из LDAP (AD)
def getLDAPusers(): def getLDAPusers(domain, group):
order = 'int_number' order = 'int_number'
result = '<h4 align=left>Список абонентов ЛТБ</h4>' if domain == 'domain1':
listHeader = '<table class="table_dark"><tr><th>№ п/п</th>\n' \ head = '<p>Для внешних абонентов звонить на xxxxxx + добавочный номер</p>'
'<th><a href=index.py?query_type=internal&order=int_number>Телефон</a></th>\n' \ org = '1'
'<th><a href=index.py?query_type=internal&order=fio>ФИО</a></th>\n' \ s = Server('192.168.1.1')
'<th>Описание<th>Эл.почта</th></tr>\n' c = Connection(s, user='domain1\\phone', password="pass", authentication=NTLM)
result = result + listHeader
# define the server and the connection
s = Server('1.1.1.1')
c = Connection(s, user="DOMEN\\user", password="some-pass", authentication=NTLM)
# perform the Bind operation # perform the Bind operation
if not c.bind(): if not c.bind():
print('error in bind', c.result) print('error in bind', c.result)
c.search('OU=users,dc=domain1,dc=local', '(objectclass=person)',
attributes=['cn', 'mail', 'telephoneNumber', 'department', 'title', 'mobile'])
elif domain == 'domain2':
head = '<p>Для внешних абонентов звонить на xxxxxxx + добавочный номер</p>'
org = '2'
s = Server('192.168.2.2')
c = Connection(s, user='domain2\\phone', password="pass", authentication=NTLM)
# perform the Bind operation
if not c.bind():
print('error in bind', c.result)
c.search('OU=users,dc=domain2,dc=local', '(objectclass=person)',
attributes=['cn', 'mail', 'telephoneNumber', 'department', 'title', 'mobile'])
elif domain == 'domain3':
head = '<p>Для внешних абонентов звонить на xxxxxxx + добавочный номер</p>'
org = '3'
s = Server('192.168.3.3')
c = Connection(s, user='domain3\\phone', password="pass", authentication=NTLM)
# perform the Bind operation
if not c.bind():
print('error in bind', c.result)
c.search('OU=users,dc=domain3,dc=local', '(objectclass=person)',
attributes=['cn', 'mail', 'telephoneNumber', 'department', 'title', 'mobile'])
result = '<h4 align=left>Список абонентов %s</h4>\n%s' % (org, head)
#return (result + "</table>\n")
# listHeader = '<table class="table_dark"><tr>\n' \
# '<th><a href=index.py?query_type=internal&order=fio>ФИО</a></th>\n' \
# '<th>Отдел</th><th>Должность</th>\n' \
# '<th><a href=index.py?query_type=internal&order=int_number>Телефон</a></th>\n' \
# '<th>Эл.почта</th><th>Мобильный телефон</th></tr>\n'
listHeader = '<table class="table_dark"><tr>\n' \
'<th>ФИО</th>\n' \
'<th>Отдел</th><th>Должность</th>\n' \
'<th>Телефон</th>\n' \
'<th>Эл.почта</th><th>Мобильный телефон</th></tr>\n'
result = result + listHeader
c.search('OU=LTB_users,dc=dals,dc=local', '(objectclass=person)',
attributes=['cn', 'mail', 'telephoneNumber', 'department'])
# print(c.result)
for item in c.entries: for item in c.entries:
# print(item) # print(item)
# item = re.sub('\n', '', str(item)) # item = re.sub('\n', '', str(item))
@ -108,6 +143,11 @@ def getLDAPusers():
dep = department.groups()[1] dep = department.groups()[1]
else: else:
dep = "" dep = ""
title = re.search("(title:)(.+?)(\n)", item)
if title:
title = title.groups()[1]
else:
title = ""
mail = re.search("(mail:)(.+?)(\n)", item) mail = re.search("(mail:)(.+?)(\n)", item)
if mail: if mail:
mail = mail.groups()[1] mail = mail.groups()[1]
@ -118,11 +158,15 @@ def getLDAPusers():
phone = telephone.groups()[1] phone = telephone.groups()[1]
else: else:
phone = "" phone = ""
rowData = "<tr><td></td><td>%s</td><td>%s</td><td>%s</td><td><a href=\"mailto:%s\">%s</a></td></tr>\n" % (name, phone, dep, mail, mail) mobile = re.search("(mobile:)(.+?)(\n)", item)
if mobile:
mobile = mobile.groups()[1]
else:
mobile = ""
rowData = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href=\"mailto:%s\">%s</a></td><td>%s</td></tr>\n" % (name, dep, title, phone, mail, mail, mobile)
result = result + rowData result = result + rowData
return (result + "</table>\n") return (result + "</table>\n")
def getCOline(): def getCOline():
conn = connectDB() conn = connectDB()
c = conn.cursor() c = conn.cursor()
@ -200,9 +244,9 @@ def ErrorMessage(txt):
def connectDB(): def connectDB():
c = pymysql.connect( c = pymysql.connect(
db='ats_test', db='ats',
user='ats', user='ats',
passwd='', passwd='pass',
host='kis', host='x.x.x.x',
charset='utf8') charset='utf8')
return c return c