Name
global.PrivacyPopulatePrimaryReferenceLinks
Description
Populate dp_primary_reference for classified tables that have primary reference field(s)
Script
var PrivacyPopulatePrimaryReferenceLinks = Class.create();
PrivacyPopulatePrimaryReferenceLinks.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/*
* Populate dp_primary_reference entries for a privacyConfig and dataClass
* @sysparm privacyConfigId dp_configuration sys_id
* @sysparm dataClassid data_classification sys_id
*/
populateLinks: function() {
var privacyConfigId = this.getParameter('sysparm_privacyConfigId');
var dataClassId = this.getParameter('sysparm_dataClassId');
var primaryTableName = this.getParameter('sysparm_primaryTableName');
return this.populateLinksUtil(privacyConfigId, dataClassId, primaryTableName);
},
populateLinksUtil: function(privacyConfigId, dataClassId, primaryTableName) {
var existingTables = this._getExistingTables(privacyConfigId);
var possibleTables = this._getPossibleTables(dataClassId);
var tablesToAdd = this._getTablesToAdd(existingTables, possibleTables);
tablesToAdd = this._filterTablesWithPrimaryReference(tablesToAdd, primaryTableName);
tablesToAdd.forEach(function(item) {
var gr = new GlideRecord('dp_primary_reference');
gr.setValue('table', item);
gr.setValue('reference_column', '');
gr.setValue('privacy_config', privacyConfigId);
gr.insert();
});
// return a value to make it easy to test excute ACL for this script include
return true;
},
/*
* Return true if and only if a table has a primary table reference field
* @param tableDictionaryId sys_id of table entry in sys_dictionary
*/
tableHasPrimaryReference: function(tableName, primaryTableName) {
var parentTablesAndSelf = GlideDBObjectManager.getTables(tableName);
var ptString = parentTablesAndSelf.toString().replace('[', '').replace(']', ''); // Convert JSON to String
var dbom = GlideDBObjectManager.get();
var tables = dbom.getAllExtensions(primaryTableName);
var tbString = tables.toString().replace('[', '').replace(']', ''); // Convert JSON to String
var now_GR = new GlideRecord('sys_dictionary');
now_GR.addQuery('name', 'IN', ptString);
now_GR.addQuery('internal_type', 'reference');
now_GR.addQuery('reference', 'IN', tbString);
now_GR.query();
var rv = now_GR.hasNext();
return rv;
},
/*
* Return list of primary reference fields for a table, including those from ancestor tables
* @param name table name
* @primaryTableName primary table name (typically sys_user)
*/
getPrimaryReferenceFieldsForTable: function(table, primaryTableName) {
var gru = new GlideRecordUtil();
var tables = gru.getTables(table);
var tbString = tables.toString().replace('[', '').replace(']', ''); // Convert JSON to String
var dbom = GlideDBObjectManager.get();
var primaryTables = dbom.getAllExtensions(primaryTableName);
var primaryTbString = primaryTables.toString().replace('[', '').replace(']', ''); // Convert JSON to String
var gr = new GlideRecord('sys_dictionary');
gr.addQuery('name', 'IN', tbString);
gr.addQuery('internal_type', 'reference');
gr.addQuery('reference', 'IN', primaryTbString);
gr.orderBy('element');
gr.query();
var rv = [];
while (gr.next()) {
var td = GlideTableDescriptor.get(gr.getValue("name"));
var ed = td.getElementDescriptor(gr.getValue("element"));
if (ed == null || ed.isFirstTableName())
rv.push(gr.getUniqueValue());
}
return rv;
},
/*
* Return list of eligible sysIds of dp_technique_with_params.
* @param sys_id from m2m_dictionary_dataclass table
*/
getEligibleTechniqueConfigs: function(m2mClassSysId) {
var techniqueConfigSysIds = [];
var m2mClassRecord = new GlideRecord('m2m_dictionary_dataclass');
m2mClassRecord.addQuery("sys_id", m2mClassSysId);
m2mClassRecord.query();
if(!m2mClassRecord.next()) {
gs.error("Did not find records in m2m_dictionary_dataclass for id: " + m2mClassSysId);
return techniqueConfigSysIds;
}
var sysDictionarySysId = m2mClassRecord.getValue("sys_dictionary");
var sys_dictionary_record = new GlideRecord('sys_dictionary');
sys_dictionary_record.addQuery("sys_id", sysDictionarySysId);
sys_dictionary_record.query();
if(!sys_dictionary_record.next()) {
gs.error("Did not find records in sys_dictionary for id: " + sysDictionarySysId);
return techniqueConfigSysIds;
}
var tableName = sys_dictionary_record.getValue("name");
var column = sys_dictionary_record.getValue("element");
return this.getEligibleTechniqueConfigsByTableAndColumn(tableName, column);
},
/**
*Return list of eligible sysIds of dp_technique_with_params.
* @param tableName
* @param column
*/
getEligibleTechniqueConfigsByTableAndColumn: function(tableName, column) {
var techniqueConfigSysIds = [];
var privacyApi = new SNC.DataPrivacyApi();
var records = privacyApi.getEligibleTechniqueParameters(tableName, column);
while (records.next()) {
techniqueConfigSysIds.push(records.getValue('sys_id'));
}
return techniqueConfigSysIds;
},
/*
* Return list of distinct tables referenced for a data class from m2m_dictionary_dataclass
* @param dataClassId data_classification table entry sys_id
*/
getClassifiedTablesForDataClass: function(dataClassId, primaryTableName) {
var jobApi = new SNC.DataProtectionJob();
var gr = new GlideRecord('m2m_dictionary_dataclass');
gr.addQuery('data_class', dataClassId);
gr.query();
var rv = [];
while (gr.next()) {
var internalType = gr.getElement('sys_dictionary.internal_type').getValue();
if (!jobApi.isSupportedFieldType(internalType))
continue;
var table = gr.getElement('sys_dictionary.name').getValue();
if (table !== primaryTableName && rv.indexOf(table) === -1)
rv.push(table);
}
return rv;
},
_getExistingTables: function(privacyConfigId) {
var gr = new GlideRecord('dp_primary_reference');
gr.addQuery('privacy_config', privacyConfigId);
gr.query();
var existingTables = [];
while (gr.next())
existingTables.push(gr.getValue('table'));
return existingTables;
},
_getPossibleTables: function(dataClassId) {
var gr = new GlideRecord('m2m_dictionary_dataclass');
gr.addQuery('data_class', dataClassId);
gr.query();
var possibleTables = [];
while (gr.next()) {
var table = gr.getElement('sys_dictionary.name').getValue();
if (possibleTables.indexOf(table) === -1)
possibleTables.push(table);
}
return possibleTables;
},
_getTablesToAdd: function(existingTables, possibleTables) {
// We want to return possibleTables - existingTables
// Ensure logic works on ES5 and all recent browser versions
var tablesToAdd = [];
possibleTables.forEach(function(item) {
if (existingTables.indexOf(item) === -1)
tablesToAdd.push(item);
});
return tablesToAdd;
},
_filterTablesWithPrimaryReference: function(tables, primaryTableName) {
var filteredTables = [];
var dbom = GlideDBObjectManager.get();
var primaryTables = dbom.getAllExtensions(primaryTableName);
var tbString = primaryTables.toString().replace('[', '').replace(']', ''); // Convert JSON to String
tables.forEach(function(table) {
var parentTablesAndSelf = GlideDBObjectManager.getTables(table);
var ptString = parentTablesAndSelf.toString().replace('[', '').replace(']', ''); // Convert JSON to String
var now_GR = new GlideRecord('sys_dictionary');
now_GR.addQuery('name', 'IN', ptString);
now_GR.addQuery('internal_type', 'reference');
now_GR.addQuery('reference', 'IN', tbString);
now_GR.query();
if (now_GR.hasNext()) {
if (primaryTables.indexOf(table) === -1 && filteredTables.indexOf(table) === -1)
filteredTables.push(table);
}
});
return filteredTables;
},
type: 'PrivacyPopulatePrimaryReferenceLinks'
});
Sys ID
9ce5b912eb4230101c396f16c55228c1