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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. """
  3. # Created on 2016.12.26
  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 ad_remove_members_from_groups(connection,
  28. members_dn,
  29. groups_dn,
  30. fix):
  31. """
  32. :param connection: a bound Connection object
  33. :param members_dn: the list of members to remove from groups
  34. :param groups_dn: the list of groups where members are to be removed
  35. :param fix: checks for group existence and existing members
  36. :return: a boolean where True means that the operation was successful and False means an error has happened
  37. Removes users-groups relations following the Activwe Directory rules: users are removed from groups' member attribute
  38. """
  39. if not isinstance(members_dn, SEQUENCE_TYPES):
  40. members_dn = [members_dn]
  41. if not isinstance(groups_dn, SEQUENCE_TYPES):
  42. groups_dn = [groups_dn]
  43. if connection.check_names: # builds new lists with sanitized dn
  44. safe_members_dn = []
  45. safe_groups_dn = []
  46. for member_dn in members_dn:
  47. safe_members_dn.append(safe_dn(member_dn))
  48. for group_dn in groups_dn:
  49. safe_groups_dn.append(safe_dn(group_dn))
  50. members_dn = safe_members_dn
  51. groups_dn = safe_groups_dn
  52. error = False
  53. for group in groups_dn:
  54. if fix: # checks for existance of group and for already assigned members
  55. result = connection.search(group, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER, attributes=['member'])
  56. if not connection.strategy.sync:
  57. response, result = connection.get_response(result)
  58. else:
  59. response, result = connection.response, connection.result
  60. if not result['description'] == 'success':
  61. raise LDAPInvalidDnError(group + ' not found')
  62. existing_members = response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []
  63. else:
  64. existing_members = members_dn
  65. existing_members = [element.lower() for element in existing_members]
  66. changes = dict()
  67. member_to_remove = [element for element in members_dn if element.lower() in existing_members]
  68. if member_to_remove:
  69. changes['member'] = (MODIFY_DELETE, member_to_remove)
  70. if changes:
  71. result = connection.modify(group, changes)
  72. if not connection.strategy.sync:
  73. _, result = connection.get_response(result)
  74. else:
  75. result = connection.result
  76. if result['description'] != 'success':
  77. error = True
  78. break
  79. return not error