Beispiel #1
0
def get_all_listings_for_profile_by_id(current_request_username, profile_id, listing_id=None):
    try:
        if profile_id == 'self':
            profile_instance = models.Profile.objects.get(user__username=current_request_username).user
        else:
            profile_instance = models.Profile.objects.get(id=profile_id).user
    except models.Profile.DoesNotExist:
        return None

    try:
        listings = models.Listing.objects.filter(owners__id=profile_instance.id)
        listings = listings.exclude(is_private=True)
        # filter out listings by user's access level
        titles_to_exclude = []
        for i in listings:
            if not i.security_marking:
                logger.debug('Listing {0!s} has no security_marking'.format(i.title))
            if not system_has_access_control(current_request_username, i.security_marking):
                titles_to_exclude.append(i.title)
        listings = listings.exclude(title__in=titles_to_exclude)  # TODO: Base it on ids

        if listing_id:
            filtered_listing = listings.get(id=listing_id)
        else:
            filtered_listing = listings.all()

        return filtered_listing
    except models.Listing.DoesNotExist:
        return None
Beispiel #2
0
    def for_user(self, username):
        # get all listings
        objects = super(AccessControlListingManager, self).get_queryset()
        # filter out private listings
        user = Profile.objects.get(user__username=username)
        if user.highest_role() == 'APPS_MALL_STEWARD':
            exclude_orgs = []
        elif user.highest_role() == 'ORG_STEWARD':
            user_orgs = user.stewarded_organizations.all()
            user_orgs = [i.title for i in user_orgs]
            exclude_orgs = Agency.objects.exclude(title__in=user_orgs)
        else:
            user_orgs = user.organizations.all()
            user_orgs = [i.title for i in user_orgs]
            exclude_orgs = Agency.objects.exclude(title__in=user_orgs)

        objects = objects.exclude(is_private=True,
                                  agency__in=exclude_orgs)

        # filter out listings by user's access level
        titles_to_exclude = []
        for i in objects:
            if not i.security_marking:
                logger.debug('Listing {0!s} has no security_marking'.format(i.title))
            if not system_has_access_control(username, i.security_marking):
                titles_to_exclude.append(i.title)
        objects = objects.exclude(title__in=titles_to_exclude)
        return objects
def suggest(request_username, search_param_parser):
    """
    Suggest

    It must respects restrictions
     - Private apps (apps only from user's agency)
     - User's max_classification_level

    Args:
        request_username(string)
        search_param_parser(SearchParamParser): Parsed Request Search Object

    Returns:
        listing titles in a list
    """
    # Create ES client
    es_client = elasticsearch_factory.get_client()

    elasticsearch_factory.check_elasticsearch()

    if search_param_parser.search_string is None:
        return []

    user_exclude_orgs = get_user_exclude_orgs(request_username)

    # Override Limit - Only 15 results should come if limit was not set
    if search_param_parser.limit_set is False:
        search_param_parser.limit = constants.ES_SUGGEST_LIMIT

    search_query = elasticsearch_util.make_search_query_obj(
        search_param_parser, exclude_agencies=user_exclude_orgs)
    # Only Retrieve ['title', 'security_marking', 'id'] fields from Elasticsearch for suggestions
    search_query['_source'] = ['title', 'security_marking', 'id']

    # print(json.dumps(search_query, indent=4))  #  Print statement for debugging output
    res = es_client.search(index=settings.ES_INDEX_NAME, body=search_query)

    hits = res.get('hits', {}).get('hits', None)
    if not hits:
        return []

    hit_titles = []

    for hit in hits:
        source = hit.get('_source')

        exclude_bool = False
        if not source.get('security_marking'):
            exclude_bool = True
            logger.debug('Listing {0!s} has no security_marking'.format(
                source.get('title')))
        if not system_has_access_control(request_username,
                                         source.get('security_marking')):
            exclude_bool = True

        if exclude_bool is False:
            temp = {'title': source['title'], 'id': source['id']}
            hit_titles.append(temp)

    return hit_titles
Beispiel #4
0
    def process_next_start(self):
        """
        execute security_marking check on each listing
        """
        while True:
            listing = self.starts.next()

            if not listing['security_marking']:
                logger.debug('Listing {0!s} has no security_marking'.format(listing['title']))
            else:
                if self.featured:
                    if listing['is_featured'] is True:
                        if system_has_access_control(self.username, listing['security_marking']):
                            return listing
                else:
                    if system_has_access_control(self.username, listing['security_marking']):
                        return listing
Beispiel #5
0
 def process_next_start(self):
     """
     execute security_marking check on each listing
     """
     while True:
         listing = self.starts.next()
         if not listing.security_marking:
             logger.debug('Listing {0!s} has no security_marking'.format(listing.title))
         if system_has_access_control(self.username, listing.security_marking):
             return listing
Beispiel #6
0
 def for_user(self, username):
     # get all images
     objects = super(AccessControlImageManager, self).get_queryset()
     # filter out listings by user's access level
     images_to_exclude = []
     for i in objects:
         if not system_has_access_control(username, i.security_marking):
             images_to_exclude.append(i.id)
     objects = objects.exclude(id__in=images_to_exclude)
     return objects
Beispiel #7
0
    def validate_security_marking(self, value):
        # don't allow user to select a security marking that is above
        # their own access level
        profile = generic_model_access.get_profile(
            self.context['request'].user.username)

        if value:
            if not system_has_access_control(profile.user.username, value):
                raise serializers.ValidationError(
                    'Security marking too high for current user')
        else:
            raise serializers.ValidationError('Security marking is required')

        return value
Beispiel #8
0
    def retrieve(self, request, pk=None):
        """
        Return an image, enforcing access control
        """
        queryset = self.get_queryset()
        image = get_object_or_404(queryset, pk=pk)
        image_path = str(image.id) + '_' + image.image_type.name + '.' + image.file_extension
        # enforce access control
        profile = generic_model_access.get_profile(self.request.user.username)

        if not system_has_access_control(profile.user.username, image.security_marking):
            raise errors.PermissionDenied()

        content_type = 'image/' + image.file_extension
        try:
            with media_storage.open(image_path) as f:
                return HttpResponse(f.read(), content_type=content_type)
        except IOError:
            logger.error('No image found for pk {}'.format(pk))
            return Response(status=status.HTTP_404_NOT_FOUND)
Beispiel #9
0
    def validate(self, data):
        access_control_instance = plugin_manager.get_system_access_control_plugin(
        )
        # logger.debug('inside ListingSerializer.validate', extra={'request':self.context.get('request')})
        profile = generic_model_access.get_profile(
            self.context['request'].user.username)

        # This checks to see if value exist as a key and value is not None
        if not data.get('title'):
            raise serializers.ValidationError('Title is required')

        if 'security_marking' not in data:
            raise serializers.ValidationError('Security Marking is Required')

        # Assign a default security_marking level if none is provided
        if not data.get('security_marking'):
            data['security_marking'] = constants.DEFAULT_SECURITY_MARKING

        if not access_control_instance.validate_marking(
                data['security_marking']):
            raise serializers.ValidationError(
                'Security Marking Format is Invalid')

        # Don't allow user to select a security marking that is above
        # their own access level
        if not system_has_access_control(profile.user.username,
                                         data.get('security_marking')):
            raise serializers.ValidationError(
                'Security marking too high for current user')

        data['description'] = data.get('description')
        data['launch_url'] = data.get('launch_url')
        data['version_name'] = data.get('version_name')
        data['unique_name'] = data.get('unique_name')
        data['what_is_new'] = data.get('what_is_new')
        data['description_short'] = data.get('description_short')
        data['requirements'] = data.get('requirements')
        data['is_private'] = data.get('is_private', False)
        data['security_marking'] = data.get('security_marking')

        # only checked on update, not create
        data['is_enabled'] = data.get('is_enabled', False)
        data['is_featured'] = data.get('is_featured', False)
        data['approval_status'] = data.get('approval_status')

        # Agency
        agency_title = None if data.get('agency') is None else data.get(
            'agency', {}).get('title')
        if agency_title:
            data['agency'] = agency_model_access.get_agency_by_title(
                agency_title)
            if data['agency'] is None:
                raise serializers.ValidationError('Invalid Agency')
        else:
            data['agency'] = profile.organizations.all()[0]

        # Listing Type
        type_title = None if data.get('listing_type') is None else data.get(
            'listing_type', {}).get('title')
        if type_title:
            data['listing_type'] = model_access.get_listing_type_by_title(
                type_title)
        else:
            data['listing_type'] = None

        # Images
        image_keys = [
            'small_icon', 'large_icon', 'banner_icon', 'large_banner_icon'
        ]

        for image_key in image_keys:
            current_image_value = data.get(image_key)
            if current_image_value:
                if 'id' not in current_image_value:
                    raise serializers.ValidationError(
                        'Image({!s}) requires a {!s}'.format(image_key, 'id'))
                if current_image_value.get('security_marking') is None:
                    current_image_value[
                        'security_marking'] = constants.DEFAULT_SECURITY_MARKING
                if not access_control_instance.validate_marking(
                        current_image_value['security_marking']):
                    raise errors.InvalidInput(
                        '{!s} Security Marking is invalid'.format(image_key))
            else:
                data[image_key] = None

        # Screenshot
        screenshots = data.get('screenshots')
        if screenshots is not None:
            screenshots_out = []
            image_require_fields = ['id']
            for screenshot_set in screenshots:
                if ('small_image' not in screenshot_set
                        or 'large_image' not in screenshot_set):
                    raise serializers.ValidationError(
                        'Screenshot Set requires {0!s} fields'.format(
                            'small_image, large_icon'))
                screenshot_small_image = screenshot_set.get('small_image')
                screenshot_large_image = screenshot_set.get('large_image')

                for field in image_require_fields:
                    if field not in screenshot_small_image:
                        raise serializers.ValidationError(
                            'Screenshot Small Image requires a {0!s}'.format(
                                field))

                for field in image_require_fields:
                    if field not in screenshot_large_image:
                        raise serializers.ValidationError(
                            'Screenshot Large Image requires a {0!s}'.format(
                                field))

                if not screenshot_small_image.get('security_marking'):
                    screenshot_small_image[
                        'security_marking'] = constants.DEFAULT_SECURITY_MARKING
                if not access_control_instance.validate_marking(
                        screenshot_small_image['security_marking']):
                    raise errors.InvalidInput('Security Marking is invalid')

                if not screenshot_large_image.get('security_marking'):
                    screenshot_large_image[
                        'security_marking'] = constants.DEFAULT_SECURITY_MARKING
                if not access_control_instance.validate_marking(
                        screenshot_large_image['security_marking']):
                    raise errors.InvalidInput('Security Marking is invalid')

                screenshots_out.append(screenshot_set)
                data['screenshots'] = screenshots_out
        else:
            data['screenshots'] = None

        # Contacts
        if 'contacts' in data:
            for contact in data['contacts']:
                if 'name' not in contact:
                    raise serializers.ValidationError(
                        'Contact requires [name] field')
                if 'email' not in contact:
                    raise serializers.ValidationError(
                        'Contact requires [email] field')
                if 'secure_phone' not in contact:
                    contact['secure_phone'] = None
                if 'unsecure_phone' not in contact:
                    contact['unsecure_phone'] = None
                if 'contact_type' not in contact:
                    raise serializers.ValidationError(
                        'Contact requires [contact_type] field')

                contact_type = contact.get('contact_type')
                contact_type_name = None if contact_type is None else contact_type.get(
                    'name')

                if not contact_type_name:
                    raise serializers.ValidationError(
                        'Contact field requires correct format')

                contact_type_instance = contact_type_model_access.get_contact_type_by_name(
                    contact_type_name)
                if not contact_type_instance:
                    raise serializers.ValidationError(
                        'Contact Type [{}] not found'.format(
                            contact_type_name))

        # owners
        owners = []
        if 'owners' in data:
            for owner in data['owners']:
                user_dict = owner.get('user')
                user_username = None if user_dict is None else user_dict.get(
                    'username')

                if not user_username:
                    raise serializers.ValidationError(
                        'Owner field requires correct format')

                owner_profile = generic_model_access.get_profile(user_username)
                if not owner_profile:
                    raise serializers.ValidationError(
                        'Owner Profile not found')

                # Don't allow user to select a security marking that is above
                # their own access level
                if not system_has_access_control(owner_profile.user.username,
                                                 data.get('security_marking')):
                    raise serializers.ValidationError(
                        'Security marking too high for current owner profile')

                owners.append(owner_profile)
        data['owners'] = owners

        # Categories
        categories = []
        if 'categories' in data:
            for category in data['categories']:
                category_title = category.get('title')
                if not category_title:
                    raise serializers.ValidationError(
                        'Categories field requires correct format')

                category_instance = category_model_access.get_category_by_title(
                    category_title)
                if not category_instance:
                    raise serializers.ValidationError(
                        'Category [{}] not found'.format(category_title))

                categories.append(category_instance)
        data['categories'] = categories

        # Intents
        intents = []
        if 'intents' in data:
            for intent in data['intents']:
                intent_action = intent.get('action')
                if not intent_action:
                    raise serializers.ValidationError(
                        'Intent field requires correct format')

                intent_instance = intent_model_access.get_intent_by_action(
                    intent_action)
                if not intent_instance:
                    raise serializers.ValidationError(
                        'Intent Action [{}] not found'.format(intent_action))

                intents.append(intent_instance)
        data['intents'] = intents

        # doc urls will be created in create()
        if 'doc_urls' in data:
            pass

        # tags will be created (if necessary) in create()
        if 'tags' in data:
            pass

        # logger.debug('leaving ListingSerializer.validate', extra={'request':self.context.get('request')})
        return data
def search(request_username, search_param_parser):
    """
    Filter Listings
    Too many variations to cache results

    Users shall be able to search for listings'
     - title
     - description
     - description_short
     - tags__name

    Filter by
     - category
     - agency
     - listing types

    Users shall only see what they are authorized to see
      'is_private': false,
      'approval_status': 'APPROVED',
      'is_deleted': false,
      'is_enabled': true,
      'security_marking': 'UNCLASSIFIED',

    Sorted by Relevance
      'avg_rate': 0,
      'total_votes': 0,
      'total_rate5': 0,
      'total_rate4': 0,
      'total_rate3': 0,
      'total_rate2': 0,
      'total_rate1': 0,
      'total_reviews': 0,
      'is_featured': true,

    It must respects restrictions
     - Private apps (apps only from user's agency)
     - User's max_classification_level

    Args:
        username(str): username
        search_param_parser(SearchParamParser): parameters
    """
    elasticsearch_factory.check_elasticsearch()
    # Create ES client
    es_client = elasticsearch_factory.get_client()

    user_exclude_orgs = get_user_exclude_orgs(request_username)
    search_query = elasticsearch_util.make_search_query_obj(
        search_param_parser, exclude_agencies=user_exclude_orgs)

    # print(json.dumps(search_query, indent=4))
    res = es_client.search(index=settings.ES_INDEX_NAME, body=search_query)

    hits = res.get('hits', {})
    inner_hits = hits.get('hits', None)
    if not hits:
        return []

    hit_titles = []

    excluded_count = 0

    for current_innter_hit in inner_hits:
        source = current_innter_hit.get('_source')
        source['_score'] = current_innter_hit.get('_score')

        # Add URLs to icons
        image_keys_to_add_url = [
            'large_icon', 'small_icon', 'banner_icon', 'large_banner_icon'
        ]

        for image_key in image_keys_to_add_url:
            if source.get(image_key) is not None:
                if search_param_parser.base_url:
                    source[image_key]['url'] = '{!s}/api/image/{!s}/'.format(
                        search_param_parser.base_url, source[image_key]['id'])
                else:
                    source[image_key]['url'] = '/api/image/{!s}/'.format(
                        source[image_key]['id'])

        exclude_bool = False
        if not source.get('security_marking'):
            exclude_bool = True
            logger.debug('Listing {0!s} has no security_marking'.format(
                source.get('title')))
        if not system_has_access_control(request_username,
                                         source.get('security_marking')):
            exclude_bool = True

        if exclude_bool is False:
            hit_titles.append(source)
        else:
            excluded_count = excluded_count + 1

    # Total Records in Elasticsearch
    final_count = hits.get('total')
    # Total Records minus what the user does not have access to see, this count should never be below zero
    # TODO: Figure out smarter logic for excluded_count compensation (rivera 11/14/2016)
    final_count_with_excluded = final_count - excluded_count

    final_results = {'count': final_count_with_excluded, 'results': hit_titles}

    final_results['previous'] = None
    final_results['next'] = None

    # if final_count_with_excluded < 0 then previous and next should be None
    if final_count_with_excluded < 0:
        return final_results

    previous_offset_prediction = search_param_parser.offset - search_param_parser.limit
    next_offset_prediction = search_param_parser.offset + search_param_parser.limit

    final_results['next_offset_prediction'] = next_offset_prediction

    # Previous URL - previous_offset_prediction is less than zero, previous should be None
    if previous_offset_prediction >= 0:
        final_results['previous'] = generate_link(search_param_parser,
                                                  previous_offset_prediction)

    # Next URL
    if next_offset_prediction <= final_count_with_excluded:
        final_results['next'] = generate_link(search_param_parser,
                                              next_offset_prediction)

    return final_results