Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. from main import logging
  2. try:
  3. import ldap3
  4. except ImportError:
  5. raise ImportError('LDAP3 not found. Are you in the "venv" and did you install the requirements.txt ?')
  6. try:
  7. from credentials import LDAP_SERVER_URI
  8. from credentials import LDAP_ADMIN_LOGIN
  9. from credentials import LDAP_ADMIN_PASSWORD
  10. from credentials import LDAP_BASE_SEARCH
  11. except ImportError:
  12. raise ImportError("Could not find credentials. Make sure credentials.py is there.")
  13.  
  14. log = logging.getLogger(module)
  15.  
  16.  
  17. class LDAP():
  18.  
  19. def __init__(self):
  20. self.server_Uri = LDAP_SERVER_URI
  21. self.admin_login = LDAP_ADMIN_LOGIN
  22. self.admin_password = LDAP_ADMIN_PASSWORD
  23. self.base_search = LDAP_BASE_SEARCH
  24. self.port = 389
  25. self.ssl = False
  26.  
  27. def connect(self):
  28. try:
  29. server = ldap3.Server(self.uri, port= self.port, use_ssl= self.ssl, get_info= ldap3.ALL)
  30. self.connect = ldap3.Connection(server, self.admin_login, self.admin_password)
  31. self.connect.bind()
  32. #log.debug(self.connect.extend.standard.who_am_i())
  33. except Exception as e:
  34. log.error(e)
  35.  
  36. def printResult(self):
  37. resultCode = self.connect.result['result']
  38. description = self.connect.result['description']
  39. log.info("\n\nResult code : " + str(resultCode) + ", " + str(description))
  40.  
  41. @property
  42. def is_connected(self):
  43. return self.connect.bind()
  44.  
  45. def deleteUser(self, search_filter):
  46. if self.is_connected:
  47. log.info("\nBase : " + str(self.base_search))
  48. log.info("\nSearch Filter to delete the user : " + str(search_filter))
  49. # ldap3.extend.microsoft.modifyPassword.ad_modify_password
  50. # gets properly quoted and utf-16le encoded.
  51. if self.connect.search(search_base = self.base_search, search_filter= search_filter, search_scope=ldap3.SUBTREE, attributes = ['cn','givenName'], paged_size = 5):
  52. for entry in self.connect.response:
  53. user_dn= entry.get("dn")
  54. # perform the Delete operation
  55. self.connect.delete(user_dn)
  56. log.info("\nUser with User_DN = " + user_dn + " deleted.")
  57. self.printResult()
  58.  
  59. else:
  60. log.debug("\n\n\nThe user you tried to delete doesn't exist in this base.")
  61. else:
  62. log.error("LDAP not connected.")
  63.  
  64. def createUserLDAP(self,igcID):
  65. if self.is_connected:
  66. log.info("Creating user with igcID : " + igcID)
  67. 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' })
  68. self.printResult()
  69. resultCode = connect.result['result']
  70. description = connect.result['description']
  71. return resultCode, description
  72. else:
  73. log.error("LDAP not connected.")
  74.  
  75. def modifyCurrentIgcID(self):
  76. if self.is_connected:
  77. log.info("Modifying current IgcID of indiceAgent for next user creation.")
  78.  
  79. # dn : ou=indiceAgent,ou=indices,ou=infrastructure,dc=justice,dc=fr
  80. if self.connect.search(search_base="ou=indices,ou=infrastructure,dc=justice,dc=fr", search_filter="(ou=indiceAgent)", search_scope=SUBTREE, attributes="description"):
  81. for entry in self.connect.response:
  82. self.connect.modify("ou=indiceAgent,ou=indices,ou=infrastructure,dc=justice,dc=fr",
  83. {'description':[(MODIFY_REPLACE, [newIgcID])]})
  84. log.info("indiceAgent has now an IgcID attribute value of : " + str(newIgcID))
  85. self.printResult()
  86. else:
  87. log.error("LDAP not connected.")
  88.  
  89. def getIgcID():
  90. if self.is_connected:
  91. igcID_user =""
  92. if conn.search(search_base="ou=indices,ou=infrastructure,dc=justice,dc=fr", search_filter="(ou=indiceAgent)", search_scope=SUBTREE, attributes="description"):
  93. for entry in conn.response:
  94. # Take the string igcID from the list description from the dict attributes
  95. igcID_user= entry['attributes'].get('description')[0]
  96. print("\nCurrent IgcID = " + str(igcID_user))
  97. conn.unbind()
  98. return igcID_user
  99. else:
  100. log.error("LDAP not connected.")
  101.  
  102. def incrementIgcID(igcID):
  103. # Parse the letter "L" and the number
  104. # Increment +1 the number
  105. # put L and new number together
  106. firstLetter = igcID[0]
  107. nb = int(igcID[1:])
  108. nb = nb+1
  109. nxtIgcID = firstLetter + "0" + str(nb)
  110. return nxtIgcID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement