Skip to content

Commit ee9242e

Browse files
authored
Merge pull request #76 from jm66/get-users
Adds get_users method to provide an interface for
2 parents ba8caa8 + e85e8a3 commit ee9242e

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ directives:
8181
object details. Default: ``list`` (all).
8282
``LDAP_USER_OBJECT_FILTER`` The filter to use when searching for a user object.
8383
Default: '(&(objectclass=Person)(userPrincipalName=%s))'
84+
``LDAP_USERS_OBJECT_FILTER`` The filter to use when searching for users objects.
85+
Default: 'objectclass=Person'
8486
``LDAP_USER_GROUPS_FIELD`` The field to return when searching for a user's
8587
groups. Default: 'memberOf'.
8688
``LDAP_GROUPS_OBJECT_FILTER`` The filter to use when searching for groups objects.

examples/basic_auth/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
from flask_simpleldap import LDAP
33

44
app = Flask(__name__)
5-
# app.config['LDAP_HOST'] = 'ldap.example.org' # defaults to localhost
5+
#app.config["LDAP_HOST"] = "ldap.example.org" # defaults to localhost
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: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ 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(
49-
"LDAP_USER_OBJECT_FILTER", "(&(objectclass=Person)(userPrincipalName=%s))"
50-
)
5148
app.config.setdefault("LDAP_USER_GROUPS_FIELD", "memberOf")
49+
app.config.setdefault("LDAP_USER_OBJECT_FILTER",
50+
"(&(objectclass=Person)(userPrincipalName=%s))")
51+
app.config.setdefault("LDAP_USERS_OBJECT_FILTER",
52+
"objectclass=Person")
5253
app.config.setdefault("LDAP_GROUP_FIELDS", [])
53-
app.config.setdefault("LDAP_GROUPS_OBJECT_FILTER", "objectclass=Group")
54-
app.config.setdefault(
55-
"LDAP_GROUP_OBJECT_FILTER", "(&(objectclass=Group)(userPrincipalName=%s))"
56-
)
5754
app.config.setdefault("LDAP_GROUP_MEMBERS_FIELD", "member")
55+
app.config.setdefault("LDAP_GROUP_OBJECT_FILTER",
56+
"(&(objectclass=Group)(userPrincipalName=%s))")
57+
app.config.setdefault("LDAP_GROUPS_OBJECT_FILTER", "objectclass=Group")
5858
app.config.setdefault("LDAP_LOGIN_VIEW", "login")
5959
app.config.setdefault("LDAP_REALM_NAME", "LDAP authentication")
6060
app.config.setdefault("LDAP_OPENLDAP", False)
@@ -63,11 +63,14 @@ def init_app(app):
6363
app.config.setdefault("LDAP_CUSTOM_OPTIONS", None)
6464

6565
if app.config["LDAP_USE_SSL"] or app.config["LDAP_USE_TLS"]:
66-
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
66+
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,
67+
ldap.OPT_X_TLS_NEVER)
6768

6869
if app.config["LDAP_REQUIRE_CERT"]:
69-
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
70-
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, app.config["LDAP_CERT_PATH"])
70+
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,
71+
ldap.OPT_X_TLS_DEMAND)
72+
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,
73+
app.config["LDAP_CERT_PATH"])
7174

7275
if app.config["LDAP_BASE_DN"] is None:
7376
raise LDAPException("LDAP_BASE_DN cannot be None!")
@@ -168,9 +171,45 @@ def bind_user(self, username, password):
168171
except ldap.LDAPError:
169172
return
170173

171-
def get_object_details(
172-
self, user=None, group=None, query_filter=None, dn_only=False
173-
):
174+
def get_users(self, fields=None, dn_only=False):
175+
"""Returns a ``list`` with the users in base dn
176+
or empty ``list`` if unsuccessful.
177+
178+
LDAP query setting is ``LDAP_USERS_OBJECT_FILTER``
179+
180+
:param fields: list of user fields to retrieve.
181+
if ``None`` or empty, default user fields
182+
``LDAP_USER_FIELDS`` is used
183+
:param bool dn_only: If we should only retrieve the object's
184+
distinguished name or not. Default: ``False``.
185+
:type fields: list
186+
"""
187+
conn = self.bind
188+
try:
189+
fields = fields or current_app.config["LDAP_USER_FIELDS"]
190+
if current_app.config["LDAP_OPENLDAP"]:
191+
records = conn.search_s(
192+
current_app.config["LDAP_BASE_DN"], ldap.SCOPE_SUBTREE,
193+
current_app.config["LDAP_USERS_OBJECT_FILTER"],
194+
fields)
195+
else:
196+
records = conn.search_s(
197+
current_app.config["LDAP_BASE_DN"], ldap.SCOPE_SUBTREE,
198+
current_app.config["LDAP_USERS_OBJECT_FILTER"],
199+
fields)
200+
conn.unbind_s()
201+
if records:
202+
if dn_only:
203+
return [r[0] for r in records]
204+
else:
205+
return [r[1] for r in records]
206+
else:
207+
return []
208+
except ldap.LDAPError as e:
209+
raise LDAPException(self.error(e.args))
210+
211+
def get_object_details(self, user=None, group=None, query_filter=None,
212+
dn_only=False):
174213
"""Returns a ``dict`` with the object's (user or group) details.
175214
176215
:param str user: Username of the user object you want details for.
@@ -371,10 +410,10 @@ def wrapped(*args, **kwargs):
371410
if g.user is None:
372411
next_path = request.full_path or request.path
373412
if next_path == "/?":
374-
return redirect(url_for(current_app.config["LDAP_LOGIN_VIEW"]))
375-
return redirect(
376-
url_for(current_app.config["LDAP_LOGIN_VIEW"], next=next_path)
377-
)
413+
return redirect(
414+
url_for(current_app.config["LDAP_LOGIN_VIEW"]))
415+
return redirect(url_for(current_app.config["LDAP_LOGIN_VIEW"],
416+
next=next_path))
378417
return func(*args, **kwargs)
379418

380419
return wrapped

0 commit comments

Comments
 (0)