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.

PagedSearch.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. """
  3. # Created on 2014.07.08
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from ... import SUBTREE, DEREF_ALWAYS
  25. from ...utils.dn import safe_dn
  26. from ...core.results import DO_NOT_RAISE_EXCEPTIONS, RESULT_SIZE_LIMIT_EXCEEDED
  27. from ...core.exceptions import LDAPOperationResult
  28. from ...utils.log import log, log_enabled, ERROR, BASIC, PROTOCOL, NETWORK, EXTENDED
  29. def paged_search_generator(connection,
  30. search_base,
  31. search_filter,
  32. search_scope=SUBTREE,
  33. dereference_aliases=DEREF_ALWAYS,
  34. attributes=None,
  35. size_limit=0,
  36. time_limit=0,
  37. types_only=False,
  38. get_operational_attributes=False,
  39. controls=None,
  40. paged_size=100,
  41. paged_criticality=False):
  42. if connection.check_names and search_base:
  43. search_base = safe_dn(search_base)
  44. responses = []
  45. cookie = True # performs search at least one time
  46. while cookie:
  47. result = connection.search(search_base,
  48. search_filter,
  49. search_scope,
  50. dereference_aliases,
  51. attributes,
  52. size_limit,
  53. time_limit,
  54. types_only,
  55. get_operational_attributes,
  56. controls,
  57. paged_size,
  58. paged_criticality,
  59. None if cookie is True else cookie)
  60. if not isinstance(result, bool):
  61. response, result = connection.get_response(result)
  62. else:
  63. response = connection.response
  64. result = connection.result
  65. responses.extend(response)
  66. try:
  67. cookie = result['controls']['1.2.840.113556.1.4.319']['value']['cookie']
  68. except KeyError:
  69. cookie = None
  70. if result and result['result'] not in DO_NOT_RAISE_EXCEPTIONS:
  71. if log_enabled(PROTOCOL):
  72. log(PROTOCOL, 'paged search operation result <%s> for <%s>', result, connection)
  73. if result['result'] == RESULT_SIZE_LIMIT_EXCEEDED:
  74. while responses:
  75. yield responses.pop()
  76. raise LDAPOperationResult(result=result['result'], description=result['description'], dn=result['dn'], message=result['message'], response_type=result['type'])
  77. while responses:
  78. yield responses.pop()
  79. connection.response = None
  80. def paged_search_accumulator(connection,
  81. search_base,
  82. search_filter,
  83. search_scope=SUBTREE,
  84. dereference_aliases=DEREF_ALWAYS,
  85. attributes=None,
  86. size_limit=0,
  87. time_limit=0,
  88. types_only=False,
  89. get_operational_attributes=False,
  90. controls=None,
  91. paged_size=100,
  92. paged_criticality=False):
  93. if connection.check_names and search_base:
  94. search_base = safe_dn(search_base)
  95. responses = []
  96. for response in paged_search_generator(connection,
  97. search_base,
  98. search_filter,
  99. search_scope,
  100. dereference_aliases,
  101. attributes,
  102. size_limit,
  103. time_limit,
  104. types_only,
  105. get_operational_attributes,
  106. controls,
  107. paged_size,
  108. paged_criticality):
  109. responses.append(response)
  110. connection.response = responses
  111. return responses