From ef11f978e7c7f53dd680aaee9086e325ec61e9c0 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 12 Apr 2019 18:14:23 +0200 Subject: [PATCH 1/2] ldap: PEP8 / code style (#30125) --- src/authentic2/backends/ldap_backend.py | 56 +++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/authentic2/backends/ldap_backend.py b/src/authentic2/backends/ldap_backend.py index 1bc2289d..7367cc36 100644 --- a/src/authentic2/backends/ldap_backend.py +++ b/src/authentic2/backends/ldap_backend.py @@ -1,3 +1,19 @@ +# authentic2 - versatile identity manager +# Copyright (C) 2010-2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + try: import ldap import ldap.modlist @@ -16,18 +32,12 @@ import random import base64 import os -# code originaly copied from by now merely inspired by -# http://www.amherst.k12.oh.us/django-ldap.html - -log = logging.getLogger(__name__) - from django.core.exceptions import ImproperlyConfigured from django.conf import settings from django.contrib.auth.models import Group from django.utils.encoding import force_bytes, force_text from django.utils import six from django.utils.six.moves.urllib import parse as urlparse -from django.utils import six from authentic2.a2_rbac.models import Role @@ -39,13 +49,17 @@ from authentic2.compat import get_user_model from authentic2.models import UserExternalId from authentic2.middleware import StoreRequestMiddleware from authentic2.user_login_failure import user_login_failure, user_login_success -from django_rbac.utils import get_ou_model from authentic2.a2_rbac.utils import get_default_ou from authentic2.ldap_utils import FilterFormatter -from authentic2.utils import utf8_encode, to_list - +from authentic2.utils import utf8_encode from authentic2.backends import is_user_authenticable +from django_rbac.utils import get_ou_model + +# code originaly copied from by now merely inspired by +# http://www.amherst.k12.oh.us/django-ldap.html + +log = logging.getLogger(__name__) DEFAULT_CA_BUNDLE = '' @@ -261,12 +275,12 @@ class LDAPUser(get_user_model()): def update_request(self): request = StoreRequestMiddleware.get_request() if request: - assert not request.session is None + assert request.session is not None self.init_to_session(request.session) def init_from_request(self): request = StoreRequestMiddleware.get_request() - assert request and not request.session is None + assert request and request.session is not None self.init_from_session(request.session) def keep_password(self, password): @@ -377,7 +391,9 @@ class LDAPBackend(object): 'bindpw': '', 'bindsasl': (), 'user_dn_template': '', - 'user_filter': 'uid=%s', # will be '(|(mail=%s)(uid=%s))' if A2_ACCEPT_EMAIL_AUTHENTICATION is set (see update_default) + # user_filter will be '(|(mail=%s)(uid=%s))' if + # A2_ACCEPT_EMAIL_AUTHENTICATION is set (see update_default) + 'user_filter': 'uid=%s', 'sync_ldap_users_filter': '', 'user_basedn': '', 'group_dn_template': '', @@ -587,7 +603,7 @@ class LDAPBackend(object): if not block['connect_with_user_credentials']: try: self.bind(block, conn) - except Exception as e: + except Exception: log.exception(u'rebind failure after login bind') raise ldap.SERVER_DOWN break @@ -740,8 +756,7 @@ class LDAPBackend(object): for role_name in role_names: role, error = self.get_role(block, role_id=role_name) if role is None: - log.warning('error %s: couldn\'t retrieve role %r', - error, role_name) + log.warning(u'error %s: couldn\'t retrieve role "%s"', error, role_name) continue # Add missing roles if dn in role_dns and role not in roles: @@ -843,7 +858,6 @@ class LDAPBackend(object): if group not in groups: user.groups.add(group) - def populate_mandatory_roles(self, user, block): mandatory_roles = block.get('set_mandatory_roles') if not mandatory_roles: @@ -855,8 +869,7 @@ class LDAPBackend(object): for role_name in mandatory_roles: role, error = self.get_role(block, role_id=role_name) if role is None: - log.warning('error %s: couldn\'t retrieve role %r', - error, role_name) + log.warning(u'error %s: couldn\'t retrieve role "%s"', error, role_name) continue if role not in roles: user.roles.add(role) @@ -1005,7 +1018,6 @@ class LDAPBackend(object): return def lookup_by_external_id(self, block, attributes): - User = get_user_model() for eid_tuple in map_text(block['external_id_tuples']): external_id = self.build_external_id(eid_tuple, attributes) if not external_id: @@ -1020,7 +1032,7 @@ class LDAPBackend(object): user = users[0] if len(users) > 1: log.info('found %d users, collectings roles into the first one and deleting the other ones.', - len(users)) + len(users)) for other in users[1:]: for r in other.roles.all(): user.roles.add(r) @@ -1313,8 +1325,8 @@ class LDAPBackend(object): if isinstance(cls._DEFAULTS[d], bool) and not isinstance(block[d], bool): raise ImproperlyConfigured( 'LDAP_AUTH_SETTINGS: attribute %r must be a boolean' % d) - if (isinstance(cls._DEFAULTS[d], (list, tuple)) and - not isinstance(block[d], (list, tuple))): + if (isinstance(cls._DEFAULTS[d], (list, tuple)) + and not isinstance(block[d], (list, tuple))): raise ImproperlyConfigured( 'LDAP_AUTH_SETTINGS: attribute %r must be a list or a tuple' % d) if isinstance(cls._DEFAULTS[d], dict) and not isinstance(block[d], dict): -- 2.20.1