2017-09-18 16:13:30 -07:00
|
|
|
import Ember from 'ember';
|
2017-12-06 10:23:33 -08:00
|
|
|
import { capitalize } from '@ember/string';
|
2017-12-14 12:57:17 -08:00
|
|
|
import { ISecurityClassificationOption } from 'wherehows-web/constants/dataset-compliance';
|
2017-10-13 23:08:39 -07:00
|
|
|
import {
|
|
|
|
Classification,
|
|
|
|
nonIdFieldLogicalTypes,
|
|
|
|
NonIdLogicalType,
|
|
|
|
idLogicalTypes,
|
|
|
|
genericLogicalTypes,
|
|
|
|
fieldIdentifierTypes,
|
2017-10-17 11:33:06 -07:00
|
|
|
IdLogicalType,
|
2017-12-11 18:26:26 -08:00
|
|
|
ComplianceFieldIdValue
|
2017-10-13 23:08:39 -07:00
|
|
|
} from 'wherehows-web/constants/datasets/compliance';
|
2017-12-14 12:57:17 -08:00
|
|
|
import { IComplianceField } from 'wherehows-web/constants/index';
|
2017-12-12 22:19:01 -08:00
|
|
|
import { IComplianceDataType } from 'wherehows-web/typings/api/list/compliance-datatypes';
|
2017-09-27 09:33:20 -07:00
|
|
|
|
2017-12-11 13:13:34 -08:00
|
|
|
/**
|
|
|
|
* Describes the interface for a drop down option for the field format column in the compliance table
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @interface IFieldFormatDropdownOption
|
|
|
|
*/
|
|
|
|
export interface IFieldFormatDropdownOption {
|
|
|
|
value: IdLogicalType | NonIdLogicalType;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
2017-10-04 10:56:59 -07:00
|
|
|
/**
|
|
|
|
* Length of time between suggestion modification time and last modified time for the compliance policy
|
|
|
|
* If a policy has been updated within the range of this window then it is considered as stale / or
|
|
|
|
* has been seen previously
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
const lastSeenSuggestionInterval: number = 7 * 24 * 60 * 60 * 1000;
|
|
|
|
|
2017-10-03 18:07:38 -07:00
|
|
|
/**
|
|
|
|
* Percentage value for a compliance policy suggestion with a low confidence score
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
const lowQualitySuggestionConfidenceThreshold = 0.5;
|
|
|
|
|
2017-04-28 22:02:46 -07:00
|
|
|
/**
|
|
|
|
* Stores a unique list of classification values
|
2017-12-12 22:19:01 -08:00
|
|
|
* @type {Array<Classification>} the list of classification values
|
2017-04-28 22:02:46 -07:00
|
|
|
*/
|
2017-12-12 22:19:01 -08:00
|
|
|
const classifiers = Object.values(Classification);
|
2017-05-19 03:05:25 -07:00
|
|
|
|
2017-12-06 10:23:33 -08:00
|
|
|
/**
|
|
|
|
* Takes a string, returns a formatted string. Niche , single use case
|
|
|
|
* for now, so no need to make into a helper
|
|
|
|
* @param {string} string
|
|
|
|
*/
|
2017-12-12 22:19:01 -08:00
|
|
|
const formatAsCapitalizedStringWithSpaces = (string: string) => capitalize(string.toLowerCase().replace(/[_]/g, ' '));
|
2017-12-06 10:23:33 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A derived list of security classification options from classifiers list, including an empty string option and value
|
|
|
|
* @type {Array<ISecurityClassificationOption>}
|
|
|
|
*/
|
|
|
|
const securityClassificationDropdownOptions: Array<ISecurityClassificationOption> = [
|
2017-12-14 12:57:17 -08:00
|
|
|
null,
|
2017-12-06 10:23:33 -08:00
|
|
|
...classifiers.sort()
|
2017-12-14 12:57:17 -08:00
|
|
|
].map((value: ISecurityClassificationOption['value']) => ({
|
2017-12-06 10:23:33 -08:00
|
|
|
value,
|
2017-12-14 12:57:17 -08:00
|
|
|
label: value ? formatAsCapitalizedStringWithSpaces(value) : 'Unspecified',
|
|
|
|
isDisabled: !value
|
2017-12-06 10:23:33 -08:00
|
|
|
}));
|
|
|
|
|
2017-09-18 16:13:30 -07:00
|
|
|
/**
|
|
|
|
* Checks if the identifierType is a mixed Id
|
|
|
|
* @param {string} identifierType
|
2017-10-13 23:08:39 -07:00
|
|
|
* @return {boolean}
|
2017-09-18 16:13:30 -07:00
|
|
|
*/
|
|
|
|
const isMixedId = (identifierType: string) => identifierType === fieldIdentifierTypes.generic.value;
|
|
|
|
/**
|
|
|
|
* Checks if the identifierType is a custom Id
|
|
|
|
* @param {string} identifierType
|
2017-10-13 23:08:39 -07:00
|
|
|
* @return {boolean}
|
2017-09-18 16:13:30 -07:00
|
|
|
*/
|
|
|
|
const isCustomId = (identifierType: string) => identifierType === fieldIdentifierTypes.custom.value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of logicalType mappings for displaying its value and a label by logicalType
|
2017-12-11 13:13:34 -08:00
|
|
|
* @template T IdLogicalType | NonIdLogicalType
|
|
|
|
* @template K 'id' | 'generic'
|
|
|
|
* @param {K} logicalType
|
2017-12-12 22:19:01 -08:00
|
|
|
* @returns {(Array<{ value: T; label: string }>)}
|
2017-12-11 13:13:34 -08:00
|
|
|
*/
|
|
|
|
const logicalTypeValueLabel = <T extends IdLogicalType | NonIdLogicalType, K extends 'id' | 'generic'>(
|
|
|
|
logicalType: K
|
|
|
|
) => {
|
|
|
|
const logicalTypes: Array<IdLogicalType | NonIdLogicalType> = {
|
2017-09-18 16:13:30 -07:00
|
|
|
id: idLogicalTypes,
|
|
|
|
generic: genericLogicalTypes
|
2017-10-13 23:08:39 -07:00
|
|
|
}[logicalType];
|
|
|
|
|
2017-12-11 13:13:34 -08:00
|
|
|
return logicalTypes.map((value: T) => {
|
2017-10-13 23:08:39 -07:00
|
|
|
let label: string;
|
|
|
|
|
|
|
|
// guard checks that if the logical type string is generic, then the value union can be assumed to be
|
|
|
|
// a NonIdLogicalType, otherwise it is an id /custom logicalType
|
|
|
|
if (logicalType === 'generic') {
|
|
|
|
label = nonIdFieldLogicalTypes[<NonIdLogicalType>value].displayAs;
|
|
|
|
} else {
|
|
|
|
label = value.replace(/_/g, ' ').replace(/([A-Z]{3,})/g, value => Ember.String.capitalize(value.toLowerCase()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
label
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2017-09-18 16:13:30 -07:00
|
|
|
|
2017-10-13 23:08:39 -07:00
|
|
|
/**
|
|
|
|
* Map logicalTypes to options consumable by DOM
|
2017-12-11 13:13:34 -08:00
|
|
|
* @returns {Array<IFieldFormatDropdownOption>}
|
2017-10-13 23:08:39 -07:00
|
|
|
*/
|
2017-12-11 13:13:34 -08:00
|
|
|
const logicalTypesForIds: Array<IFieldFormatDropdownOption> = logicalTypeValueLabel('id');
|
2017-09-18 16:13:30 -07:00
|
|
|
|
2017-10-13 23:08:39 -07:00
|
|
|
/**
|
|
|
|
* Map generic logical type to options consumable in DOM
|
2017-12-11 13:13:34 -08:00
|
|
|
* @returns {Array<IFieldFormatDropdownOption>}
|
2017-10-13 23:08:39 -07:00
|
|
|
*/
|
2017-12-11 13:13:34 -08:00
|
|
|
const logicalTypesForGeneric: Array<IFieldFormatDropdownOption> = logicalTypeValueLabel('generic');
|
2017-09-18 16:13:30 -07:00
|
|
|
|
2017-09-27 09:33:20 -07:00
|
|
|
/**
|
|
|
|
* Caches a list of fieldIdentifierTypes values
|
2017-12-11 18:26:26 -08:00
|
|
|
* @type {Array<ComplianceFieldIdValue>}
|
2017-09-27 09:33:20 -07:00
|
|
|
*/
|
2017-12-11 18:26:26 -08:00
|
|
|
const fieldIdentifierTypeValues: Array<ComplianceFieldIdValue> = Object.values(ComplianceFieldIdValue);
|
2017-09-27 09:33:20 -07:00
|
|
|
|
2017-12-12 22:19:01 -08:00
|
|
|
/**
|
|
|
|
* Retrieves the default security classification for an identifier type, or null if it does not exist
|
|
|
|
* @param {Array<IComplianceDataType>} [complianceDataTypes=[]] the list of compliance data types
|
|
|
|
* @param {ComplianceFieldIdValue} identifierType the compliance data type id string
|
|
|
|
* @returns {(IComplianceDataType['defaultSecurityClassification'] | null)}
|
|
|
|
*/
|
|
|
|
const getDefaultSecurityClassification = (
|
|
|
|
complianceDataTypes: Array<IComplianceDataType> = [],
|
2017-12-14 12:57:17 -08:00
|
|
|
identifierType: IComplianceField['identifierType']
|
2017-12-12 22:19:01 -08:00
|
|
|
): IComplianceDataType['defaultSecurityClassification'] | null => {
|
2017-12-14 12:57:17 -08:00
|
|
|
const complianceDataType = complianceDataTypes.findBy('id', identifierType || '');
|
2017-12-12 22:19:01 -08:00
|
|
|
|
|
|
|
return complianceDataType ? complianceDataType.defaultSecurityClassification : null;
|
|
|
|
};
|
|
|
|
|
2017-05-23 12:18:55 -07:00
|
|
|
export {
|
2017-12-06 10:23:33 -08:00
|
|
|
securityClassificationDropdownOptions,
|
|
|
|
formatAsCapitalizedStringWithSpaces,
|
2017-09-27 09:33:20 -07:00
|
|
|
fieldIdentifierTypeValues,
|
2017-09-18 16:13:30 -07:00
|
|
|
isMixedId,
|
|
|
|
isCustomId,
|
|
|
|
logicalTypesForIds,
|
|
|
|
logicalTypesForGeneric,
|
2017-10-04 10:56:59 -07:00
|
|
|
lastSeenSuggestionInterval,
|
2017-10-16 18:11:57 -07:00
|
|
|
lowQualitySuggestionConfidenceThreshold,
|
2017-12-12 22:19:01 -08:00
|
|
|
logicalTypeValueLabel,
|
|
|
|
getDefaultSecurityClassification
|
2017-05-23 12:18:55 -07:00
|
|
|
};
|