@@ -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