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