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.

addMembersToGroups.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_ADD, BASE, DEREF_NEVER
  26. def ad_add_members_to_groups(connection,
  27. members_dn,
  28. groups_dn,
  29. fix=True):
  30. """
  31. :param connection: a bound Connection object
  32. :param members_dn: the list of members to add to groups
  33. :param groups_dn: the list of groups where members are to be added
  34. :param fix: checks for group existence and already assigned members
  35. :return: a boolean where True means that the operation was successful and False means an error has happened
  36. Establishes users-groups relations following the Active Directory rules: users are added to the member attribute of groups.
  37. Raises LDAPInvalidDnError if members or groups are not found in the DIT.
  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. error = False
  44. for group in groups_dn:
  45. if fix: # checks for existance of group and for already assigned members
  46. result = connection.search(group, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER, attributes=['member'])
  47. if not connection.strategy.sync:
  48. response, result = connection.get_response(result)
  49. else:
  50. response, result = connection.response, connection.result
  51. if not result['description'] == 'success':
  52. raise LDAPInvalidDnError(group + ' not found')
  53. existing_members = response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []
  54. existing_members = [element.lower() for element in existing_members]
  55. else:
  56. existing_members = []
  57. changes = dict()
  58. member_to_add = [element for element in members_dn if element.lower() not in existing_members]
  59. if member_to_add:
  60. changes['member'] = (MODIFY_ADD, member_to_add)
  61. if changes:
  62. result = connection.modify(group, changes)
  63. if not connection.strategy.sync:
  64. _, result = connection.get_response(result)
  65. else:
  66. result = connection.result
  67. if result['description'] != 'success':
  68. error = True
  69. break
  70. return not error # returns True if no error is raised in the LDAP operations