|
@ -20,7 +20,9 @@ |
|
|
############################################################################## |
|
|
############################################################################## |
|
|
|
|
|
|
|
|
import re |
|
|
import re |
|
|
from openerp.osv import orm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from openerp import models, api |
|
|
|
|
|
from openerp.exceptions import UserError |
|
|
import logging |
|
|
import logging |
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
_logger = logging.getLogger(__name__) |
|
@ -31,10 +33,11 @@ except ImportError: |
|
|
_logger.debug('Can not `from ldap.filter import filter_format`.') |
|
|
_logger.debug('Can not `from ldap.filter import filter_format`.') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CompanyLDAP(orm.Model): |
|
|
|
|
|
|
|
|
class CompanyLDAP(models.Model): |
|
|
_inherit = 'res.company.ldap' |
|
|
_inherit = 'res.company.ldap' |
|
|
|
|
|
|
|
|
def action_populate(self, cr, uid, ids, context=None): |
|
|
|
|
|
|
|
|
@api.multi |
|
|
|
|
|
def action_populate(self): |
|
|
""" |
|
|
""" |
|
|
Prepopulate the user table from one or more LDAP resources. |
|
|
Prepopulate the user table from one or more LDAP resources. |
|
|
|
|
|
|
|
@ -43,16 +46,13 @@ class CompanyLDAP(orm.Model): |
|
|
|
|
|
|
|
|
Return the number of users created (as far as we can tell). |
|
|
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) |
|
|
|
|
|
|
|
|
users_pool = self.env['res.users'] |
|
|
|
|
|
users_no_before = users_pool.search_count([]) |
|
|
logger = logging.getLogger('orm.ldap') |
|
|
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", |
|
|
|
|
|
self.ids) |
|
|
|
|
|
|
|
|
for conf in self.get_ldap_dicts(cr, ids): |
|
|
|
|
|
|
|
|
for conf in self.get_ldap_dicts(self.cr, self.ids): |
|
|
if not conf['create_user']: |
|
|
if not conf['create_user']: |
|
|
continue |
|
|
continue |
|
|
attribute_match = re.search( |
|
|
attribute_match = re.search( |
|
@ -60,33 +60,29 @@ class CompanyLDAP(orm.Model): |
|
|
if attribute_match: |
|
|
if attribute_match: |
|
|
login_attr = attribute_match.group(1) |
|
|
login_attr = attribute_match.group(1) |
|
|
else: |
|
|
else: |
|
|
raise orm.except_orm( |
|
|
|
|
|
"No login attribute found", |
|
|
|
|
|
|
|
|
raise UserError( |
|
|
|
|
|
"No login attribute found" |
|
|
"Could not extract login attribute from filter %s" % |
|
|
"Could not extract login attribute from filter %s" % |
|
|
conf['ldap_filter']) |
|
|
conf['ldap_filter']) |
|
|
ldap_filter = filter_format(conf['ldap_filter'] % '*', ()) |
|
|
ldap_filter = filter_format(conf['ldap_filter'] % '*', ()) |
|
|
for result in self.query(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) |
|
|
|
|
|
|
|
|
self.get_or_create_user(conf, result[1][login_attr][0], result) |
|
|
|
|
|
|
|
|
users_no_after = users_pool.search( |
|
|
|
|
|
cr, uid, [], context=context, count=True) |
|
|
|
|
|
|
|
|
users_no_after = users_pool.search_count([]) |
|
|
users_created = users_no_after - users_no_before |
|
|
users_created = users_no_after - users_no_before |
|
|
logger.debug("%d users created", users_created) |
|
|
logger.debug("%d users created", users_created) |
|
|
return users_created |
|
|
return users_created |
|
|
|
|
|
|
|
|
def populate_wizard(self, cr, uid, ids, context=None): |
|
|
|
|
|
|
|
|
@api.multi |
|
|
|
|
|
def populate_wizard(self): |
|
|
""" |
|
|
""" |
|
|
GUI wrapper for the populate method that reports back |
|
|
GUI wrapper for the populate method that reports back |
|
|
the number of users created. |
|
|
the number of users created. |
|
|
""" |
|
|
""" |
|
|
if not ids: |
|
|
|
|
|
|
|
|
if not self: |
|
|
return |
|
|
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) |
|
|
|
|
|
|
|
|
wizard_obj = self.env['res.company.ldap.populate_wizard'] |
|
|
|
|
|
res_id = wizard_obj.create({'ldap_id': self.id}).id |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
'name': wizard_obj._description, |
|
|
'name': wizard_obj._description, |
|
@ -94,7 +90,7 @@ class CompanyLDAP(orm.Model): |
|
|
'view_mode': 'form', |
|
|
'view_mode': 'form', |
|
|
'res_model': wizard_obj._name, |
|
|
'res_model': wizard_obj._name, |
|
|
'domain': [], |
|
|
'domain': [], |
|
|
'context': context, |
|
|
|
|
|
|
|
|
'context': self.env.context, |
|
|
'type': 'ir.actions.act_window', |
|
|
'type': 'ir.actions.act_window', |
|
|
'target': 'new', |
|
|
'target': 'new', |
|
|
'res_id': res_id, |
|
|
'res_id': res_id, |
|
|