From f7ac5cf999187f13ebe116f685d8bf502f37af2c Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Fri, 26 Jul 2013 14:36:58 +0100 Subject: [PATCH 1/9] ADD Module to use email from LDAP records --- users_ldap_mail/__init__.py | 22 ++++++++++ users_ldap_mail/__openerp__.py | 40 ++++++++++++++++++ users_ldap_mail/users_ldap_model.py | 63 +++++++++++++++++++++++++++++ users_ldap_mail/users_ldap_view.xml | 18 +++++++++ 4 files changed, 143 insertions(+) create mode 100644 users_ldap_mail/__init__.py create mode 100644 users_ldap_mail/__openerp__.py create mode 100644 users_ldap_mail/users_ldap_model.py create mode 100644 users_ldap_mail/users_ldap_view.xml diff --git a/users_ldap_mail/__init__.py b/users_ldap_mail/__init__.py new file mode 100644 index 000000000..a6d454183 --- /dev/null +++ b/users_ldap_mail/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Daniel Reis. +# +# 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 . +# +############################################################################## + +import users_ldap_model diff --git a/users_ldap_mail/__openerp__.py b/users_ldap_mail/__openerp__.py new file mode 100644 index 000000000..97709502d --- /dev/null +++ b/users_ldap_mail/__openerp__.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Daniel Reis. +# +# 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 . +# +############################################################################## + +{ +"name" : "LDAP mapping for user name and e-mail", +"version" : "1.0", +"depends" : ["auth_ldap"], +"author" : "Daniel Reis", +"description": """\ +Allows to define the LDAP attributes to use to retrieve user name and e-mail address. + +The default attribute used for the name is "cn". +For Active Directory, you might prefer to use "displayName" instead. +AD also supports the "mail" attribute, so it can be mapped into OpenERP. +""", +"category" : "Tools", +"data" : [ + 'users_ldap_view.xml', +], +"installable": True, +} +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py new file mode 100644 index 000000000..52b3899e8 --- /dev/null +++ b/users_ldap_mail/users_ldap_model.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Daniel Reis +# +# 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 . +# +############################################################################## + +from openerp.osv import fields, orm + + +class CompanyLDAP(orm.Model): + _inherit='res.company.ldap' + _columns={ + 'name_attribute': fields.char('Name Attribute', size=64, + help="Default in 'cn'. For an AD you could use 'displayName' instead."), + 'mail_attribute': fields.char('E-mail attribute', size=64, + help="Active Directory uses the 'mail' attribute."), + } + + + def get_ldap_dicts(self, cr, ids=None): + """ + Copy of auth_ldap's funtion, changing only the SQL, so that it returns + all fields in the table. + """ + + if ids: + id_clause = 'AND id IN (%s)' + args = [tuple(ids)] + else: + id_clause = '' + args = [] + cr.execute(""" + SELECT * + FROM res_company_ldap + WHERE ldap_server != '' """ + id_clause + """ ORDER BY sequence + """, args) + return cr.dictfetchall() + + + def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry): + values = super(CompanyLDAP, self).map_ldap_attributes(cr, uid, conf, + login, ldap_entry) + if conf.get('name_attribute'): + values['name'] = ldap_entry[1][conf['name_attribute']][0] + if conf.get('mail_attribute'): + values['email'] = ldap_entry[1][conf['mail_attribute']][0] + return values + diff --git a/users_ldap_mail/users_ldap_view.xml b/users_ldap_mail/users_ldap_view.xml new file mode 100644 index 000000000..9395e602f --- /dev/null +++ b/users_ldap_mail/users_ldap_view.xml @@ -0,0 +1,18 @@ + + + + + res.company.form.inherit.users_ldap_mail + res.company + + + + + + + + + + + + From 50e82c307a71f8ebc37e4747aab66c45e1bb33a3 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Mon, 29 Jul 2013 08:57:18 +0100 Subject: [PATCH 2/9] ADD defsult and sugegsted values; auto-install --- users_ldap_mail/__openerp__.py | 19 ++++++++++--------- users_ldap_mail/users_ldap_model.py | 12 ++++++------ users_ldap_mail/users_ldap_view.xml | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/users_ldap_mail/__openerp__.py b/users_ldap_mail/__openerp__.py index 97709502d..690613642 100644 --- a/users_ldap_mail/__openerp__.py +++ b/users_ldap_mail/__openerp__.py @@ -2,7 +2,7 @@ ############################################################################## # # OpenERP, Open Source Management Solution -# This module copyright (C) 2013 Daniel Reis. +# Copyright (C) 2013 Daniel Reis (https://launchpad.com/~dreis-pt) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -20,21 +20,22 @@ ############################################################################## { -"name" : "LDAP mapping for user name and e-mail", -"version" : "1.0", -"depends" : ["auth_ldap"], -"author" : "Daniel Reis", -"description": """\ +'name': "LDAP mapping for user name and e-mail", +'version': "1.0", +'depends': ["auth_ldap"], +'author': "Daniel Reis (https://launchpad.com/~dreis-pt)", +'description': """\ Allows to define the LDAP attributes to use to retrieve user name and e-mail address. The default attribute used for the name is "cn". For Active Directory, you might prefer to use "displayName" instead. AD also supports the "mail" attribute, so it can be mapped into OpenERP. """, -"category" : "Tools", -"data" : [ +'category': "Tools", +'data': [ 'users_ldap_view.xml', ], -"installable": True, +'installable': True, +'auto_install': True, } # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py index 52b3899e8..0dd3bf169 100644 --- a/users_ldap_mail/users_ldap_model.py +++ b/users_ldap_mail/users_ldap_model.py @@ -23,21 +23,22 @@ from openerp.osv import fields, orm class CompanyLDAP(orm.Model): - _inherit='res.company.ldap' - _columns={ + _inherit = 'res.company.ldap' + _columns = { 'name_attribute': fields.char('Name Attribute', size=64, help="Default in 'cn'. For an AD you could use 'displayName' instead."), 'mail_attribute': fields.char('E-mail attribute', size=64, help="Active Directory uses the 'mail' attribute."), - } - + } + _defaults = { + 'mail_attribute': 'mail', + } def get_ldap_dicts(self, cr, ids=None): """ Copy of auth_ldap's funtion, changing only the SQL, so that it returns all fields in the table. """ - if ids: id_clause = 'AND id IN (%s)' args = [tuple(ids)] @@ -51,7 +52,6 @@ class CompanyLDAP(orm.Model): """, args) return cr.dictfetchall() - def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry): values = super(CompanyLDAP, self).map_ldap_attributes(cr, uid, conf, login, ldap_entry) diff --git a/users_ldap_mail/users_ldap_view.xml b/users_ldap_mail/users_ldap_view.xml index 9395e602f..945a5651e 100644 --- a/users_ldap_mail/users_ldap_view.xml +++ b/users_ldap_mail/users_ldap_view.xml @@ -8,7 +8,7 @@ - + From 9301493482472e4665511827db1388e300b6f6c3 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Fri, 16 Aug 2013 18:07:10 +0100 Subject: [PATCH 3/9] FIX Tolerate attribute inexistance --- users_ldap_mail/users_ldap_model.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py index 0dd3bf169..33c976834 100644 --- a/users_ldap_mail/users_ldap_model.py +++ b/users_ldap_mail/users_ldap_model.py @@ -21,6 +21,8 @@ from openerp.osv import fields, orm +import logging +_log = logging.getLogger(__name__) class CompanyLDAP(orm.Model): _inherit = 'res.company.ldap' @@ -54,10 +56,16 @@ class CompanyLDAP(orm.Model): def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry): values = super(CompanyLDAP, self).map_ldap_attributes(cr, uid, conf, - login, ldap_entry) - if conf.get('name_attribute'): - values['name'] = ldap_entry[1][conf['name_attribute']][0] - if conf.get('mail_attribute'): - values['email'] = ldap_entry[1][conf['mail_attribute']][0] + login, ldap_entry) + mapping = [ + ('name', 'name_attribute'), + ('email', 'mail_attribute'), + ] + for value_key, conf_name in mapping: + try: + values[value_key] = ldap_entry[1][conf[conf_name]][0] + except KeyError: + _log.warning('No LDAP attribute "%s" found for login "%s"' % ( + conf.get(conf_name, values.get('login')))) return values From d0008687d08b88f914fbcf11369db5c4c1775b67 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Mon, 19 Aug 2013 09:10:25 +0100 Subject: [PATCH 4/9] FIX --- users_ldap_mail/users_ldap_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py index 33c976834..dd447ec3b 100644 --- a/users_ldap_mail/users_ldap_model.py +++ b/users_ldap_mail/users_ldap_model.py @@ -66,6 +66,6 @@ class CompanyLDAP(orm.Model): values[value_key] = ldap_entry[1][conf[conf_name]][0] except KeyError: _log.warning('No LDAP attribute "%s" found for login "%s"' % ( - conf.get(conf_name, values.get('login')))) + conf.get(conf_name), values.get('login'))) return values From ebc2530788246a39590b083bb931b87d520d8f25 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Mon, 19 Aug 2013 09:27:43 +0100 Subject: [PATCH 5/9] CHG Removed auto_install --- users_ldap_mail/__openerp__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/users_ldap_mail/__openerp__.py b/users_ldap_mail/__openerp__.py index 690613642..fd445f2e7 100644 --- a/users_ldap_mail/__openerp__.py +++ b/users_ldap_mail/__openerp__.py @@ -36,6 +36,5 @@ AD also supports the "mail" attribute, so it can be mapped into OpenERP. 'users_ldap_view.xml', ], 'installable': True, -'auto_install': True, } # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: From 3886577031402860eac22b068c258805f80a5a1f Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Wed, 21 Aug 2013 08:57:25 +0100 Subject: [PATCH 6/9] FIX name_attribute is given an explicit default --- users_ldap_mail/users_ldap_model.py | 5 +++-- users_ldap_mail/users_ldap_view.xml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py index dd447ec3b..8e31dedc9 100644 --- a/users_ldap_mail/users_ldap_model.py +++ b/users_ldap_mail/users_ldap_model.py @@ -28,11 +28,12 @@ class CompanyLDAP(orm.Model): _inherit = 'res.company.ldap' _columns = { 'name_attribute': fields.char('Name Attribute', size=64, - help="Default in 'cn'. For an AD you could use 'displayName' instead."), + help="LDAP attribute for the user's name. Usually 'cn' or 'displayName'"), 'mail_attribute': fields.char('E-mail attribute', size=64, - help="Active Directory uses the 'mail' attribute."), + help="LDAP attribute to use for e-mail addresses."), } _defaults = { + 'name_attribute': 'cn', 'mail_attribute': 'mail', } diff --git a/users_ldap_mail/users_ldap_view.xml b/users_ldap_mail/users_ldap_view.xml index 945a5651e..9395e602f 100644 --- a/users_ldap_mail/users_ldap_view.xml +++ b/users_ldap_mail/users_ldap_view.xml @@ -8,7 +8,7 @@ - + From 54d2c64a5ff2a6ac24a62cf44224b77fa801a116 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Wed, 21 Aug 2013 11:38:55 +0100 Subject: [PATCH 7/9] FIX no warning messages when an attribute definition is not set --- users_ldap_mail/users_ldap_model.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py index dd447ec3b..a05ac0c50 100644 --- a/users_ldap_mail/users_ldap_model.py +++ b/users_ldap_mail/users_ldap_model.py @@ -28,9 +28,10 @@ class CompanyLDAP(orm.Model): _inherit = 'res.company.ldap' _columns = { 'name_attribute': fields.char('Name Attribute', size=64, - help="Default in 'cn'. For an AD you could use 'displayName' instead."), + help="By default 'cn' is used. " + "For ActiveDirectory you might use 'displayName' instead."), 'mail_attribute': fields.char('E-mail attribute', size=64, - help="Active Directory uses the 'mail' attribute."), + help="LDAP attribute to use to retrieve em-mail address."), } _defaults = { 'mail_attribute': 'mail', @@ -63,7 +64,8 @@ class CompanyLDAP(orm.Model): ] for value_key, conf_name in mapping: try: - values[value_key] = ldap_entry[1][conf[conf_name]][0] + if conf[conf_name]: + values[value_key] = ldap_entry[1][conf[conf_name]][0] except KeyError: _log.warning('No LDAP attribute "%s" found for login "%s"' % ( conf.get(conf_name), values.get('login'))) From 96e76e11227a243497d94160d21b1bf42af4f619 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Wed, 21 Aug 2013 11:45:42 +0100 Subject: [PATCH 8/9] (fix incorrect commit) --- users_ldap_mail/users_ldap_view.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users_ldap_mail/users_ldap_view.xml b/users_ldap_mail/users_ldap_view.xml index 9395e602f..945a5651e 100644 --- a/users_ldap_mail/users_ldap_view.xml +++ b/users_ldap_mail/users_ldap_view.xml @@ -8,7 +8,7 @@ - + From 2bd413abca9da7125767af2e3cd56a90f1cefd2d Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Tue, 3 Sep 2013 08:39:30 +0100 Subject: [PATCH 9/9] Change name_attribute defaults to 'cn' --- users_ldap_mail/users_ldap_model.py | 1 + users_ldap_mail/users_ldap_view.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/users_ldap_mail/users_ldap_model.py b/users_ldap_mail/users_ldap_model.py index a05ac0c50..0bb72a2de 100644 --- a/users_ldap_mail/users_ldap_model.py +++ b/users_ldap_mail/users_ldap_model.py @@ -34,6 +34,7 @@ class CompanyLDAP(orm.Model): help="LDAP attribute to use to retrieve em-mail address."), } _defaults = { + 'name_attribute': 'cn', 'mail_attribute': 'mail', } diff --git a/users_ldap_mail/users_ldap_view.xml b/users_ldap_mail/users_ldap_view.xml index 945a5651e..9395e602f 100644 --- a/users_ldap_mail/users_ldap_view.xml +++ b/users_ldap_mail/users_ldap_view.xml @@ -8,7 +8,7 @@ - +