Exemplo n.º 1
0
    def list(self, request, profile_pk=None):
        """
        Retrieves all listings for a specific profile that they own
        """

        current_request_username = request.user.username
        queryset = self.get_queryset(current_request_username, profile_pk)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            current_request_username)

        if anonymize_identifiable_data:
            return Response([])

        if queryset:
            page = self.paginate_queryset(queryset)

            if page is not None:
                serializer = listing_serializers.ListingSerializer(
                    page, context={'request': request}, many=True)
                response = self.get_paginated_response(serializer.data)
                return response

            serializer = listing_serializers.ListingSerializer(
                queryset, context={'request': request}, many=True)
            return Response(serializer.data)
        else:
            return Response({'detail': 'Not Found'},
                            status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 2
0
    def to_representation(self, data):
        access_control_instance = plugin_manager.get_system_access_control_plugin(
        )
        ret = super(ProfileSerializer, self).to_representation(data)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            self.context['request'].user.username)

        from_current_view = self.context.get('self')

        if from_current_view:
            if anonymize_identifiable_data:
                ret['second_party_user'] = True
            else:
                ret['second_party_user'] = False

        check_request_self = False
        if self.context['request'].user.id == ret['id']:
            check_request_self = True

        if anonymize_identifiable_data and not check_request_self:
            ret['display_name'] = access_control_instance.anonymize_value(
                'display_name')
            ret['bio'] = access_control_instance.anonymize_value('bio')
            ret['dn'] = access_control_instance.anonymize_value('dn')

        return ret
Exemplo n.º 3
0
    def to_representation(self, data):
        ret = super(ListingSerializer, self).to_representation(data)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            self.context['request'].user.username)

        if anonymize_identifiable_data:
            ret['contacts'] = []

        return ret
Exemplo n.º 4
0
    def to_representation(self, data):
        ret = super(NotificationSerializer, self).to_representation(data)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            self.context['request'].user.username)

        if anonymize_identifiable_data:
            pass
            # TODO: Hide Peer data (erivera 2016-07-29)

        return ret
Exemplo n.º 5
0
    def to_representation(self, data):
        access_control_instance = plugin_manager.get_system_access_control_plugin(
        )
        ret = super(ContactTypeSerializer, self).to_representation(data)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            self.context['request'].user.username)

        if anonymize_identifiable_data:
            ret['name'] = access_control_instance.anonymize_value(
                'contact_type_name')

        return ret
Exemplo n.º 6
0
    def to_representation(self, data):
        access_control_instance = plugin_manager.get_system_access_control_plugin()
        ret = super(NotificationSerializer, self).to_representation(data)

        peer = ret['peer']
        if peer and peer.get('_bookmark_listing_ids'):
            del peer['_bookmark_listing_ids']
        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(self.context['request'].user.username)

        if anonymize_identifiable_data:
            if peer:
                peer['user']['username'] = access_control_instance.anonymize_value('username')
            # TODO: Hide Peer data (erivera 2016-07-29)

        return ret
Exemplo n.º 7
0
    def to_representation(self, data):
        access_control_instance = plugin_manager.get_system_access_control_plugin(
        )
        ret = super(ShortProfileSerializer, self).to_representation(data)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            self.context['request'].user.username)

        check_request_self = False
        if self.context['request'].user.id == ret['id']:
            check_request_self = True

        if anonymize_identifiable_data and not check_request_self:
            ret['display_name'] = access_control_instance.anonymize_value(
                'display_name')
            ret['dn'] = access_control_instance.anonymize_value('dn')

        return ret
Exemplo n.º 8
0
def delete_listing(username, listing):
    """
    TODO: need a way to keep track of this listing as being deleted.

    for now just remove
    """
    profile = generic_model_access.get_profile(username)
    # app_owners = [i.user.username for i in listing.owners.all()]
    # ensure user is the author of this review, or that user is an org
    # steward or apps mall steward

    # Don't allow 2nd-party user to be an delete a listing
    if system_anonymize_identifiable_data(profile.user.username):
        raise errors.PermissionDenied(
            'Current profile has does not have delete permissions')

    priv_roles = ['APPS_MALL_STEWARD', 'ORG_STEWARD']
    if profile.highest_role(
    ) in priv_roles or listing.approval_status == 'IN_PROGRESS':
        pass
    else:
        raise errors.PermissionDenied(
            'Only Org Stewards and admins can delete listings')

    if listing.is_deleted:
        raise errors.PermissionDenied('The listing has already been deleted')
    old_approval_status = listing.approval_status
    listing = _add_listing_activity(profile, listing,
                                    models.ListingActivity.DELETED)
    listing.is_deleted = True
    listing.is_enabled = False
    listing.is_featured = False
    listing.approval_status = models.Listing.DELETED
    # TODO Delete the values of other field
    # Keep lisiting as shell listing for history
    listing.save()
    # listing.delete()

    dispatcher.publish('listing_approval_status_changed',
                       listing=listing,
                       profile=profile,
                       old_approval_status=old_approval_status,
                       new_approval_status=listing.approval_status)
Exemplo n.º 9
0
    def to_representation(self, data):
        access_control_instance = plugin_manager.get_system_access_control_plugin(
        )
        ret = super(ContactSerializer, self).to_representation(data)

        # Used to anonymize usernames
        anonymize_identifiable_data = system_anonymize_identifiable_data(
            self.context['request'].user.username)

        if anonymize_identifiable_data:
            ret['secure_phone'] = access_control_instance.anonymize_value(
                'secure_phone')
            ret['unsecure_phone'] = access_control_instance.anonymize_value(
                'unsecure_phone')
            ret['secure_phone'] = access_control_instance.anonymize_value(
                'secure_phone')
            ret['name'] = access_control_instance.anonymize_value('name')
            ret['organization'] = access_control_instance.anonymize_value(
                'organization')
            ret['email'] = access_control_instance.anonymize_value('email')
        return ret
Exemplo n.º 10
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')

        # Don't allow 2nd-party user to be an submit/edit a listing
        if system_anonymize_identifiable_data(profile.user.username):
            raise serializers.ValidationError(
                'Permissions are invalid for current profile')

        # TODO: errors.PermissionDenied instead of serializers.ValidationError

        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')

                # Don't allow 2nd-party user to be an owner of a listing
                if system_anonymize_identifiable_data(
                        owner_profile.user.username):
                    raise serializers.ValidationError(
                        'Permissions are invalid 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