Retrieving a list in a system property from a business rule

Adam C1
Tera Contributor

We are using isMemberOf in a business rule to validate a user is in a group before proceeding.

Initially this had just two groups, but will be expanding to 8+ groups.

if (gs.getUser().isMemberOf('Group1)||gs.getUser().isMemberOf('Group2')||gs.getUser().isMemberOf('Group3')||gs.getUser().isMemberOf('Group4')){

Instead of continuing to add additional "or" conditions  we would like to create system property that would hold either a comma separated list of group names or the sysid of the groups if needed and reference this for the initial check.

Assuming we will be using .getProperty to save the list to a var then using .spilt(',');

What would be the syntax to loop through the list for the .isMemberOf check

 

 

 

 

 

4 REPLIES 4

-O-
Kilo Patron
Kilo Patron

Why not create a role and grant that role to those groups? Roles are way easier to use all over the place, perform better and can be more easily managed.

Adam C1
Tera Contributor

Agreed that roles would be much easier to manage.  In this case there is actually a mix of multiple roles involved and not all the users with the same role should have the BR applied to them.  We would need to create an entirely new role just for this specific initial check alone.

Also the script has additional sections that are performing a similar checks against sub sets of other groups, so going the property route would allow us to extend that into those sections as well, as opposed to creating 2-4 new roles.

Something like this should do it:

gs.debug(isMemberOfGroupsInProperty('<property name>')(gs.getUser()));

function isMemberOfGroupsInProperty (propertyName) {
	return function (user) {
		return String(gs.getProperty(propertyName, ''))
			// Split the property value on comma, semicolon or new line trimming elements at the same time
			.split(/\s*[,;\n]+\s*/g)
			// Eliminate empty elements - also insures that an empty list results in a 0 length array (instead of an array with one element - an empty string)
			.filter(retainNotEmptyString())
			// Check memberhsip of a given user in each group, actually upto the 1st positive finding
			.some(isOneOfTheMembersUser(user));
	};
}

function retainNotEmptyString () {
	return function (group) {
		gs.debug('Vetting group moniker [' + group + ']');
		return group != '';
	};
}

function isOneOfTheMembersUser (user) {
	return function (group) {
		gs.debug('Checking memberhsip in [' + group + ']');
		return user.isMemberOf(group);
	};
}

Or in case you must use the SI "pattern":

var SI = Class.create();

SI.LOG_PROPERTY = '<log property name>';

SI.prototype = {

	initialize: function SI$initialize () {
		this._log = new global.GSLog(global.SI.LOG_PROPERTY, this.type);
	},

	isMemberOfGroupsInProperty: function SI$isMemberOfGroupsInProperty (propertyName) {
		var context = this;

		return function (user) {
			return String(gs.getProperty(propertyName, ''))
				.split(/\s*[,;\n]+\s*/g)
				.filter(context._retainNotEmptyString())
				.some(context._isOneOfTheMembersUser(user));
		};
	},

	type: 'SI',

	_retainNotEmptyString: function SI$_retainNotEmptyString () {
		var context = this;

		return function (group) {
			context._log.debug('Vetting group moniker [' + group + ']');
			return group != '';
		};
	},

	_isOneOfTheMembersUser: function SI$_isOneOfTheMembersUser (user) {
		var context = this;

		return function (group) {
			context._log.debug('Checking memberhsip in [' + group + ']');
			return user.isMemberOf(group);
		};
	},

};

Of course you would need to give it a proper name, in place of SI.

-O-
Kilo Patron
Kilo Patron

Forgot the "test" Scripts - Background instruction for the SI based solution:

gs.debug(new global.SI().isMemberOfGroupsInProperty('<name of property>')(gs.getUser()));