48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
# -*- 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)}")
|