Skip to content
Snippets Groups Projects
Commit 8904ab96 authored by Julian's avatar Julian
Browse files

Added lazyconfig_* functions to configure LDAP models with app config

parent e967d2b5
No related branches found
No related tags found
No related merge requests found
...@@ -146,6 +146,7 @@ disable=missing-module-docstring, ...@@ -146,6 +146,7 @@ disable=missing-module-docstring,
too-few-public-methods, too-few-public-methods,
method-hidden, method-hidden,
bad-continuation, bad-continuation,
too-many-ancestors,
# Enable the message, report, category or checker with the given id(s). You can # Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option # either give multiple identifier separated by comma (,) or put this option
......
from collections import UserString, UserList
from flask import current_app
class LazyConfigString(UserString):
def __init__(self, seq=None, key=None, default=None, error=True):
# pylint: disable=super-init-not-called
self.__seq = seq
self.__key = key
self.__default = default
self.__error = error
@property
def data(self):
if self.__seq is not None:
obj = self.__seq
elif self.__error:
obj = current_app.config[self.__key]
else:
obj = current_app.config.get(self.__key, self.__default)
return str(obj)
def __bytes__(self):
return self.data.encode()
def __get__(self, obj, owner=None):
return self.data
def lazyconfig_str(key, **kwargs):
return LazyConfigString(None, key, **kwargs)
class LazyConfigList(UserList):
def __init__(self, seq=None, key=None, default=None, error=True):
# pylint: disable=super-init-not-called
self.__seq = seq
self.__key = key
self.__default = default
self.__error = error
@property
def data(self):
if self.__seq is not None:
obj = self.__seq
elif self.__error:
obj = current_app.config[self.__key]
else:
obj = current_app.config.get(self.__key, self.__default)
return obj
def __get__(self, obj, owner=None):
return self.data
def lazyconfig_list(key, **kwargs):
return LazyConfigList(None, key, **kwargs)
from uffd.ldap import LDAPModel, LDAPAttribute from uffd.ldap import LDAPModel, LDAPAttribute
from uffd.lazyconfig import lazyconfig_str, lazyconfig_list
class Mail(LDAPModel): class Mail(LDAPModel):
ldap_base = 'ou=postfix,dc=example,dc=com' ldap_base = lazyconfig_str('LDAP_BASE_MAIL')
ldap_dn_attribute = 'uid' ldap_dn_attribute = 'uid'
ldap_dn_base = 'ou=postfix,dc=example,dc=com' ldap_dn_base = lazyconfig_str('LDAP_BASE_MAIL')
ldap_filter = '(objectClass=postfixVirtual)' ldap_filter = '(objectClass=postfixVirtual)'
ldap_object_classes = ['top', 'postfixVirtual'] ldap_object_classes = lazyconfig_list('MAIL_LDAP_OBJECTCLASSES')
uid = LDAPAttribute('uid') uid = LDAPAttribute('uid')
receivers = LDAPAttribute('mailacceptinggeneralid', multi=True) receivers = LDAPAttribute('mailacceptinggeneralid', multi=True)
......
...@@ -5,6 +5,7 @@ from flask import current_app ...@@ -5,6 +5,7 @@ from flask import current_app
from ldap3.utils.hashed import hashed, HASHED_SALTED_SHA512 from ldap3.utils.hashed import hashed, HASHED_SALTED_SHA512
from uffd.ldap import LDAPModel, LDAPAttribute, LDAPRelation from uffd.ldap import LDAPModel, LDAPAttribute, LDAPRelation
from uffd.lazyconfig import lazyconfig_str, lazyconfig_list
def get_next_uid(): def get_next_uid():
max_uid = current_app.config['LDAP_USER_MIN_UID'] max_uid = current_app.config['LDAP_USER_MIN_UID']
...@@ -17,11 +18,11 @@ def get_next_uid(): ...@@ -17,11 +18,11 @@ def get_next_uid():
return next_uid return next_uid
class User(LDAPModel): class User(LDAPModel):
ldap_base = 'ou=users,dc=example,dc=com' ldap_base = lazyconfig_str('LDAP_BASE_USER')
ldap_dn_attribute = 'uid' ldap_dn_attribute = 'uid'
ldap_dn_base = 'ou=users,dc=example,dc=com' ldap_dn_base = lazyconfig_str('LDAP_BASE_USER')
ldap_filter = '(objectClass=person)' ldap_filter = '(objectClass=person)'
ldap_object_classes = ['top', 'inetOrgPerson', 'organizationalPerson', 'person', 'posixAccount'] ldap_object_classes = lazyconfig_list('LDAP_USER_OBJECTCLASSES')
uid = LDAPAttribute('uidNumber', default=get_next_uid) uid = LDAPAttribute('uidNumber', default=get_next_uid)
loginname = LDAPAttribute('uid') loginname = LDAPAttribute('uid')
...@@ -101,7 +102,7 @@ class User(LDAPModel): ...@@ -101,7 +102,7 @@ class User(LDAPModel):
return True return True
class Group(LDAPModel): class Group(LDAPModel):
ldap_base = 'ou=groups,dc=example,dc=com' ldap_base = lazyconfig_str('LDAP_BASE_GROUPS')
ldap_filter = '(objectClass=groupOfUniqueNames)' ldap_filter = '(objectClass=groupOfUniqueNames)'
gid = LDAPAttribute('gidNumber') gid = LDAPAttribute('gidNumber')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment