Initial commit

This commit is contained in:
Holger Sielaff
2025-06-26 08:41:56 +02:00
commit 69e2718826
16 changed files with 808 additions and 0 deletions

18
lib/__init__.py Normal file
View File

@@ -0,0 +1,18 @@
from . import ldap
CONFIG_PARAMETER_PREFIX = 'ldap_partner_sync_settings'
def param_name(param):
"""Just concatinates the name parts
of an sync param
"""
return f'{CONFIG_PARAMETER_PREFIX}.{param}'
def get_config(env, param, default=None):
"""Returns the param depending
on env
"""
p = param_name(param)
return env['ir.config_parameter'].sudo().get_param(p, default)

47
lib/ldap.py Normal file
View File

@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
import logging
"""
We want Flectra too
"""
try:
from odoo.exceptions import UserError
except ModuleNotFoundError:
from flectra.exceptions import UserError
from ldap3 import Server, Connection
from ldap3.core.exceptions import LDAPException
_logger = logging.getLogger(__name__)
def get_base_o_params(env, base_dn: str = None):
from . import get_config
base_dn = base_dn or get_config(env, 'ldap_base', 'o=customer,dc=my-company')
name = base_dn.split(',')[0]
root = base_dn.split(',')[1:]
objcc = name.split('=')[0]
if objcc != 'o':
raise ValueError(f'Root obect has to be "o" for organization - not {objcc}')
return name, root, {'objectClass': ['organization'], 'o': name.split('=')[1]}
def get_ldap_connection(env):
from . import get_config
"""LDAP Verbindung mit ldap3 herstellen"""
ldap_url = get_config(env, 'ldap_host', 'ldap://localhost:389')
ldap_bind_dn = get_config(env, 'ldap_user', 'cn=admin,dc=example,dc=com')
ldap_password = get_config(env, 'ldap_password', 'admin')
try:
server = Server(ldap_url)
conn = Connection(
server,
ldap_bind_dn,
ldap_password,
auto_bind=True
)
return conn
except LDAPException as e:
_logger.error(f"LDAP Connection failed: {str(e)}")
raise UserError(f"LDAP Verbindung fehlgeschlagen: {str(e)}")