user criteria - restrict access to catalog item based on groups that start with cis

gracjan
Tera Guru

Here is the code that I am using but it is not working. What is wrong with it?

 

 

// Check if the current user belongs to a group starting with "CIS"
(function() {
// Get the current user's groups
var userGroups = gs.getUser().getMyGroups();

// Loop through the groups to check if any starts with "CIS"
for (var i = 0; i < userGroups.size(); i++) {
var groupName = userGroups.get(i).getName();
if (groupName.startsWith("CIS")) {
// User belongs to a group starting with "CIS", allow access
return true;
}
}

// User doesn't belong to any group starting with "CIS", deny access
return false;
})();

The faulty result for this script is that both users whom have the group and whom not have the group, have no access to the catalog item.

 

Please help!

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @gracjan 

Let's give my adjustment a try.

var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', user_id);
grMember.addQuery('group.name', 'STARTSWITH', 'CIS');
grMember.setLimit(1);
grMember.query();
answer = grMember.hasNext();

 

Cheers,

Tai Vu

View solution in original post

3 REPLIES 3

shubhamdubey
Mega Sage

@gracjan 

Good day !!

 

If you are writting the script in user criteria then gs.getUser() not recommended.user criteria.PNG

Tai Vu
Kilo Patron
Kilo Patron

Hi @gracjan 

Let's give my adjustment a try.

var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', user_id);
grMember.addQuery('group.name', 'STARTSWITH', 'CIS');
grMember.setLimit(1);
grMember.query();
answer = grMember.hasNext();

 

Cheers,

Tai Vu

Brilliant. Thank you!