Lookup recipient on LDAP by mail=
This commit is contained in:
parent
0e51f193c2
commit
4d54472d79
|
@ -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:
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block main %}
|
||||
<h1>FileDrop App</h1>
|
||||
<div>
|
||||
<form method="POST">
|
||||
<strong>Found Recipient:</strong>
|
||||
<address>{{ recipient.dn }}</address>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -3,7 +3,6 @@
|
|||
{% block main %}
|
||||
<h1>FileDrop App</h1>
|
||||
<div>
|
||||
Form to select and upload files will go here.
|
||||
<form method="POST">
|
||||
<dl>
|
||||
<dt>{{ form.email.label }}</dt>
|
||||
|
|
Loading…
Reference in New Issue