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.
 
 
 
 

93 line
3.7 KiB

  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, LDAPOperationsErrorResult
  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. raise_error=False):
  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 group existence and existing members
  37. :param raise_error: If the operation fails it raises an error instead of returning False
  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 Activwe Directory rules: users are removed from groups' member attribute
  40. """
  41. if not isinstance(members_dn, SEQUENCE_TYPES):
  42. members_dn = [members_dn]
  43. if not isinstance(groups_dn, SEQUENCE_TYPES):
  44. groups_dn = [groups_dn]
  45. if connection.check_names: # builds new lists with sanitized dn
  46. members_dn = [safe_dn(member_dn) for member_dn in members_dn]
  47. groups_dn = [safe_dn(group_dn) for group_dn in groups_dn]
  48. error = False
  49. for group in groups_dn:
  50. if fix: # checks for existance of group and for already assigned members
  51. result = connection.search(group, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER, attributes=['member'])
  52. if not connection.strategy.sync:
  53. response, result = connection.get_response(result)
  54. else:
  55. response, result = connection.response, connection.result
  56. if not result['description'] == 'success':
  57. raise LDAPInvalidDnError(group + ' not found')
  58. existing_members = response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []
  59. else:
  60. existing_members = members_dn
  61. existing_members = [element.lower() for element in existing_members]
  62. changes = dict()
  63. member_to_remove = [element for element in members_dn if element.lower() in existing_members]
  64. if member_to_remove:
  65. changes['member'] = (MODIFY_DELETE, member_to_remove)
  66. if changes:
  67. result = connection.modify(group, changes)
  68. if not connection.strategy.sync:
  69. _, result = connection.get_response(result)
  70. else:
  71. result = connection.result
  72. if result['description'] != 'success':
  73. error = True
  74. result_error_params = ['result', 'description', 'dn', 'message']
  75. if raise_error:
  76. raise LDAPOperationsErrorResult([(k, v) for k, v in result.items() if k in result_error_params])
  77. break
  78. return not error