def METHOD_NAME(cls, perm_ids, flat=False):
    user_ids = cls.users.through.objects.filter(assetpermission_id__in=perm_ids) \
        .values_list('user_id', flat=True).distinct()
    group_ids = cls.user_groups.through.objects.filter(assetpermission_id__in=perm_ids) \
        .values_list('usergroup_id', flat=True).distinct()
    group_user_ids = User.groups.through.objects.filter(usergroup_id__in=group_ids) \
        .values_list('user_id', flat=True).distinct()
    user_ids = set(user_ids) | set(group_user_ids)
    if flat:
        return user_ids
    return User.objects.filter(id__in=user_ids)