From 112279a96692a1a0d6c8399968ea73a1f54a2adf Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Tue, 23 Jul 2013 12:21:53 +0100 Subject: [PATCH 01/48] Therp's modules to sync OpenERP with LDAP directories --- users_ldap_populate/__init__.py | 1 + users_ldap_populate/__openerp__.py | 49 ++++++++++ users_ldap_populate/i18n/nl.po | 56 ++++++++++++ users_ldap_populate/model/__init__.py | 2 + users_ldap_populate/model/populate_wizard.py | 42 +++++++++ users_ldap_populate/model/users_ldap.py | 96 ++++++++++++++++++++ users_ldap_populate/view/populate_wizard.xml | 18 ++++ users_ldap_populate/view/users_ldap.xml | 20 ++++ 8 files changed, 284 insertions(+) create mode 100644 users_ldap_populate/__init__.py create mode 100644 users_ldap_populate/__openerp__.py create mode 100644 users_ldap_populate/i18n/nl.po create mode 100644 users_ldap_populate/model/__init__.py create mode 100644 users_ldap_populate/model/populate_wizard.py create mode 100644 users_ldap_populate/model/users_ldap.py create mode 100644 users_ldap_populate/view/populate_wizard.xml create mode 100644 users_ldap_populate/view/users_ldap.xml diff --git a/users_ldap_populate/__init__.py b/users_ldap_populate/__init__.py new file mode 100644 index 0000000000..16e8b082fe --- /dev/null +++ b/users_ldap_populate/__init__.py @@ -0,0 +1 @@ +import model diff --git a/users_ldap_populate/__openerp__.py b/users_ldap_populate/__openerp__.py new file mode 100644 index 0000000000..e73d47c879 --- /dev/null +++ b/users_ldap_populate/__openerp__.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2012 Therp BV (). +# +# 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 Populate", + "version": "1.0", + "author": "Therp BV", + "category": 'Tools', + "description": """ +This module allows to prepopulate the user database with all entries in the LDAP +database. + +In order to schedule the population of the user database on a regular basis, +create a new scheduled action with the following properties: + +- Object: res.company.ldap +- Function: action_populate +- Arguments: [res.company.ldap.id] + +Substitute res.company.ldap.id with the actual id of the res.company.ldap object you want to query. + +""", + "depends": [ + 'auth_ldap', + ], + "data": [ + 'view/users_ldap.xml', + 'view/populate_wizard.xml', + ], + 'installable': True, +} +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/users_ldap_populate/i18n/nl.po b/users_ldap_populate/i18n/nl.po new file mode 100644 index 0000000000..ea0bf5db9a --- /dev/null +++ b/users_ldap_populate/i18n/nl.po @@ -0,0 +1,56 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * users_ldap_populate +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 6.1\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2012-07-31 13:08+0000\n" +"PO-Revision-Date: 2012-07-31 13:08+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: users_ldap_populate +#: field:res.company.ldap.populate_wizard,ldap_id:0 +msgid "LDAP Configuration" +msgstr "LDAP configuratie" + +#. module: users_ldap_populate +#: view:res.company:0 +msgid "Populate user database" +msgstr "Gebruikersbestand bevolken" + +#. module: users_ldap_populate +#: field:res.company.ldap.populate_wizard,name:0 +msgid "Name" +msgstr "Naam" + +#. module: users_ldap_populate +#: view:res.company:0 +msgid "Populate" +msgstr "Bevolken" + +#. module: users_ldap_populate +#: model:ir.model,name:users_ldap_populate.model_res_company_ldap_populate_wizard +msgid "Populate users from LDAP" +msgstr "Gebruikersbestand bevolken op basis van LDAP" + +#. module: users_ldap_populate +#: field:res.company.ldap.populate_wizard,users_created:0 +msgid "Number of users created" +msgstr "Aantal aangemaakte gebruikers" + +#. module: users_ldap_populate +#: model:ir.model,name:users_ldap_populate.model_res_company_ldap +msgid "res.company.ldap" +msgstr "res.company.ldap" + +#. module: users_ldap_populate +#: view:res.company.ldap.populate_wizard:0 +msgid "OK" +msgstr "OK" diff --git a/users_ldap_populate/model/__init__.py b/users_ldap_populate/model/__init__.py new file mode 100644 index 0000000000..1fa6a8664d --- /dev/null +++ b/users_ldap_populate/model/__init__.py @@ -0,0 +1,2 @@ +import users_ldap +import populate_wizard diff --git a/users_ldap_populate/model/populate_wizard.py b/users_ldap_populate/model/populate_wizard.py new file mode 100644 index 0000000000..6677e2245a --- /dev/null +++ b/users_ldap_populate/model/populate_wizard.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2012 Therp BV (). +# +# 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 osv import osv, fields + +class CompanyLDAPPopulateWizard(osv.TransientModel): + _name = 'res.company.ldap.populate_wizard' + _description = 'Populate users from LDAP' + _columns = { + 'name': fields.char('Name', size=16), + 'ldap_id': fields.many2one( + 'res.company.ldap', 'LDAP Configuration'), + 'users_created': fields.integer( + 'Number of users created', readonly=True), + } + + def create(self, cr, uid, vals, context=None): + ldap_pool = self.pool.get('res.company.ldap') + users_pool = self.pool.get('res.users') + if 'ldap_id' in vals: + vals['users_created'] = ldap_pool.action_populate( + cr, uid, vals['ldap_id'], context=context) + return super(CompanyLDAPPopulateWizard, self).create( + cr, uid, vals, context=None) diff --git a/users_ldap_populate/model/users_ldap.py b/users_ldap_populate/model/users_ldap.py new file mode 100644 index 0000000000..f58388e8a2 --- /dev/null +++ b/users_ldap_populate/model/users_ldap.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2012 Therp BV (). +# +# 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 re +from ldap.filter import filter_format +from openerp.osv import orm, fields +import openerp.exceptions +import logging + +class CompanyLDAP(orm.Model): + _inherit = 'res.company.ldap' + + def action_populate(self, cr, uid, ids, context=None): + """ + Prepopulate the user table from one or more LDAP resources. + + Obviously, the option to create users must be toggled in + the LDAP configuration. + + Return the number of users created (as far as we can tell). + """ + if isinstance(ids, (int, float)): + ids = [ids] + + 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) + + for conf in self.get_ldap_dicts(cr, ids): + if not conf['create_user']: + continue + attribute_match = re.search( + '([a-zA-Z_]+)=\%s', conf['ldap_filter']) + if attribute_match: + login_attr = attribute_match.group(1) + else: + raise osv.except_osv( + "No login attribute found", + "Could not extract login attribute from filter %s" % + conf['ldap_filter']) + ldap_filter = filter_format(conf['ldap_filter'] % '*', ()) + for result in self.query(conf, ldap_filter): + self.get_or_create_user( + cr, uid, conf, result[1][login_attr][0], result) + + users_no_after = users_pool.search( + cr, uid, [], context=context, count=True) + users_created = users_no_after - users_no_before + logger.debug("%d users created", users_created) + return users_created + + def populate_wizard(self, cr, uid, ids, context=None): + """ + GUI wrapper for the populate method that reports back + the number of users created. + """ + if not ids: + return + if isinstance(ids, (int, float)): + ids = [ids] + wizard_obj = self.pool.get('res.company.ldap.populate_wizard') + res_id = wizard_obj.create( + cr, uid, {'ldap_id': ids[0]}, context=context) + + return { + 'name': wizard_obj._description, + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': wizard_obj._name, + 'domain': [], + 'context': context, + 'type': 'ir.actions.act_window', + 'target': 'new', + 'res_id': res_id, + 'nodestroy': True, + } diff --git a/users_ldap_populate/view/populate_wizard.xml b/users_ldap_populate/view/populate_wizard.xml new file mode 100644 index 0000000000..5b80f72148 --- /dev/null +++ b/users_ldap_populate/view/populate_wizard.xml @@ -0,0 +1,18 @@ + + + + + Add populate button to ldap view + res.company.ldap.populate_wizard + form + +
+ + +