From f3d90a2b7db8b9436cccd4c4dfde8d7cca80c950 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 3 Feb 2019 12:59:14 +0100 Subject: [PATCH] Add utility code to get max UID Other changes: - Introduce logging - Introduce .env config parameters - LDAP_SEARCH_BASE: The base used in the LDAP search to find uid - IPV6_WORK_USER_GROUP: The LDAP group to which the newly added user should belong to --- ipv6work/settings.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ipv6work/settings.py b/ipv6work/settings.py index 7856304..91b852e 100644 --- a/ipv6work/settings.py +++ b/ipv6work/settings.py @@ -12,9 +12,12 @@ https://docs.djangoproject.com/en/2.1/ref/settings/ import os import ldap +import logging from decouple import config, Csv from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion +logger = logging.getLogger(__name__) + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -207,6 +210,44 @@ LOGGING = { }, } +LDAP_SEARCH_BASE=config( + 'LDAP_SEARCH_BASE', + default='ou=users,dc=ungleich,dc=ch' +) + +LDAP_MAX_UID_PATH = os.path.join( + os.path.abspath(os.path.dirname(__file__)), + 'ldap_max_uid_file' +) + +IPV6_WORK_USER_GROUP = config('IPV6_WORK_USER_GROUP', cast=int) + + +def set_max_uid(max_uid): + """ + a utility function to save max_uid value to a file + + :param max_uid: an integer representing the max uid + :return: + """ + with open(LDAP_MAX_UID_PATH, 'w+') as handler: + handler.write(max_uid) + + +def get_max_uid(): + """ + A utility function to read the max uid value that was previously set + + :return: An integer representing the max uid value that was previously set + """ + try: + with open(LDAP_MAX_UID_PATH, 'r+') as handler: + return int(handler.read()) + except FileNotFoundError as fnfe: + logger.error("File not found : " + str(fnfe)) + ret = config('DEFAULT_START_UID', cast=int, default=10000) + logger.error("So, returing UID={}".format(ret)) + if config('ENABLE_DEBUG_LOG', cast=bool, default=False): loggers_dict = {} LOGGING['handlers']['file'] = {