2
2
import re
3
3
from functools import wraps
4
4
import ldap
5
- from ldap import filter
5
+ from ldap import filter as ldap_filter
6
6
from flask import abort , current_app , g , make_response , redirect , url_for , \
7
7
request
8
8
@@ -93,7 +93,7 @@ def initialize(self):
93
93
conn .start_tls_s ()
94
94
return conn
95
95
except ldap .LDAPError as e :
96
- raise LDAPException (self .error (e ))
96
+ raise LDAPException (self .error (e . args ))
97
97
98
98
@property
99
99
def bind (self ):
@@ -111,7 +111,7 @@ def bind(self):
111
111
current_app .config ['LDAP_PASSWORD' ])
112
112
return conn
113
113
except ldap .LDAPError as e :
114
- raise LDAPException (self .error (e ))
114
+ raise LDAPException (self .error (e . args ))
115
115
116
116
def bind_user (self , username , password ):
117
117
"""Attempts to bind a user to the LDAP server using the credentials
@@ -158,12 +158,12 @@ def get_object_details(self, user=None, group=None, dn_only=False):
158
158
if user is not None :
159
159
if not dn_only :
160
160
fields = current_app .config ['LDAP_USER_FIELDS' ]
161
- query = filter .filter_format (
161
+ query = ldap_filter .filter_format (
162
162
current_app .config ['LDAP_USER_OBJECT_FILTER' ], (user ,))
163
163
elif group is not None :
164
164
if not dn_only :
165
165
fields = current_app .config ['LDAP_GROUP_FIELDS' ]
166
- query = filter .filter_format (
166
+ query = ldap_filter .filter_format (
167
167
current_app .config ['LDAP_GROUP_OBJECT_FILTER' ], (group ,))
168
168
conn = self .bind
169
169
try :
@@ -187,7 +187,7 @@ def get_object_details(self, user=None, group=None, dn_only=False):
187
187
result [k ] = v
188
188
return result
189
189
except ldap .LDAPError as e :
190
- raise LDAPException (self .error (e ))
190
+ raise LDAPException (self .error (e . args ))
191
191
192
192
def get_user_groups (self , user ):
193
193
"""Returns a ``list`` with the user's groups or ``None`` if
@@ -203,14 +203,14 @@ def get_user_groups(self, user):
203
203
[str (current_app .config ['LDAP_GROUP_MEMBER_FILTER_FIELD' ])]
204
204
records = conn .search_s (
205
205
current_app .config ['LDAP_BASE_DN' ], ldap .SCOPE_SUBTREE ,
206
- ldap . filter .filter_format (
206
+ ldap_filter .filter_format (
207
207
current_app .config ['LDAP_GROUP_MEMBER_FILTER' ],
208
208
(self .get_object_details (user , dn_only = True ),)),
209
209
fields )
210
210
else :
211
211
records = conn .search_s (
212
212
current_app .config ['LDAP_BASE_DN' ], ldap .SCOPE_SUBTREE ,
213
- ldap . filter .filter_format (
213
+ ldap_filter .filter_format (
214
214
current_app .config ['LDAP_USER_OBJECT_FILTER' ],
215
215
(user ,)),
216
216
[current_app .config ['LDAP_USER_GROUPS_FIELD' ]])
@@ -228,11 +228,11 @@ def get_user_groups(self, user):
228
228
records [0 ][1 ]:
229
229
groups = records [0 ][1 ][
230
230
current_app .config ['LDAP_USER_GROUPS_FIELD' ]]
231
- result = [re .findall (b'(?:cn=|CN=)(.*?),' , group )[0 ] for
232
- group in groups ]
231
+ result = [re .findall (b'(?:cn=|CN=)(.*?),' , group )[0 ]
232
+ for group in groups ]
233
233
return result
234
234
except ldap .LDAPError as e :
235
- raise LDAPException (self .error (e ))
235
+ raise LDAPException (self .error (e . args ))
236
236
237
237
def get_group_members (self , group ):
238
238
"""Returns a ``list`` with the group's members or ``None`` if
@@ -245,7 +245,7 @@ def get_group_members(self, group):
245
245
try :
246
246
records = conn .search_s (
247
247
current_app .config ['LDAP_BASE_DN' ], ldap .SCOPE_SUBTREE ,
248
- ldap . filter .filter_format (
248
+ ldap_filter .filter_format (
249
249
current_app .config ['LDAP_GROUP_OBJECT_FILTER' ], (group ,)),
250
250
[current_app .config ['LDAP_GROUP_MEMBERS_FIELD' ]])
251
251
conn .unbind_s ()
@@ -256,15 +256,15 @@ def get_group_members(self, group):
256
256
current_app .config ['LDAP_GROUP_MEMBERS_FIELD' ]]
257
257
return members
258
258
except ldap .LDAPError as e :
259
- raise LDAPException (self .error (e ))
259
+ raise LDAPException (self .error (e . args ))
260
260
261
261
@staticmethod
262
262
def error (e ):
263
- e = e . args [0 ]
263
+ e = e [0 ]
264
264
if 'desc' in e :
265
265
return e ['desc' ]
266
266
else :
267
- return e [ 0 ]
267
+ return e
268
268
269
269
@staticmethod
270
270
def login_required (func ):
0 commit comments