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 %} +