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.

removeMembersFromGroups.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. """
  2. """
  3. # Created on 2016.04.17
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2016 - 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 ...core.exceptions import LDAPInvalidDnError
  25. from ... import SEQUENCE_TYPES, MODIFY_DELETE, BASE, DEREF_NEVER
  26. from ...utils.dn import safe_dn
  27. def edir_remove_members_from_groups(connection,
  28. members_dn,
  29. groups_dn,
  30. fix,
  31. transaction):
  32. """
  33. :param connection: a bound Connection object
  34. :param members_dn: the list of members to remove from groups
  35. :param groups_dn: the list of groups where members are to be removed
  36. :param fix: checks for inconsistences in the users-groups relation and fixes them
  37. :param transaction: activates an LDAP transaction
  38. :return: a boolean where True means that the operation was successful and False means an error has happened
  39. Removes users-groups relations following the eDirectory rules: groups are removed from securityEquals and groupMembership
  40. attributes in the member object while members are removed from member and equivalentToMe attributes in the group object.
  41. Raises LDAPInvalidDnError if members or groups are not found in the DIT.
  42. """
  43. if not isinstance(members_dn, SEQUENCE_TYPES):
  44. members_dn = [members_dn]
  45. if not isinstance(groups_dn, SEQUENCE_TYPES):
  46. groups_dn = [groups_dn]
  47. if connection.check_names: # builds new lists with sanitized dn
  48. safe_members_dn = []
  49. safe_groups_dn = []
  50. for member_dn in members_dn:
  51. safe_members_dn.append(safe_dn(member_dn))
  52. for group_dn in groups_dn:
  53. safe_groups_dn.append(safe_dn(group_dn))
  54. members_dn = safe_members_dn
  55. groups_dn = safe_groups_dn
  56. transaction_control = None
  57. error = False
  58. if transaction:
  59. transaction_control = connection.extend.novell.start_transaction()
  60. if not error:
  61. for member in members_dn:
  62. if fix: # checks for existance of member and for already assigned groups
  63. result = connection.search(member, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER, attributes=['securityEquals', 'groupMembership'])
  64. if not connection.strategy.sync:
  65. response, result = connection.get_response(result)
  66. else:
  67. response, result = connection.response, connection.result
  68. if not result['description'] == 'success':
  69. raise LDAPInvalidDnError(member + ' not found')
  70. existing_security_equals = response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []
  71. existing_group_membership = response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []
  72. else:
  73. existing_security_equals = groups_dn
  74. existing_group_membership = groups_dn
  75. existing_security_equals = [element.lower() for element in existing_security_equals]
  76. existing_group_membership = [element.lower() for element in existing_group_membership]
  77. changes = dict()
  78. security_equals_to_remove = [element for element in groups_dn if element.lower() in existing_security_equals]
  79. group_membership_to_remove = [element for element in groups_dn if element.lower() in existing_group_membership]
  80. if security_equals_to_remove:
  81. changes['securityEquals'] = (MODIFY_DELETE, security_equals_to_remove)
  82. if group_membership_to_remove:
  83. changes['groupMembership'] = (MODIFY_DELETE, group_membership_to_remove)
  84. if changes:
  85. result = connection.modify(member, changes, controls=[transaction_control] if transaction else None)
  86. if not connection.strategy.sync:
  87. _, result = connection.get_response(result)
  88. else:
  89. result = connection.result
  90. if result['description'] != 'success':
  91. error = True
  92. break
  93. if not error:
  94. for group in groups_dn:
  95. if fix: # checks for existance of group and for already assigned members
  96. result = connection.search(group, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER, attributes=['member', 'equivalentToMe'])
  97. if not connection.strategy.sync:
  98. response, result = connection.get_response(result)
  99. else:
  100. response, result = connection.response, connection.result
  101. if not result['description'] == 'success':
  102. raise LDAPInvalidDnError(group + ' not found')
  103. existing_members = response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []
  104. existing_equivalent_to_me = response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []
  105. else:
  106. existing_members = members_dn
  107. existing_equivalent_to_me = members_dn
  108. existing_members = [element.lower() for element in existing_members]
  109. existing_equivalent_to_me = [element.lower() for element in existing_equivalent_to_me]
  110. changes = dict()
  111. member_to_remove = [element for element in members_dn if element.lower() in existing_members]
  112. equivalent_to_me_to_remove = [element for element in members_dn if element.lower() in existing_equivalent_to_me]
  113. if member_to_remove:
  114. changes['member'] = (MODIFY_DELETE, member_to_remove)
  115. if equivalent_to_me_to_remove:
  116. changes['equivalentToMe'] = (MODIFY_DELETE, equivalent_to_me_to_remove)
  117. if changes:
  118. result = connection.modify(group, changes, controls=[transaction_control] if transaction else None)
  119. if not connection.strategy.sync:
  120. _, result = connection.get_response(result)
  121. else:
  122. result = connection.result
  123. if result['description'] != 'success':
  124. error = True
  125. break
  126. if transaction:
  127. if error: # aborts transaction in case of error in the modify operations
  128. result = connection.extend.novell.end_transaction(commit=False, controls=[transaction_control])
  129. else:
  130. result = connection.extend.novell.end_transaction(commit=True, controls=[transaction_control])
  131. if result['description'] != 'success':
  132. error = True
  133. return not error # return True if no error is raised in the LDAP operations