1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-24 10:37:23 +00:00

Catch attribute error on ldap import user

Fixes for Login with Windows AD
This commit is contained in:
Ozzieisaacs 2020-12-03 18:51:03 +01:00
parent 7c96fac95c
commit 046a074c3a
2 changed files with 22 additions and 16 deletions

View File

@ -85,8 +85,8 @@ def init_app(app, config):
log.error(e) log.error(e)
def get_object_details(user=None): def get_object_details(user=None,query_filter=None):
return _ldap.get_object_details(user) return _ldap.get_object_details(user, query_filter=query_filter)
def bind(): def bind():

View File

@ -328,26 +328,31 @@ def import_ldap_users():
user = username.decode('utf-8') user = username.decode('utf-8')
if '=' in user: if '=' in user:
# if member object field is empty take user object as filter # if member object field is empty take user object as filter
if config.config_ldap_member_user_object:
query_filter = config.config_ldap_member_user_object
else:
query_filter = config.config_ldap_user_object
try: try:
if config.config_ldap_member_user_object: user_identifier = extract_user_identifier(user, query_filter)
user_identifier = extract_user_identifier(user, config.config_ldap_member_user_object)
else:
user_identifier = extract_user_identifier(user, config.config_ldap_user_object)
except Exception as e: except Exception as e:
log.warning(e) log.warning(e)
continue continue
else: else:
user_identifier = user user_identifier = user
query_filter = None
if ub.session.query(ub.User).filter(ub.User.nickname == user_identifier.lower()).first(): if ub.session.query(ub.User).filter(ub.User.nickname == user_identifier.lower()).first():
log.warning("LDAP User: %s Already in Database", user_identifier) log.warning("LDAP User: %s Already in Database", user_identifier)
continue continue
user_data = services.ldap.get_object_details(user=user_identifier) try:
user_data = services.ldap.get_object_details(user=user_identifier, query_filter=query_filter)
except AttributeError:
log.exception(e)
continue
if user_data: if user_data:
content = ub.User() content = ub.User()
# user_login_field = extract_dynamic_field_from_filter(user, config.config_ldap_user_object) user_login_field = extract_dynamic_field_from_filter(user, config.config_ldap_user_object)
content.nickname = user_identifier # user_data[user_login_field][0].decode('utf-8') content.nickname = user_data[user_login_field][0].decode('utf-8')
content.password = '' # dummy password which will be replaced by ldap one content.password = '' # dummy password which will be replaced by ldap one
if 'mail' in user_data: if 'mail' in user_data:
content.email = user_data['mail'][0].decode('utf-8') content.email = user_data['mail'][0].decode('utf-8')
@ -355,7 +360,7 @@ def import_ldap_users():
content.kindle_mail = user_data['mail'][1].decode('utf-8') content.kindle_mail = user_data['mail'][1].decode('utf-8')
else: else:
log.debug('No Mail Field Found in LDAP Response') log.debug('No Mail Field Found in LDAP Response')
content.email = user + '@email.com' content.email = content.nickname + '@email.com'
content.role = config.config_default_role content.role = config.config_default_role
content.sidebar_view = config.config_default_show content.sidebar_view = config.config_default_show
content.allowed_tags = config.config_allowed_tags content.allowed_tags = config.config_allowed_tags
@ -385,14 +390,15 @@ def extract_user_data_from_field(user, field):
else: else:
raise Exception("Could Not Parse LDAP User: {}".format(user)) raise Exception("Could Not Parse LDAP User: {}".format(user))
# CN=Firstname LastName,OU=Laba,OU=...,DC=...,DC=... def extract_dynamic_field_from_filter(user, filter):
# CN=user displayname,OU=ouname1,OU=ouname2,OU=ouname3,DC=domain,DC=domain
def extract_user_identifier(user, filter):
match = re.search("([a-zA-Z0-9-]+)=%s", filter, re.IGNORECASE | re.UNICODE) match = re.search("([a-zA-Z0-9-]+)=%s", filter, re.IGNORECASE | re.UNICODE)
if match: if match:
dynamic_field = match.group(1) return match.group(1)
else: else:
raise Exception("Could Not Parse LDAP User: {}".format(user)) raise Exception("Could Not Parse LDAP User: {}", user)
def extract_user_identifier(user, filter):
dynamic_field = extract_dynamic_field_from_filter(user, filter)
return extract_user_data_from_field(user, dynamic_field) return extract_user_data_from_field(user, dynamic_field)