Guest User

Untitled

a guest
Apr 17th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 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 AD_SERVER_URI
  8. from credentials import AD_ADMIN_LOGIN
  9. from credentials import AD_ADMIN_PASSWORD
  10. from credentials import AD_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 AD():
  18.  
  19. def __init__(self):
  20. self.server_Uri = AD_SERVER_URI
  21. self.admin_login = AD_ADMIN_LOGIN
  22. self.admin_password = AD_ADMIN_PASSWORD
  23. self.base_search = AD_BASE_SEARCH
  24. self.port = 636
  25. self.ssl = True
  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 createUserAD(self):
  46. if self.is_connected:
  47. current_password = 'CeCiEstUnPassWord49'
  48. user_dn = "CN=test T. test,CN=Users,DC=ac,DC=justice,DC=fr"
  49. self.connect.add(
  50. user_dn,
  51. ['user'],
  52. {
  53. 'sn': 'test',
  54. 'cn': 'test T. test',
  55. 'description': 'Compte de test Kerberos',
  56. 'userPrincipalName': 'test@ac.justice.fr'
  57. })
  58. self.connect.extend.microsoft.modify_password(user_dn, current_password, controls=None)
  59. self.connect.modify(
  60. user_dn,
  61. {'pwdLastSet': (ldap3.MODIFY_REPLACE, ["0"])})
  62. else:
  63. log.error("AD not connected.")
  64.  
  65. def deleteUser(self, search_filter):
  66. if self.is_connected:
  67. log.info("\nBase : " + str(self.base_search))
  68. log.info("\nSearch Filter to delete the user : " + str(search_filter))
  69. # ldap3.extend.microsoft.modifyPassword.ad_modify_password
  70. # gets properly quoted and utf-16le encoded.
  71. if self.connect.search(search_base = self.base_search, search_filter= search_filter, search_scope=ldap3.SUBTREE, attributes = ['cn','givenName'], paged_size = 5):
  72. for entry in self.connect.response:
  73. user_dn= entry.get("dn")
  74. # perform the Delete operation
  75. self.connect.delete(user_dn)
  76. log.info("\nUser with User_DN = " + user_dn + " deleted.")
  77. self.printResult()
  78.  
  79. else:
  80. log.debug("\n\n\nThe user you tried to delete doesn't exist in this base.")
  81. else:
  82. log.error("AD not connected.")
Add Comment
Please, Sign In to add comment