Name

sn_reacf.RemedialActionHelperSNC

Description

No description available

Script

var RemedialActionHelperSNC = Class.create();
RemedialActionHelperSNC.prototype = {
  initialize: function() {},

  TABLES: {
  	ACTION_ROLE: "sn_reacf_remedial_role",
  	USER_CRITERIA_ACTION_INCLUSION: "sn_reacf_remedial_user_criteria_mtom",
  	USER_CRITERIA_ACTION_EXCLUSION: "sn_reacf_remedial_user_criteria_no_mtom",
  	REMEDIAL_ACTION: "sn_reacf_remedial_action",
  	ACTION_PARAMETER: "sn_reacf_remedial_action_parameter",
  	ACTION_EXECUTION: "sn_reacf_remedial_action_execution",
  	SN_REACF_REMEDIAL_ACTION_PARAMETER: "sn_reacf_remedial_action_parameter"
  },

  REMEDIAL_ACTION_PROVIDER_EXTENSION_POINT: "sn_reacf.RemedialActionProvider",

  USER_CRITERIA: "user_criteria",

  REMEDIAL_ACTION_ENGINE_EXTENSION_POINT: "sn_reacf.RemedialActionEngine",

  FIELDS: {
  	ACTION_ROLE: {
  		ACTION: "remedial_action",
  		ROLE: "role"
  	},
  	USER_CRITERIA_ACTION_INCLUSION: {
  		ACTION: "remedial_action",
  		USER_CRITERIA: "user_criteria"
  	},
  	USER_CRITERIA_ACTION_EXCLUSION: {
  		ACTION: "remedial_action",
  		USER_CRITERIA: "user_criteria"
  	},
  	ACTION_PARAMETER: {
  		MODEL: "model",
  		ELEMENT: "element",
  		MANDATORY: "mandatory",
  		DEFAULT_VALUE: "default_value"
  	},
  	ACTION_EXECUTION: {
  		REMEDIAL_ACTION: "remedial_action",
  		STATE: "state",
  		PARENT_RECORD_TABLE: "parent_record_table",
  		PARENT_RECORD_ID: "parent_record_id",
  		ACTION_RECORD_ID: "action_record_id",
  		ACTION_RECORD_TABLE: "action_record_table",
  		ACTION_PARAMETERS: "remedial_action_parameters",
  		TARGET_RECORD_ID: "target_record_id",
  		USER_EXECUTED: "user_executed",
  		ORIGIN: "origin"
  	},
  	ACTION: {
  		ACTION: "action_id",
  		ACTION_TABLE: "action_table"
  	},
  	SYS_DOMAIN: "sys_domain",
  	MODEL: "model",
  	ELEMENT: "element",
  },

  ACTION_EXECUTION_STATES: {
  	NEW: "new",
  	IN_PROGRESS: "in_progress",
  	COMPLETED: "completed",
  	FAILED: "failed"
  },

  isAuthorized: function(remedialAction) {
  	var isUserRoleAllowed = this._isUserRoleAllowed(remedialAction);
  	var isUserCriteriaIncluded = this._isUserCriteriaIncluded(remedialAction);
  	var isUserCriteriaExcluded = this._isUserCriteriaExcluded(remedialAction);
  	if (!isUserRoleAllowed['isConfigured'] && !isUserCriteriaIncluded['isConfigured'] && !isUserCriteriaExcluded['isConfigured'])
  		return false;
  	if (!isUserRoleAllowed['status'] || !isUserCriteriaIncluded['status'] || isUserCriteriaExcluded['status'])
  		return false;
  	return true;
  },

  _isUserRoleAllowed: function(remedialAction) {
  	var remedialRoleGr = new GlideRecord(this.TABLES.ACTION_ROLE);
  	remedialRoleGr.addQuery(this.FIELDS.ACTION_ROLE.ACTION, remedialAction.getUniqueValue());
  	remedialRoleGr.addNotNullQuery(this.FIELDS.ACTION_ROLE.ROLE);
  	remedialRoleGr.query();
  	if (!remedialRoleGr.hasNext()){
  		return {
  			"isConfigured": false,
  			"status": true
  		};
  	}
  	while (remedialRoleGr.next()){
  		if (gs.hasRole(remedialRoleGr.role.name)){
  			return {
  				"isConfigured": true,
  				"status": true
  			};
  		}
  	}
  	return {
  		"isConfigured": true,
  		"status": false
  	};
  },

  _isUserCriteriaIncluded: function(remedialAction) {
  	var includedUCs = this._fetchAssociatedUCs(this.TABLES.USER_CRITERIA_ACTION_INCLUSION,
  		this.FIELDS.USER_CRITERIA_ACTION_INCLUSION.ACTION,
  		remedialAction.getUniqueValue());
  	if (!includedUCs.length){
  		return {
  			"isConfigured": false,
  			"status": true
  		};
  	}
  	return {
  		"isConfigured": true,
  		"status": sn_uc.UserCriteriaLoader.userMatches(gs.getUserID(), includedUCs)
  	};
  },

  _isUserCriteriaExcluded: function(remedialAction) {
  	var excludedUCs = this._fetchAssociatedUCs(this.TABLES.USER_CRITERIA_ACTION_EXCLUSION,
  		this.FIELDS.USER_CRITERIA_ACTION_EXCLUSION.ACTION,
  		remedialAction.getUniqueValue());
  	if (!excludedUCs.length){
  		return {
  			"isConfigured": false,
  			"status": false
  		};
  	}
  	return {
  		"isConfigured": true,
  		"status": sn_uc.UserCriteriaLoader.userMatches(gs.getUserID(), excludedUCs)
  	};
  },

  _fetchAssociatedUCs: function(tableName, columnName, columnValue) {
  	var gr = new GlideRecord(tableName);
  	gr.addQuery(columnName, columnValue);
  	gr.addNotNullQuery(this.USER_CRITERIA);
  	gr.query();
  	var ucs = [];
  	while (gr.next())
  		ucs.push(gr.getValue(this.USER_CRITERIA));
  	return ucs;
  },

  insertRemedialActionExecutionRequest: function(remedialAction, actionParams, parentTable, parentRecord, targetRecord, origin) {

  	actionParams = actionParams || {};
  	if (!remedialAction) {
  		gs.error("insertRemedialActionExecutionRequest: invalid inputs");
  		return;
  	}

  	var gr = new GlideRecord(this.TABLES.ACTION_EXECUTION);
  	gr.initialize();
  	gr.setValue(this.FIELDS.ACTION_EXECUTION.REMEDIAL_ACTION, remedialAction);
  	gr.setValue(this.FIELDS.ACTION_EXECUTION.STATE, this.ACTION_EXECUTION_STATES.NEW);
  	gr.setValue(this.FIELDS.ACTION_EXECUTION.PARENT_RECORD_TABLE, parentTable);
  	gr.setValue(this.FIELDS.ACTION_EXECUTION.PARENT_RECORD_ID, parentRecord);
  	if (targetRecord) {
  		gr.setValue(this.FIELDS.ACTION_EXECUTION.TARGET_RECORD_ID, targetRecord);
  	}
  	gr.setValue(this.FIELDS.ACTION_EXECUTION.USER_EXECUTED, gs.getUserID());
  	if (origin) {
  		gr.setValue(this.FIELDS.ACTION_EXECUTION.ORIGIN, origin);
  	}
  	this._setGlideVarInputs(gr, this.FIELDS.ACTION_EXECUTION.ACTION_PARAMETERS, actionParams);
  	return gr.insert();
  },

  updateRemedialActionExecutionRequestById: function(actionRequestSysId, state, actionExecutionTable, actionExecutionId, actionParams) {
  	if (!actionRequestSysId) {
  		gs.error("updateRemedialActionExecutionRequestById: invalid inputs");
  		return;
  	}
  	var actionRequestGr = new GlideRecord(this.TABLES.ACTION_EXECUTION);
  	if (actionRequestGr.get(actionRequestSysId)) {
  		return this.updateRemedialActionExecutionRequest(actionRequestGr, state, actionExecutionTable, actionExecutionId, actionParams);
  	}
  },

  updateRemedialActionExecutionRequest: function(actionRequestGr, state, actionExecutionTable, actionExecutionId, actionParams) {
  	if (!this._isValidGr(actionRequestGr)) {
  		gs.error("updateRemedialActionExecutionRequest: invalid inputs");
  		return;
  	}
  	if (!gs.nil(state))
  		actionRequestGr.setValue(this.FIELDS.ACTION_EXECUTION.STATE, state);
  	if (!gs.nil(actionExecutionTable) && !gs.nil(actionExecutionId)) {
  		actionRequestGr.setValue(this.FIELDS.ACTION_EXECUTION.ACTION_RECORD_ID, actionExecutionId);
  		actionRequestGr.setValue(this.FIELDS.ACTION_EXECUTION.ACTION_RECORD_TABLE, actionExecutionTable);
  	}
  	if (!gs.nil(actionParams)) {
  		var remedialAction = actionRequestGr[this.FIELDS.ACTION_EXECUTION.REMEDIAL_ACTION].getRefRecord();
  		if (remedialAction.isValidRecord() && this.isActionParamsValid(remedialAction, actionParams)) {
  			this._setGlideVarInputs(actionRequestGr, this.FIELDS.ACTION_EXECUTION.ACTION_PARAMETERS, actionParams);
  		} else {
  			gs.error("updateRemedialActionExecutionRequest: Skip update as action params are invalid");
  			return;
  		}
  	}
  	return actionRequestGr.update();
  },


  isActionParamsValid: function(remedialAction, actionParams, skipMandatoryCheck) {
  	var actionParamsGr = new GlideRecord(this.TABLES.ACTION_PARAMETER);
  	actionParamsGr.addActiveQuery();
  	actionParamsGr.addQuery(this.FIELDS.ACTION_PARAMETER.MODEL, remedialAction.getUniqueValue());
  	actionParamsGr.query();
  	var activeParams = [];
  	while (actionParamsGr.next()) {
  		var paramName = actionParamsGr.getValue(this.FIELDS.ACTION_PARAMETER.ELEMENT);
  		if (typeof actionParams[paramName] === 'undefined')
  			actionParams[paramName] = actionParamsGr.getValue(this.FIELDS.ACTION_PARAMETER.DEFAULT_VALUE) || "";
  		var isParamMandatory = actionParamsGr.getValue(this.FIELDS.ACTION_PARAMETER.MANDATORY) == "1";
  		if (!skipMandatoryCheck && isParamMandatory && actionParams[paramName] === "")
  			return false;
  		activeParams.push(paramName);
  	}

  	var actionParamsKeys = Object.keys(actionParams);
  	for(var i = 0; i < actionParamsKeys.length; i++) {
  		var param = actionParamsKeys[i];
  		if(activeParams.indexOf(param) == -1) {
  			gs.info("[RemedialActionHelper]: Action parameter - " + param + " is inactive or not configured for the Action");
  			delete actionParams[param];
  		}
  	}

  	return true;
  },

  _getSysId: function(grOrSysId) {
  	return this._isValidGr(grOrSysId) ? grOrSysId.getUniqueValue() : grOrSysId;
  },

  _isValidGr: function(gr) {
  	if (gr && gr instanceof GlideRecord && gr.isValidRecord())
  		return true;
  },

  _setGlideVarInputs: function(gr, field, inputs) {
  	for (var input in inputs) {
  		if (this._isValidGr(inputs[input]))
  			gr[field][input] = inputs[input].getUniqueValue();
  		else
  			gr[field][input] = inputs[input];
  	}
  },

  getGlideRecord: function(tableName, sysId) {
  	var gr = new GlideRecord(tableName);
  	if (gr.get(sysId))
  		return gr;
  },

  isSysId: function(nameOrSysId) {
  	return nameOrSysId.length === 32 && /^[a-zA-Z0-9]+$/.test(nameOrSysId);
  },

  getGlideRecordByInternalName: function(tableName, recordName) {
  	var gr = new GlideRecord(tableName);
  	if (gr.get('internal_name', recordName))
  		return gr;
  },

  getExtensionPointImplementation: function(remedialActionGr) {
  	var implementations = new GlideScriptedExtensionPoint().getExtensions(this.REMEDIAL_ACTION_PROVIDER_EXTENSION_POINT);
  	for (var i in implementations) {
  		if (implementations[i].handles(remedialActionGr.getValue(this.FIELDS.ACTION.ACTION_TABLE + ""))) {
  			return implementations[i];
  		}
  	}
  },
  
  getActionParams: function(remedialActionExecutionGr) {
  	var remedialActionParamGr = new GlideRecord(this.TABLES.SN_REACF_REMEDIAL_ACTION_PARAMETER);
  	remedialActionParamGr.addActiveQuery();
  	remedialActionParamGr.addQuery(this.FIELDS.MODEL, remedialActionExecutionGr.getValue(this.FIELDS.ACTION_EXECUTION.REMEDIAL_ACTION));
  	remedialActionParamGr.query();
  	var actionParams = {};
  	var execParams = remedialActionExecutionGr[this.FIELDS.ACTION_EXECUTION.ACTION_PARAMETERS];
  	while (remedialActionParamGr.next()) {
  		var paramName = remedialActionParamGr.getValue(this.FIELDS.ELEMENT);
  		if (execParams[paramName])
  			actionParams[paramName] = execParams[paramName] + "";
  	}
  	return actionParams;
  },

  type: 'RemedialActionHelperSNC'
};

Sys ID

0ecd581e43a02110cd5b8beeaab8f2e2

Offical Documentation

Official Docs: