예제 #1
0
    def get_groups(self, parameters):
        ad_help = ActiveDirectoryHelper()
        results = ad_help.import_groups(parameters)

        ldap_json = json.loads(results)

        entries = ldap_json['entries']

        for group in entries:
            attributes = group['attributes']
            model_attributes = {}

            for key, value in attributes.items():

                if len(value) > 0:
                    value_string = ""
                    try:
                        if isinstance(value, str):
                            value_string = value
                            value_string = value_string.decode('utf-8')
                        else:
                            for e in value:
                                if isinstance(e, str):
                                    value_string = ''.join(e)
                                    # value_string = value_string.decode('utf-8')
                                else:
                                    value_string = e['encoded']

                        model_attributes[self.ldap_field_to_model(
                            key)] = value_string

                    except UnicodeDecodeError:
                        model_attributes[self.ldap_field_to_model(
                            key)] = self.cleanhex(value_string)
            """
            Don't filter on everything. Start with the properties that are
            least likely to ever change, then work towards the more mutable
            properties.
            """
            filter_attrs = {}
            if 'objectGUID' in attributes:
                filter_attrs['object_guid'] = model_attributes['object_guid']
            elif 'objectSid' in attributes:
                filter_attrs['object_sid'] = model_attributes['object_sid']
            elif 'distinguishedName' in attributes:
                filter_attrs['distinguished_name'] = model_attributes[
                    'distinguished_name']
            else:
                continue

            # If no matching group currently exists then create one, otherwise
            # update the existing group.
            ad_groups = ActiveDirectoryGroup.objects.filter(**filter_attrs)
            if ad_groups.count() == 0:

                ad_group = ActiveDirectoryGroup.objects.create(
                    ldap_configuration=parameters, **model_attributes)

                gen_group = Group.objects.create(
                    name=ad_group.cn,
                    group_type=Group.AD,
                    description="Imported group from LDAP")
                gen_group.save()

                ad_group.group = gen_group
                ad_group.save()
            else:
                # print("existing group")
                ad_groups.update(**model_attributes)
                ad_group = ad_groups.first()

                gen_group = ad_group.group
                if gen_group:
                    gen_group.name = ad_group.cn
                    gen_group.save()
예제 #2
0
파일: models.py 프로젝트: kakakacool/ava
    def get_groups(self, parameters):
        ad_help = ActiveDirectoryHelper()
        results = ad_help.import_groups(parameters)

        ldap_json = json.loads(results)

        entries = ldap_json['entries']

        for group in entries:
            attributes = group['attributes']
            model_attributes = {}

            for key, value in attributes.items():

                if len(value) > 0:
                    value_string = ""
                    try:
                        if isinstance(value, str):
                            value_string = value
                            value_string = value_string.decode('utf-8')
                        else:
                            for e in value:
                                if isinstance(e, str):
                                    value_string = ''.join(e)
                                    # value_string = value_string.decode('utf-8')
                                else:
                                    value_string = e['encoded']

                        model_attributes[self.ldap_field_to_model(key)] = value_string

                    except UnicodeDecodeError:
                        model_attributes[self.ldap_field_to_model(key)] = self.cleanhex(value_string)

            """
            Don't filter on everything. Start with the properties that are
            least likely to ever change, then work towards the more mutable
            properties.
            """
            filter_attrs = {}
            if 'objectGUID' in attributes:
                filter_attrs['object_guid'] = model_attributes['object_guid']
            elif 'objectSid' in attributes:
                filter_attrs['object_sid'] = model_attributes['object_sid']
            elif 'distinguishedName' in attributes:
                filter_attrs['distinguished_name'] = model_attributes['distinguished_name']
            else:
                continue

            # If no matching group currently exists then create one, otherwise
            # update the existing group.
            ad_groups = ActiveDirectoryGroup.objects.filter(**filter_attrs)
            if ad_groups.count() == 0:

                ad_group = ActiveDirectoryGroup.objects.create(ldap_configuration=parameters, **model_attributes)

                gen_group = Group.objects.create(name=ad_group.cn, group_type=Group.AD,
                                                 description="Imported group from LDAP")
                gen_group.save()

                ad_group.group = gen_group
                ad_group.save()
            else:
                # print("existing group")
                ad_groups.update(**model_attributes)
                ad_group = ad_groups.first()

                gen_group = ad_group.group
                if gen_group:
                    gen_group.name = ad_group.cn
                    gen_group.save()