Advertisement
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 LDAP_SERVER_URI
- from credentials import LDAP_ADMIN_LOGIN
- from credentials import LDAP_ADMIN_PASSWORD
- from credentials import LDAP_BASE_SEARCH
- except ImportError:
- raise ImportError("Could not find credentials. Make sure credentials.py is there.")
- log = logging.getLogger(module)
- class LDAP():
- def __init__(self):
- self.server_Uri = LDAP_SERVER_URI
- self.admin_login = LDAP_ADMIN_LOGIN
- self.admin_password = LDAP_ADMIN_PASSWORD
- self.base_search = LDAP_BASE_SEARCH
- self.port = 389
- self.ssl = False
- 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 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("LDAP not connected.")
- def createUserLDAP(self,igcID):
- if self.is_connected:
- log.info("Creating user with igcID : " + igcID)
- connect.add('uid=XX19674134,ou=people,dc=justice,dc=fr', ['inetOrgPerson','justicePerson'], {'displayName': 'test', 'sn': 'test', 'cn': 'test T. test', 'igcId': igcID, 'logonId': 'test T. test', 'userPassword': 'CeCiEstUnPassWord49' })
- self.printResult()
- resultCode = connect.result['result']
- description = connect.result['description']
- return resultCode, description
- else:
- log.error("LDAP not connected.")
- def modifyCurrentIgcID(self):
- if self.is_connected:
- log.info("Modifying current IgcID of indiceAgent for next user creation.")
- # dn : ou=indiceAgent,ou=indices,ou=infrastructure,dc=justice,dc=fr
- if self.connect.search(search_base="ou=indices,ou=infrastructure,dc=justice,dc=fr", search_filter="(ou=indiceAgent)", search_scope=SUBTREE, attributes="description"):
- for entry in self.connect.response:
- self.connect.modify("ou=indiceAgent,ou=indices,ou=infrastructure,dc=justice,dc=fr",
- {'description':[(MODIFY_REPLACE, [newIgcID])]})
- log.info("indiceAgent has now an IgcID attribute value of : " + str(newIgcID))
- self.printResult()
- else:
- log.error("LDAP not connected.")
- def getIgcID():
- if self.is_connected:
- igcID_user =""
- if conn.search(search_base="ou=indices,ou=infrastructure,dc=justice,dc=fr", search_filter="(ou=indiceAgent)", search_scope=SUBTREE, attributes="description"):
- for entry in conn.response:
- # Take the string igcID from the list description from the dict attributes
- igcID_user= entry['attributes'].get('description')[0]
- print("\nCurrent IgcID = " + str(igcID_user))
- conn.unbind()
- return igcID_user
- else:
- log.error("LDAP not connected.")
- def incrementIgcID(igcID):
- # Parse the letter "L" and the number
- # Increment +1 the number
- # put L and new number together
- firstLetter = igcID[0]
- nb = int(igcID[1:])
- nb = nb+1
- nxtIgcID = firstLetter + "0" + str(nb)
- return nxtIgcID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement