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
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
def to_representation(self, data): access_control_instance = plugin_manager.get_system_access_control_plugin() ret = super(ListingContactTypeSerializer, 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
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
def to_representation(self, data): access_control_instance = plugin_manager.get_system_access_control_plugin() ret = super(CreateListingProfileSerializer, 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') return ret
def to_representation(self, data): access_control_instance = plugin_manager.get_system_access_control_plugin() ret = super(CreateListingProfileSerializer, 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') return ret
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) # del ret['agency'] # del ret['listing'] return ret
def validate_listing(serializer_instance, data): # TODO Put in listing model_access.py access_control_instance = plugin_manager.get_system_access_control_plugin() profile = generic_model_access.get_profile(serializer_instance.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['usage_requirements'] = data.get('usage_requirements') data['system_requirements'] = data.get('system_requirements') data['is_private'] = data.get('is_private') data['security_marking'] = data.get('security_marking') # only checked on update, not create data['is_enabled'] = data.get('is_enabled') data['is_508_compliant'] = data.get('is_508_compliant') data['is_featured'] = data.get('is_featured') 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 return data
def validate_listing(serializer_instance, data): # TODO Put in listing model_access.py access_control_instance = plugin_manager.get_system_access_control_plugin() profile = generic_model_access.get_profile(serializer_instance.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['usage_requirements'] = data.get('usage_requirements') data['system_requirements'] = data.get('system_requirements') data['is_private'] = data.get('is_private', False) data['is_exportable'] = data.get('is_exportable', False) data['security_marking'] = data.get('security_marking') # only checked on update, not create data['is_enabled'] = data.get('is_enabled', False) data['is_508_compliant'] = data.get('is_508_compliant', 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 return data