Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from main import logging
- try:
- import ldap3
- except ImportError:
- raise ImportError('LDAP3 not found. Are you in the "venv" and did you install the requirements.txt ?')
- try:
- from credentials import AD_SERVER_URI
- from credentials import AD_ADMIN_LOGIN
- from credentials import AD_ADMIN_PASSWORD
- from credentials import AD_BASE_SEARCH
- except ImportError:
- raise ImportError("Could not find credentials. Make sure credentials.py is there.")
- log = logging.getLogger(module)
- class AD():
- def __init__(self):
- self.server_Uri = AD_SERVER_URI
- self.admin_login = AD_ADMIN_LOGIN
- self.admin_password = AD_ADMIN_PASSWORD
- self.base_search = AD_BASE_SEARCH
- self.port = 636
- self.ssl = True
- def connect(self):
- try:
- server = ldap3.Server(self.uri, port= self.port, use_ssl= self.ssl, get_info= ldap3.ALL)
- self.connect = ldap3.Connection(server, self.admin_login, self.admin_password)
- self.connect.bind()
- log.debug(self.connect.extend.standard.who_am_i())
- except Exception as e:
- log.error(e)
- def printResult(self):
- resultCode = self.connect.result['result']
- description = self.connect.result['description']
- log.info("\n\nResult code : " + str(resultCode) + ", " + str(description))
- @property
- def is_connected(self):
- return self.connect.bind()
- def createUserAD(self):
- if self.is_connected:
- current_password = 'CeCiEstUnPassWord49'
- user_dn = "CN=test T. test,CN=Users,DC=ac,DC=justice,DC=fr"
- self.connect.add(
- user_dn,
- ['user'],
- {
- 'sn': 'test',
- 'cn': 'test T. test',
- 'description': 'Compte de test Kerberos',
- 'userPrincipalName': 'test@ac.justice.fr'
- })
- self.connect.extend.microsoft.modify_password(user_dn, current_password, controls=None)
- self.connect.modify(
- user_dn,
- {'pwdLastSet': (ldap3.MODIFY_REPLACE, ["0"])})
- else:
- log.error("AD not connected.")
- def deleteUser(self, search_filter):
- if self.is_connected:
- log.info("\nBase : " + str(self.base_search))
- log.info("\nSearch Filter to delete the user : " + str(search_filter))
- # ldap3.extend.microsoft.modifyPassword.ad_modify_password
- # gets properly quoted and utf-16le encoded.
- if self.connect.search(search_base = self.base_search, search_filter= search_filter, search_scope=ldap3.SUBTREE, attributes = ['cn','givenName'], paged_size = 5):
- for entry in self.connect.response:
- user_dn= entry.get("dn")
- # perform the Delete operation
- self.connect.delete(user_dn)
- log.info("\nUser with User_DN = " + user_dn + " deleted.")
- self.printResult()
- else:
- log.debug("\n\n\nThe user you tried to delete doesn't exist in this base.")
- else:
- log.error("AD not connected.")
Add Comment
Please, Sign In to add comment