Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
addMembersToGroups.py99 linesDownload Raw Back to microsoft
1"""2"""3 4# Created on 2016.12.265#6# Author: Giovanni Cannata7#8# Copyright 2016 - 2020 Giovanni Cannata9#10# This file is part of ldap3.11#12# ldap3 is free software: you can redistribute it and/or modify13# it under the terms of the GNU Lesser General Public License as published14# by the Free Software Foundation, either version 3 of the License, or15# (at your option) any later version.16#17# ldap3 is distributed in the hope that it will be useful,18# but WITHOUT ANY WARRANTY; without even the implied warranty of19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the20# GNU Lesser General Public License for more details.21#22# You should have received a copy of the GNU Lesser General Public License23# along with ldap3 in the COPYING and COPYING.LESSER files.24# If not, see <http://www.gnu.org/licenses/>.25 26from ... import SEQUENCE_TYPES, MODIFY_ADD, BASE, DEREF_NEVER27from ...core.exceptions import LDAPInvalidDnError, LDAPOperationsErrorResult28from ...utils.dn import safe_dn29 30 31def ad_add_members_to_groups(connection,32                             members_dn,33                             groups_dn,34                             fix=True,35                             raise_error=False):36    """37    :param connection: a bound Connection object38    :param members_dn: the list of members to add to groups39    :param groups_dn: the list of groups where members are to be added40    :param fix: checks for group existence and already assigned members41    :param raise_error: If the operation fails it raises an error instead of returning False42    :return: a boolean where True means that the operation was successful and False means an error has happened43    Establishes users-groups relations following the Active Directory rules: users are added to the member attribute of groups.44    Raises LDAPInvalidDnError if members or groups are not found in the DIT.45    """46 47    if not isinstance(members_dn, SEQUENCE_TYPES):48        members_dn = [members_dn]49 50    if not isinstance(groups_dn, SEQUENCE_TYPES):51        groups_dn = [groups_dn]52 53    if connection.check_names:  # builds new lists with sanitized dn54        members_dn = [safe_dn(member_dn) for member_dn in members_dn]55        groups_dn = [safe_dn(group_dn) for group_dn in groups_dn]56 57    error = False58    for group in groups_dn:59        if fix:  # checks for existance of group and for already assigned members60            result = connection.search(group, '(objectclass=*)', BASE, dereference_aliases=DEREF_NEVER, attributes=['member'])61            if not connection.strategy.sync:62                response, result = connection.get_response(result)63            else:64                if connection.strategy.thread_safe:65                    _, result, response, _ = result66                else:67                    response = connection.response68                    result = connection.result69 70            if not result['description'] == 'success':71                raise LDAPInvalidDnError(group + ' not found')72 73            existing_members = response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []74            existing_members = [element.lower() for element in existing_members]75        else:76            existing_members = []77 78        changes = dict()79        member_to_add = [element for element in members_dn if element.lower() not in existing_members]80        if member_to_add:81            changes['member'] = (MODIFY_ADD, member_to_add)82        if changes:83            result = connection.modify(group, changes)84            if not connection.strategy.sync:85                _, result = connection.get_response(result)86            else:87                if connection.strategy.thread_safe:88                    _, result, _, _ = result89                else:90                    result = connection.result91            if result['description'] != 'success':92                error = True93                result_error_params = ['result', 'description', 'dn', 'message']94                if raise_error:95                    raise LDAPOperationsErrorResult([(k, v) for k, v in result.items() if k in result_error_params])96                break97 98    return not error  # returns True if no error is raised in the LDAP operations99 
codekingpro/portable-devtools · Team Ai