# -*- coding: utf-8 -*- """ Adds ldap_partner_sync params to res.config.settings We want Flectra too """ try: from odoo import models, fields, api from odoo.exceptions import UserError except ModuleNotFoundError: from flectra import models, fields, api from flectra.exceptions import UserError import logging from ldap3 import SUBTREE # Maybe we can use # from . import param_name from ..lib import CONFIG_PARAMETER_PREFIX from ..lib.ldap import get_ldap_connection, get_base_o_params from ..lib import param_name _logger = logging.getLogger(__name__) class ResConfigSettings(models.TransientModel): _inherit = 'res.config.settings' partner_sync_on = fields.Boolean(string='Sync aktiv', required=False, default=False, config_parameter=param_name('partner_sync_on')) ldap_base = fields.Char(string='LDAP Base', required=True, config_parameter=param_name('ldap_base')) ldap_host = fields.Char(string='LDAP Host', required=True, config_parameter=param_name('ldap_host')) ldap_port = fields.Char(string='LDAP Port', required=True, config_parameter=param_name('ldap_port')) ldap_user = fields.Char(string='LDAP User', required=True, config_parameter=param_name('ldap_user')) ldap_password = fields.Char(string='LDAP Password', required=True, config_parameter=param_name('ldap_password')) company_prefix = fields.Char(string='Company Prefix', required=True, default="company", config_parameter=param_name('company_prefix')) person_prefix = fields.Char(string='Person Prefix', required=True, default="customer", config_parameter=param_name('person_prefix')) affix_prop = fields.Char(string='Affix Property', required=True, default="id", config_parameter=param_name('affix_prop')) def button_reexport_ldap(self): """We want this button right in the Settings Panel so we call it as object """ base_dn, base_name, base_params = get_base_o_params(self.env, self.ldap_base) with get_ldap_connection(self.env) as conn: try: conn.search(base_dn, '(objectClass=*)', SUBTREE, attributes=['*']) if conn.entries: try: _logger.info(f'Delete {dn}') conn.delete(base_dn) except Exception as e: _logger.error(f"Fehler beim Löschen von {base_dn}: {type(e)} {str(e)}") raise UserError(f"Fehler beim Löschen von {base_dn}: {type(e)} {str(e)}") try: _logger.info(f'Add {base_dn} with params {base_params}') conn.add(base_dn, base_params.pop('objectClass'), base_params) except Exception as e: _logger.error(f"Fehler beim Anlegen von {base_dn}: {type(e)} {str(e)}") raise UserError(f'Could not create {base_dn}') for partner in self.env['res.partner'].search([ ('is_company', '=', False), ]): partner._sync_to_ldap() except UserError as e: raise UserError(f"LDAP Fehler: {str(e)}") return {'type': 'ir.actions.client', 'tag': 'reload'}