From 4d54472d79b644b9335512ad3481103f4e734a59 Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sat, 17 Nov 2018 10:11:36 +0100 Subject: [PATCH] Lookup recipient on LDAP by mail= --- backend/src/app.py | 38 +++++++++++++++++++++++++++++- backend/src/templates/found.html | 11 +++++++++ backend/src/templates/landing.html | 1 - 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 backend/src/templates/found.html diff --git a/backend/src/app.py b/backend/src/app.py index 0c7aafa..4794e29 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -1,5 +1,6 @@ import logging import configparser +from contextlib import contextmanager from flask import Flask, render_template from forms import UserLookupForm @@ -13,12 +14,47 @@ LDAP_PASSWORD = config.get('LDAP', 'bind_password') app = Flask(__name__) app.logger.setLevel(logging.INFO) -@app.route('/') +@contextmanager +def connect(): + server = Server(LDAP_SERVER) + conn = Connection(server, user=LDAP_BIND_DN, password=LDAP_PASSWORD) + try: + if not conn.bind(): + app.logger.error('Finded to bind using set credentials') + abort(500, description='Unable to connect to LDAP') + app.logger.info('Connected to LDAP') + yield conn + finally: + app.logger.info('Unbind()ing') + conn.unbind() + +def find_recipient(email): + base = 'ou=people,dc=xeentech,dc=com' + filter = '(mail={})'.format(email) + with connect() as conn: + ok = conn.search(base, filter, attributes=['cn', 'sn']) + if not ok or len(conn.response) == 0: + app.logger.info('Query ok but no one found for: %s', email) + else: + app.logger.info('Query found %d users', len(conn.response)) + user = conn.response[0] + app.logger.info('User object looks like: %s', str(user)) + return dict( + dn = user['dn'], + name = user['attributes']['sn'], + ) + return False + +@app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': form = UserLookupForm(request.form) + if form.validate(): email = form.email.data + recipient = find_recipient(email) + if recipient: + return render_template('found.html', recipient=recipient) else: app.logger.warn('Form validation failed') else: diff --git a/backend/src/templates/found.html b/backend/src/templates/found.html new file mode 100644 index 0000000..aafa999 --- /dev/null +++ b/backend/src/templates/found.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block main %} +

FileDrop App

+
+
+ Found Recipient: +
{{ recipient.dn }}
+
+
+{% endblock %} diff --git a/backend/src/templates/landing.html b/backend/src/templates/landing.html index 48cc565..f5666bc 100644 --- a/backend/src/templates/landing.html +++ b/backend/src/templates/landing.html @@ -3,7 +3,6 @@ {% block main %}

FileDrop App

- Form to select and upload files will go here.
{{ form.email.label }}