Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readentry.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.controls.readentry - classes for the Read Entry controls
  4. (see RFC 4527)
  5. See https://www.python-ldap.org/ for project details.
  6. """
  7. import ldap
  8. from pyasn1.codec.ber import encoder,decoder
  9. from ldap.controls import LDAPControl,KNOWN_RESPONSE_CONTROLS
  10. from pyasn1_modules.rfc2251 import AttributeDescriptionList,SearchResultEntry
  11. class ReadEntryControl(LDAPControl):
  12. """
  13. Base class for read entry control described in RFC 4527
  14. attrList
  15. list of attribute type names requested
  16. Class attributes with values extracted from the response control:
  17. dn
  18. string holding the distinguished name of the LDAP entry
  19. entry
  20. dictionary holding the LDAP entry
  21. """
  22. def __init__(self,criticality=False,attrList=None):
  23. self.criticality,self.attrList,self.entry = criticality,attrList or [],None
  24. def encodeControlValue(self):
  25. attributeSelection = AttributeDescriptionList()
  26. for i in range(len(self.attrList)):
  27. attributeSelection.setComponentByPosition(i,self.attrList[i])
  28. return encoder.encode(attributeSelection)
  29. def decodeControlValue(self,encodedControlValue):
  30. decodedEntry,_ = decoder.decode(encodedControlValue,asn1Spec=SearchResultEntry())
  31. self.dn = str(decodedEntry[0])
  32. self.entry = {}
  33. for attr in decodedEntry[1]:
  34. self.entry[str(attr[0])] = [ str(attr_value) for attr_value in attr[1] ]
  35. class PreReadControl(ReadEntryControl):
  36. """
  37. Class for pre-read control described in RFC 4527
  38. attrList
  39. list of attribute type names requested
  40. Class attributes with values extracted from the response control:
  41. dn
  42. string holding the distinguished name of the LDAP entry
  43. before the operation was done by the server
  44. entry
  45. dictionary holding the LDAP entry
  46. before the operation was done by the server
  47. """
  48. controlType = ldap.CONTROL_PRE_READ
  49. KNOWN_RESPONSE_CONTROLS[PreReadControl.controlType] = PreReadControl
  50. class PostReadControl(ReadEntryControl):
  51. """
  52. Class for post-read control described in RFC 4527
  53. attrList
  54. list of attribute type names requested
  55. Class attributes with values extracted from the response control:
  56. dn
  57. string holding the distinguished name of the LDAP entry
  58. after the operation was done by the server
  59. entry
  60. dictionary holding the LDAP entry
  61. after the operation was done by the server
  62. """
  63. controlType = ldap.CONTROL_POST_READ
  64. KNOWN_RESPONSE_CONTROLS[PostReadControl.controlType] = PostReadControl