From e342936402d7435b97fdb2c359a8577d49832563 Mon Sep 17 00:00:00 2001 From: svk28 Date: Wed, 8 Nov 2017 11:39:25 +0300 Subject: [PATCH] Added LDAP (AD) address book support --- www/index.py | 100 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 28 deletions(-) diff --git a/www/index.py b/www/index.py index e981d82..6bb4987 100755 --- a/www/index.py +++ b/www/index.py @@ -3,7 +3,7 @@ import os, sys, pymysql, re, time from ldap3 import Server, Connection, NTLM def application(environ, start_response): - #sys.stdout._encoding = 'utf-8' + #sys.stdout._encoding = 'utf-8'замена сцепления патриот стоимость работ смоленск status = '200 OK' # проверяем наличие параметров в запросе и всяко-разно реагируем if environ['QUERY_STRING'] == '': @@ -17,7 +17,9 @@ def application(environ, start_response): elif paramDict.get('query_type') == 'report': output = bytes((header() + body() + ReportForm() + ReportData(environ) + footer()).encode('utf8')) 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: output = bytes((header() + body() + footer()).encode('utf8')) @@ -48,14 +50,15 @@ def menu(): txtMenu = '
\n' \ + '
  • Список абонентов 1
  • \n' \ + '
  • Список абонентов 2
  • \n' \ + '
  • Список абонентов 3
  • \n' \ '
  • Отчёты
  • \n' return txtMenu -# Список внутренних номеров из СУБД + def getInternalNumbers(): conn = connectDB() c = conn.cursor() @@ -66,33 +69,65 @@ def getInternalNumbers(): listHeader = '\n' \ '\n' \ '\n' \ - '\n' + '\n' result = result + listHeader for row in c.fetchall(): - rowData = "\n" % (row[0], row[1], row[2], row[4], row[3], row[3]) + rowData = "\n" % (row[0], row[1], row[2], row[4], row[3]) result = result + rowData return (result + "
    № п/пТелефонФИООписаниеЭл.почта
    ОписаниеЭлектро-почта
    %s%s%s%s%s
    %s%s%s%s%s
    \n") -# Список пользователей (ФИО, телефон, почта, отдел) из LDAP (AD) -def getLDAPusers(): +# Список пользователей (ФИО, телефон, почта, отдел) из LDAP (AD) +def getLDAPusers(domain, group): order = 'int_number' - result = '

    Список абонентов ЛТБ

    ' - listHeader = '\n' \ - '\n' \ - '\n' \ - '\n' + if domain == 'domain1': + head = '

    Для внешних абонентов звонить на xxxxxx + добавочный номер

    ' + org = '1' + s = Server('192.168.1.1') + c = Connection(s, user='domain1\\phone', password="pass", authentication=NTLM) + # perform the Bind operation + if not c.bind(): + 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 = '

    Для внешних абонентов звонить на xxxxxxx + добавочный номер

    ' + 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 = '

    Для внешних абонентов звонить на xxxxxxx + добавочный номер

    ' + 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 = '

    Список абонентов %s

    \n%s' % (org, head) + + #return (result + "
    № п/пТелефонФИООписаниеЭл.почта
    \n") + + # listHeader = '\n' \ + # '\n' \ + # '\n' \ + # '\n' \ + # '\n' + listHeader = '
    ФИООтделДолжностьТелефонЭл.почтаМобильный телефон
    \n' \ + '\n' \ + '\n' \ + '\n' \ + '\n' + 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 - if not c.bind(): - print('error in bind', c.result) - c.search('OU=LTB_users,dc=dals,dc=local', '(objectclass=person)', - attributes=['cn', 'mail', 'telephoneNumber', 'department']) - # print(c.result) for item in c.entries: # print(item) # item = re.sub('\n', '', str(item)) @@ -108,6 +143,11 @@ def getLDAPusers(): dep = department.groups()[1] else: dep = "" + title = re.search("(title:)(.+?)(\n)", item) + if title: + title = title.groups()[1] + else: + title = "" mail = re.search("(mail:)(.+?)(\n)", item) if mail: mail = mail.groups()[1] @@ -118,11 +158,15 @@ def getLDAPusers(): phone = telephone.groups()[1] else: phone = "" - rowData = "\n" % (name, phone, dep, mail, mail) + mobile = re.search("(mobile:)(.+?)(\n)", item) + if mobile: + mobile = mobile.groups()[1] + else: + mobile = "" + rowData = "\n" % (name, dep, title, phone, mail, mail, mobile) result = result + rowData return (result + "
    ФИООтделДолжностьТелефонЭл.почтаМобильный телефон
    %s%s%s%s
    %s%s%s%s%s%s
    \n") - def getCOline(): conn = connectDB() c = conn.cursor() @@ -200,9 +244,9 @@ def ErrorMessage(txt): def connectDB(): c = pymysql.connect( - db='ats_test', + db='ats', user='ats', - passwd='', - host='kis', + passwd='pass', + host='x.x.x.x', charset='utf8') return c