Skip to content

Commit 137d02f

Browse files
committed
Adds get_users method to provide an interface for
listing all available users in the ldap directory based on the filter defined in LDAP_USERS_OBJECT_FILTER The get_users method has two arguments fields and dn_only to customize the result. i.e. get_users(fields=['uid']) provide a list of dict with uid included. if only get_users(dn_only=True) is provided, a list of available users dn is returned. If no users in directory, an empty list is returned. Signed-off-by: JM Lopez <[email protected]>
1 parent 78f66d8 commit 137d02f

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ directives:
7878
object details. Default: ``list`` (all).
7979
``LDAP_USER_OBJECT_FILTER`` The filter to use when searching for a user object.
8080
Default: '(&(objectclass=Person)(userPrincipalName=%s))'
81+
``LDAP_USERS_OBJECT_FILTER`` The filter to use when searching for users objects.
82+
Default: 'objectclass=Person'
8183
``LDAP_USER_GROUPS_FIELD`` The field to return when searching for a user's
8284
groups. Default: 'memberOf'.
8385
``LDAP_GROUPS_OBJECT_FILTER`` The filter to use when searching for groups objects.

examples/basic_auth/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
app.config['LDAP_BASE_DN'] = 'OU=users,dc=example,dc=org'
77
app.config['LDAP_USERNAME'] = 'CN=user,OU=Users,DC=example,DC=org'
88
app.config['LDAP_PASSWORD'] = 'password'
9+
app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=inetOrgPerson)(uid=%s))'
10+
app.config['LDAP_USERS_OBJECT_FILTER'] = 'objectclass=inetOrgPerson'
11+
app.config['LDAP_USER_FIELDS'] = ['cn', 'uid']
912

1013
ldap = LDAP(app)
1114

examples/basic_auth/app_oldap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
app.config['LDAP_OPENLDAP'] = True
1515
app.config['LDAP_OBJECTS_DN'] = 'dn'
1616
app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=inetOrgPerson)(uid=%s))'
17+
app.config['LDAP_USERS_OBJECT_FILTER'] = 'objectclass=inetOrgPerson'
1718

1819
# Groups configuration
1920
app.config['LDAP_GROUP_MEMBERS_FIELD'] = 'uniquemember'

flask_simpleldap/__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def init_app(app):
4545
app.config.setdefault('LDAP_BASE_DN', None)
4646
app.config.setdefault('LDAP_OBJECTS_DN', 'distinguishedName')
4747
app.config.setdefault('LDAP_USER_FIELDS', [])
48+
app.config.setdefault('LDAP_USERS_OBJECT_FILTER',
49+
'objectclass=Person')
4850
app.config.setdefault('LDAP_USER_OBJECT_FILTER',
4951
'(&(objectclass=Person)(userPrincipalName=%s))')
5052
app.config.setdefault('LDAP_USER_GROUPS_FIELD', 'memberOf')
@@ -155,6 +157,43 @@ def bind_user(self, username, password):
155157
except ldap.LDAPError:
156158
return
157159

160+
def get_users(self, fields=None, dn_only=False):
161+
"""Returns a ``list`` with the users in base dn
162+
or empty ``list`` if unsuccessful.
163+
164+
LDAP query setting is ``LDAP_USERS_OBJECT_FILTER``
165+
166+
:param fields: list of user fields to retrieve.
167+
if ``None`` or empty, default user fields
168+
``LDAP_USER_FIELDS`` is used
169+
:param bool dn_only: If we should only retrieve the object's
170+
distinguished name or not. Default: ``False``.
171+
:type fields: list
172+
"""
173+
conn = self.bind
174+
try:
175+
fields = fields or current_app.config['LDAP_USER_FIELDS']
176+
if current_app.config['LDAP_OPENLDAP']:
177+
records = conn.search_s(
178+
current_app.config['LDAP_BASE_DN'], ldap.SCOPE_SUBTREE,
179+
current_app.config['LDAP_USERS_OBJECT_FILTER'],
180+
fields)
181+
else:
182+
records = conn.search_s(
183+
current_app.config['LDAP_BASE_DN'], ldap.SCOPE_SUBTREE,
184+
current_app.config['LDAP_USERS_OBJECT_FILTER'],
185+
fields)
186+
conn.unbind_s()
187+
if records:
188+
if dn_only:
189+
return [r[0] for r in records]
190+
else:
191+
return [r[1] for r in records]
192+
else:
193+
return []
194+
except ldap.LDAPError as e:
195+
raise LDAPException(self.error(e.args))
196+
158197
def get_object_details(self, user=None, group=None, query_filter=None,
159198
dn_only=False):
160199
"""Returns a ``dict`` with the object's (user or group) details.
@@ -337,7 +376,7 @@ def login_required(func):
337376
@wraps(func)
338377
def wrapped(*args, **kwargs):
339378
if g.user is None:
340-
next_path=request.full_path or request.path
379+
next_path = request.full_path or request.path
341380
if next_path == '/?':
342381
return redirect(
343382
url_for(current_app.config['LDAP_LOGIN_VIEW']))

0 commit comments

Comments
 (0)