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.

vlv.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.controls.vlv - classes for Virtual List View
  4. (see draft-ietf-ldapext-ldapv3-vlv)
  5. See https://www.python-ldap.org/ for project details.
  6. """
  7. __all__ = [
  8. 'VLVRequestControl',
  9. 'VLVResponseControl',
  10. ]
  11. import ldap
  12. from ldap.ldapobject import LDAPObject
  13. from ldap.controls import (RequestControl, ResponseControl,
  14. KNOWN_RESPONSE_CONTROLS, DecodeControlTuples)
  15. from pyasn1.type import univ, namedtype, tag, namedval, constraint
  16. from pyasn1.codec.ber import encoder, decoder
  17. class ByOffsetType(univ.Sequence):
  18. tagSet = univ.Sequence.tagSet.tagImplicitly(
  19. tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))
  20. componentType = namedtype.NamedTypes(
  21. namedtype.NamedType('offset', univ.Integer()),
  22. namedtype.NamedType('contentCount', univ.Integer()))
  23. class TargetType(univ.Choice):
  24. componentType = namedtype.NamedTypes(
  25. namedtype.NamedType('byOffset', ByOffsetType()),
  26. namedtype.NamedType('greaterThanOrEqual', univ.OctetString().subtype(
  27. implicitTag=tag.Tag(tag.tagClassContext,
  28. tag.tagFormatSimple, 1))))
  29. class VirtualListViewRequestType(univ.Sequence):
  30. componentType = namedtype.NamedTypes(
  31. namedtype.NamedType('beforeCount', univ.Integer()),
  32. namedtype.NamedType('afterCount', univ.Integer()),
  33. namedtype.NamedType('target', TargetType()),
  34. namedtype.OptionalNamedType('contextID', univ.OctetString()))
  35. class VLVRequestControl(RequestControl):
  36. controlType = '2.16.840.1.113730.3.4.9'
  37. def __init__(
  38. self,
  39. criticality=False,
  40. before_count=0,
  41. after_count=0,
  42. offset=None,
  43. content_count=None,
  44. greater_than_or_equal=None,
  45. context_id=None,
  46. ):
  47. RequestControl.__init__(self,self.controlType,criticality)
  48. assert (offset is not None and content_count is not None) or \
  49. greater_than_or_equal, \
  50. ValueError(
  51. 'offset and content_count must be set together or greater_than_or_equal must be used'
  52. )
  53. self.before_count = before_count
  54. self.after_count = after_count
  55. self.offset = offset
  56. self.content_count = content_count
  57. self.greater_than_or_equal = greater_than_or_equal
  58. self.context_id = context_id
  59. def encodeControlValue(self):
  60. p = VirtualListViewRequestType()
  61. p.setComponentByName('beforeCount', self.before_count)
  62. p.setComponentByName('afterCount', self.after_count)
  63. if self.offset is not None and self.content_count is not None:
  64. by_offset = ByOffsetType()
  65. by_offset.setComponentByName('offset', self.offset)
  66. by_offset.setComponentByName('contentCount', self.content_count)
  67. target = TargetType()
  68. target.setComponentByName('byOffset', by_offset)
  69. elif self.greater_than_or_equal:
  70. target = TargetType()
  71. target.setComponentByName('greaterThanOrEqual',
  72. self.greater_than_or_equal)
  73. else:
  74. raise NotImplementedError
  75. p.setComponentByName('target', target)
  76. return encoder.encode(p)
  77. KNOWN_RESPONSE_CONTROLS[VLVRequestControl.controlType] = VLVRequestControl
  78. class VirtualListViewResultType(univ.Enumerated):
  79. namedValues = namedval.NamedValues(
  80. ('success', 0),
  81. ('operationsError', 1),
  82. ('protocolError', 3),
  83. ('unwillingToPerform', 53),
  84. ('insufficientAccessRights', 50),
  85. ('adminLimitExceeded', 11),
  86. ('innapropriateMatching', 18),
  87. ('sortControlMissing', 60),
  88. ('offsetRangeError', 61),
  89. ('other', 80),
  90. )
  91. class VirtualListViewResponseType(univ.Sequence):
  92. componentType = namedtype.NamedTypes(
  93. namedtype.NamedType('targetPosition', univ.Integer()),
  94. namedtype.NamedType('contentCount', univ.Integer()),
  95. namedtype.NamedType('virtualListViewResult',
  96. VirtualListViewResultType()),
  97. namedtype.OptionalNamedType('contextID', univ.OctetString()))
  98. class VLVResponseControl(ResponseControl):
  99. controlType = '2.16.840.1.113730.3.4.10'
  100. def __init__(self,criticality=False):
  101. ResponseControl.__init__(self,self.controlType,criticality)
  102. def decodeControlValue(self,encoded):
  103. p, rest = decoder.decode(encoded, asn1Spec=VirtualListViewResponseType())
  104. assert not rest, 'all data could not be decoded'
  105. self.targetPosition = int(p.getComponentByName('targetPosition'))
  106. self.contentCount = int(p.getComponentByName('contentCount'))
  107. virtual_list_view_result = p.getComponentByName('virtualListViewResult')
  108. self.virtualListViewResult = int(virtual_list_view_result)
  109. context_id = p.getComponentByName('contextID')
  110. if context_id.hasValue():
  111. self.contextID = str(context_id)
  112. else:
  113. self.contextID = None
  114. # backward compatibility class attributes
  115. self.target_position = self.targetPosition
  116. self.content_count = self.contentCount
  117. self.result = self.virtualListViewResult
  118. self.context_id = self.contextID
  119. KNOWN_RESPONSE_CONTROLS[VLVResponseControl.controlType] = VLVResponseControl