@@ -49,6 +49,7 @@ def init_app(app):
4949 '(&(objectclass=Person)(userPrincipalName=%s))' )
5050 app .config .setdefault ('LDAP_USER_GROUPS_FIELD' , 'memberOf' )
5151 app .config .setdefault ('LDAP_GROUP_FIELDS' , [])
52+ app .config .setdefault ('LDAP_GROUPS_OBJECT_FILTER' , 'objectclass=Group' )
5253 app .config .setdefault ('LDAP_GROUP_OBJECT_FILTER' ,
5354 '(&(objectclass=Group)(userPrincipalName=%s))' )
5455 app .config .setdefault ('LDAP_GROUP_MEMBERS_FIELD' , 'member' )
@@ -201,6 +202,42 @@ def get_object_details(self, user=None, group=None, query_filter=None,
201202 except ldap .LDAPError as e :
202203 raise LDAPException (self .error (e .args ))
203204
205+ def get_groups (self , fields = None , dn_only = False ):
206+ """Returns a ``list`` with the groups in base dn
207+ or an empty``list`` if unsuccessful.
208+
209+ LDAP query setting is ``LDAP_GROUPS_OBJECT_FILTER``
210+
211+ :param fields: list of group fields to retrieve.
212+ if ``None`` or empty, default group fields is used
213+ :type fields: list
214+ :param bool dn_only: If we should only retrieve the object's
215+ distinguished name or not. Default: ``False``.
216+ """
217+ conn = self .bind
218+ try :
219+ fields = fields or current_app .config ['LDAP_GROUP_FIELDS' ]
220+ if current_app .config ['LDAP_OPENLDAP' ]:
221+ records = conn .search_s (
222+ current_app .config ['LDAP_BASE_DN' ], ldap .SCOPE_SUBTREE ,
223+ current_app .config ['LDAP_GROUPS_OBJECT_FILTER' ],
224+ fields )
225+ else :
226+ records = conn .search_s (
227+ current_app .config ['LDAP_BASE_DN' ], ldap .SCOPE_SUBTREE ,
228+ current_app .config ['LDAP_GROUPS_OBJECT_FILTER' ],
229+ fields )
230+ conn .unbind_s ()
231+ if records :
232+ if dn_only :
233+ return [r [0 ] for r in records ]
234+ else :
235+ return [r [1 ] for r in records ]
236+ else :
237+ return []
238+ except ldap .LDAPError as e :
239+ raise LDAPException (self .error (e .args ))
240+
204241 def get_user_groups (self , user ):
205242 """Returns a ``list`` with the user's groups or ``None`` if
206243 unsuccessful.
0 commit comments