# Path: lib/Wrappers/Logger.py
# class Logger:
#     def __init__(self, name):
#         if 'UNITTESTING' in os.environ:
#             self.logging = NullLogger()
#         else:
#             settings = Settings().getSettings()
#             logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                 level=settings["logs"]["level"])
#             self.logging = logging.getLogger(name)
# 
#     def debug(self, *args, **kwargs):
#         self.logging.debug(*args, **kwargs)
# 
#     def info(self, *args, **kwargs):
#         self.logging.info(*args, **kwargs)
# 
#     def warning(self, *args, **kwargs):
#         self.logging.warning(*args, **kwargs)
# 
#     def error(self, *args, **kwargs):
#         self.logging.error(*args, **kwargs)
# 
#     def critical(self, *args, **kwargs):
#         self.logging.critical(*args, **kwargs)
# 
#     def log(self, *args, **kwargs):
#         self.logging.log(*args, **kwargs)
# 
# Path: lib/Settings.py
# class Settings:
#     def __init__(self,
#                  override_settings_path='settings.cfg'):
#         self.settingsFilePath = override_settings_path
#         self.parser = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
#         self.settings = None
#         self.getSettings()
# 
#     def getSettings(self):
#         self.parser.read('./settings.cfg')
#         self.settings = {section: dict(**self.parser[section]) for section in self.parser.sections()}
# 
#         return self.settings
# 
#     def getServiceIp(self, service):
#         return self.settings['general']['my_ip']


from ldap3 import Server, Connection, ALL, SUBTREE
from lib.Wrappers.Logger import Logger
from lib.Settings import Settings
import ldap3 as ldap
import hashlib
import base64
            except ldap.LDAPEntryAlreadyExistsResult:
                self.logger.error("Could not create user, duplicated " + user)
                return False
            except Exception as e:
                self.logger.error("Could not create user: " + str(e))
                return False

            return True

        def deleteUser(self, user):
            deleteDN = "cn="+user+","+self.dn_base
            self.ldapClient.delete(deleteDN)
            return True

        def findUser(self, user):
            self.ldapClient.search(search_base=self.dn_base,
                                   search_filter='(&(objectClass=inetOrgPerson)(cn=' + user + '))',
                                   search_scope=SUBTREE,
                                   attributes=['cn'])

            usernames = []
            for result in self.ldapClient.response:
                cn = result['attributes']['cn'][0]
                if cn:
                    usernames.append(cn)

            return usernames

    instance = None


# Path: lib/Wrappers/Logger.py
# class Logger:
#     def __init__(self, name):
#         if 'UNITTESTING' in os.environ:
#             self.logging = NullLogger()
#         else:
#             settings = Settings().getSettings()
#             logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                 level=settings["logs"]["level"])
#             self.logging = logging.getLogger(name)
# 
#     def debug(self, *args, **kwargs):
#         self.logging.debug(*args, **kwargs)
# 
#     def info(self, *args, **kwargs):
#         self.logging.info(*args, **kwargs)
# 
#     def warning(self, *args, **kwargs):
#         self.logging.warning(*args, **kwargs)
# 
#     def error(self, *args, **kwargs):
#         self.logging.error(*args, **kwargs)
# 
#     def critical(self, *args, **kwargs):
#         self.logging.critical(*args, **kwargs)
# 
#     def log(self, *args, **kwargs):
#         self.logging.log(*args, **kwargs)
# 
# Path: lib/Settings.py
# class Settings:
#     def __init__(self,
#                  override_settings_path='settings.cfg'):
#         self.settingsFilePath = override_settings_path
#         self.parser = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
#         self.settings = None
#         self.getSettings()
# 
#     def getSettings(self):
#         self.parser.read('./settings.cfg')
#         self.settings = {section: dict(**self.parser[section]) for section in self.parser.sections()}
# 
#         return self.settings
# 
#     def getServiceIp(self, service):
#         return self.settings['general']['my_ip']


# Path: lib/Users/Ldap.py
from ldap3 import Server, Connection, ALL, SUBTREE
from lib.Wrappers.Logger import Logger
from lib.Settings import Settings
import ldap3 as ldap
import hashlib
import base64

            except ldap.LDAPEntryAlreadyExistsResult:
                self.logger.error("Could not create user, duplicated " + user)
                return False
            except Exception as e:
                self.logger.error("Could not create user: " + str(e))
                return False

            return True

        def deleteUser(self, user):
            deleteDN = "cn="+user+","+self.dn_base
            self.ldapClient.delete(deleteDN)
            return True

        def findUser(self, user):
            self.ldapClient.search(search_base=self.dn_base,
                                   search_filter='(&(objectClass=inetOrgPerson)(cn=' + user + '))',
                                   search_scope=SUBTREE,
                                   attributes=['cn'])

            usernames = []
            for result in self.ldapClient.response:
                cn = result['attributes']['cn'][0]
                if cn:
                    usernames.append(cn)

            return usernames

    instance = None


    def __init__(self):