From 9022a9ab985a74fdfc408c6fca40cada373bb7c8 Mon Sep 17 00:00:00 2001 From: Alexandre Fayolle Date: Thu, 21 Feb 2019 10:46:58 +0100 Subject: [PATCH] [FIX] auth_ldap_populate would rearchive users which had been unarchive in the first place on the way: * use sets to remove duplicates * don't query all ldap configurations if one has said the user must be archived * improve the view by not relying on a string attribute --- users_ldap_populate/models/users_ldap.py | 26 +++++++++++++++--------- users_ldap_populate/view/users_ldap.xml | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/users_ldap_populate/models/users_ldap.py b/users_ldap_populate/models/users_ldap.py index a4afb5d29..ddf4ff883 100644 --- a/users_ldap_populate/models/users_ldap.py +++ b/users_ldap_populate/models/users_ldap.py @@ -25,7 +25,7 @@ from openerp import SUPERUSER_ID from openerp.tools.translate import _ import logging -_logger = logging.getLogger(__name__) +_logger = logging.getLogger('orm.ldap') try: import ldap @@ -68,8 +68,7 @@ class CompanyLDAP(orm.Model): users_pool = self.pool.get('res.users') users_no_before = users_pool.search( cr, uid, [], context=context, count=True) - logger = logging.getLogger('orm.ldap') - logger.debug("action_populate called on res.company.ldap ids %s", ids) + _logger.debug("action_populate called on res.company.ldap ids %s", ids) deactivate_unknown = None known_user_ids = [uid] @@ -85,7 +84,7 @@ class CompanyLDAP(orm.Model): deactivate_unknown &= this['deactivate_unknown_users'] if deactivate_unknown: - logger.debug("will deactivate unknown users") + _logger.debug("will deactivate unknown users") for conf in self.get_ldap_dicts(cr, ids): if not conf['create_user']: @@ -119,6 +118,7 @@ class CompanyLDAP(orm.Model): {'active': True}, context=context ) + user_id = res[0] else: raise orm.except_orm( _('UserError'), @@ -135,8 +135,8 @@ class CompanyLDAP(orm.Model): deactivated_users_count = self.do_deactivate_unknown_users( cr, uid, ids, known_user_ids, context=context) - logger.debug("%d users created", users_created) - logger.debug("%d users deactivated", deactivated_users_count) + _logger.debug("%d users created", users_created) + _logger.debug("%d users deactivated", deactivated_users_count) return users_created, deactivated_users_count def do_deactivate_unknown_users( @@ -146,18 +146,24 @@ class CompanyLDAP(orm.Model): """ res_users = self.pool.get('res.users') unknown_user_ids = [] + known_user_ids = list(set(known_user_ids)) for unknown_user in res_users.read( cr, uid, res_users.search( cr, uid, [('id', 'not in', known_user_ids)], + order='login', context=context), ['login'], context=context): - present_in_ldap = False - for conf in self.get_ldap_dicts(cr, ids): - present_in_ldap |= bool(self.get_ldap_entry_dicts( - conf, user_name=unknown_user['login'])) + present_in_ldap = any( + bool( + self.get_ldap_entry_dicts( + conf, + user_name=unknown_user.login, + )) + for conf in self.get_ldap_dicts(cr, ids) + ) if not present_in_ldap: res_users.write( cr, uid, unknown_user['id'], {'active': False}, diff --git a/users_ldap_populate/view/users_ldap.xml b/users_ldap_populate/view/users_ldap.xml index e2a742330..547fe749e 100644 --- a/users_ldap_populate/view/users_ldap.xml +++ b/users_ldap_populate/view/users_ldap.xml @@ -6,7 +6,7 @@ res.company - +