Skip to content

Commit

Permalink
Use connections saved on flask.g.ldap_conn
Browse files Browse the repository at this point in the history
This could be useful if we want to connect as a user for the current request:
g.ldap_conn = ldap.connect(binddn, secret)
  • Loading branch information
rroemhild committed Oct 11, 2015
1 parent 36500b0 commit 352273f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Changelog
=========

0.6.6 (2015-08-10)
0.6.7 (2015-10-11)
------------------

* Use connections saved on flask.g.ldap_conn

0.6.6 (2015-10-8)
------------------
* Return manager class in queries instead of fix LDAPEntry class
* Update six 1.9.0 -> 1.10.0
Expand Down
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ Authenticate with Client
return 'Welcome %s.' % username
Bind as user
------------
To bind as user for the current request save a new connection to ``flask.g.ldap_conn``:
.. code-block:: python
g.ldap_conn = ldap.connect(userdn, password)
user = User.query.get(userdn)
Unit Test
---------
Expand Down
16 changes: 11 additions & 5 deletions flask_ldapconn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from ssl import CERT_REQUIRED, PROTOCOL_TLSv1
from flask import current_app
from flask import current_app, g
from flask import _app_ctx_stack as stack
from ldap3 import Server, Connection, Tls
from ldap3 import STRATEGY_SYNC, GET_ALL_INFO, SUBTREE
Expand All @@ -21,8 +21,8 @@ def __init__(self, app=None):

self.Entry = LDAPEntry
self.Attribute = LDAPAttribute

self.Model = self.Entry
self._app = app

if app is not None:
self.init_app(app)
Expand Down Expand Up @@ -82,19 +82,25 @@ def connect(self, user, password):
return ldap_conn

def teardown(self, exception):
if hasattr(g, 'ldap_conn'):
g.ldap_conn.unbind()

ctx = stack.top
if hasattr(ctx, 'ldap_conn'):
ctx.ldap_conn.unbind()

@property
def connection(self):
user = current_app.config['LDAP_BINDDN']
password = current_app.config['LDAP_SECRET']
if hasattr(g, 'ldap_conn'):
return g.ldap_conn

ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'ldap_conn'):
ctx.ldap_conn = self.connect(user, password)
ctx.ldap_conn = self.connect(
current_app.config['LDAP_BINDDN'],
current_app.config['LDAP_SECRET']
)
return ctx.ldap_conn

def authenticate(self,
Expand Down
4 changes: 1 addition & 3 deletions flask_ldapconn/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ def get_entry_modify_dict(self, attr_dict):

@property
def connection(self):
app = current_app._get_current_object()
ldapc = app.extensions.get('ldap_conn')
return ldapc
return current_app.extensions.get('ldap_conn')

def delete(self):
'''Delete this entry from LDAP server'''
Expand Down
3 changes: 1 addition & 2 deletions flask_ldapconn/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def __iter__(self):

def get_reader_result(self):
query = ','.join(self.query)
app = current_app._get_current_object()
ldapc = app.extensions.get('ldap_conn')
ldapc = current_app.extensions.get('ldap_conn')
reader = Reader(connection=ldapc.connection,
object_def=self.object_def,
query=query,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

setup(
name='Flask-LDAPConn',
version='0.6.6',
version='0.6.7',
url='http://github.com/rroemhild/flask-ldapconn',
license='BSD',
author='Rafael Römhild',
Expand Down

0 comments on commit 352273f

Please sign in to comment.