您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

94 行
3.9 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 ... import SEQUENCE_TYPES, MODIFY_ADD, BASE, DEREF_NEVER
  25. from ...core.exceptions import LDAPInvalidDnError, LDAPOperationsErrorResult
  26. from ...utils.dn import safe_dn
  27. def ad_add_members_to_groups(connection,
  28. members_dn,
  29. groups_dn,
  30. fix=True,
  31. raise_error=False):
  32. """
  33. :param connection: a bound Connection object
  34. :param members_dn: the list of members to add to groups
  35. :param groups_dn: the list of groups where members are to be added
  36. :param fix: checks for group existence and already assigned 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. Establishes users-groups relations following the Active Directory rules: users are added to the member attribute of groups.
  40. Raises LDAPInvalidDnError if members or groups are not found in the DIT.
  41. """
  42. if not isinstance(members_dn, SEQUENCE_TYPES):
  43. members_dn = [members_dn]
  44. if not isinstance(groups_dn, SEQUENCE_TYPES):
  45. groups_dn = [groups_dn]
  46. if connection.check_names: # builds new lists with sanitized dn
  47. members_dn = [safe_dn(member_dn) for member_dn in members_dn]
  48. groups_dn = [safe_dn(group_dn) for group_dn in groups_dn]
  49. error = False
  50. for group in groups_dn:
  51. if fix: # checks for existance of group and for already assigned members
  52. result = connection.search(group, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER,
  53. attributes=['member'])
  54. if not connection.strategy.sync:
  55. response, result = connection.get_response(result)
  56. else:
  57. response, result = connection.response, connection.result
  58. if not result['description'] == 'success':
  59. raise LDAPInvalidDnError(group + ' not found')
  60. existing_members = response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []
  61. existing_members = [element.lower() for element in existing_members]
  62. else:
  63. existing_members = []
  64. changes = dict()
  65. member_to_add = [element for element in members_dn if element.lower() not in existing_members]
  66. if member_to_add:
  67. changes['member'] = (MODIFY_ADD, member_to_add)
  68. if changes:
  69. result = connection.modify(group, changes)
  70. if not connection.strategy.sync:
  71. _, result = connection.get_response(result)
  72. else:
  73. result = connection.result
  74. if result['description'] != 'success':
  75. error = True
  76. result_error_params = ['result', 'description', 'dn', 'message']
  77. if raise_error:
  78. raise LDAPOperationsErrorResult([(k, v) for k, v in result.items() if k in result_error_params])
  79. break
  80. return not error # returns True if no error is raised in the LDAP operations