Help with Script Include to get Roles Based on Group and User

patricial
Tera Contributor

Ok I need some help please!

I am jammed up with getting this to work.

I have a catItem that adds a user to Group

I need to first check what roles the group has

Check what roles the user has

If the group has itil and the user has itil = success

If the group has appover_user and user has approver_user = success

If group has itil and user does not have itil = fail

If group has approver_user and user does not have approver user = fail

If group does not have either itil or approver_user = success (regardless of what roles user has)

Here is my script Include (My client script follows) in my client script I need to return a message based on which role the user failed on

So if the user does not have ITIL (User does not have ITIL message)

If the user does not have approver_user (User does not have approver_user message)

/******* script include *********/

var getRolesAjax = Class.create();

getRolesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getRole: function(){

gs.log('>>>DEBUG: returnGroup Role() started...');

var list = [];

var grp = this.getParameter('sysparm_group');

gs.log(grp,'shipra0');

var grGroup = new GlideRecord('sys_group_has_role');

grGroup.addQuery('group', grp);

grGroup.addQuery('role.name', 'itil');//itil

grGroup.query();

while(!grGroup.next()){

//var rol = grGroup.role.getDisplayValue(); //group itil

//return 'success';

//continue;

var gr2Group = new GlideRecord('sys_group_has_role');

gr2Group.addQuery('group', grp);

gr2Group.addQuery('role.name', 'approver_user');//approver user

gr2Group.query();

while(!gr2Group.next()){

//var rol2 = gr2Group.role.getDisplayValue(); //group approver_user

//gs.log('>>>TRICIA Query GROUP APPROVER:' +urol);

return 'success';

//continue;

}

var rol = grGroup.role.getDisplayValue(); //group itil

var rol2 = gr2Group.role.getDisplayValue(); //group approver_user

var usr = this.getParameter('sysparm_requestedby');

var grUser = new GlideRecord('sys_user_has_role');

grUser.addQuery('user', usr);

grUser.addQuery('role.name', rol); //itil from group

grUser.query();

while (grUser.next()) {

var urol = grUser.role.getDisplayValue(); //itil role

var usr2 = this.getParameter('sysparm_requestedby');

var gr2User = new GlideRecord('sys_user_has_role');

gr2User.addQuery('user', usr);

gr2User.addQuery('role.name', rol2); //approver_user role

gr2User.query();

while (gr2User.next()) {

var urol2 = gr2User.role.getDisplayValue(); //approver user

gs.log('>>>TRICIA Query USER ITIL:'+rol + " " +urol);

if(rol == urol || rol == ''){

gs.log('>>>TRICIA Query USER ITIL:'+rol + " " +urol);

continue;

//return 'success';

}

if(rol2 == urol2 || rol2 == ''){

gs.log('>>>TRICIA Query for USER APPROVER:'+rol2 + " " +urol2);

return 'success';

}

}

}

}

},

type: 'getRolesAjax'

});

/*******Client Script*********/

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue === '') {

return;

}

g_form.setValue('no_license', 'false');

g_form.setValue('request_license', 'No');

var grp = g_form.getValue('group_name');

var usr = g_form.getValue('requested_by');

alert('Calling getRole... ' + grp);

var ga = new GlideAjax('getRolesAjax');

ga.addParam('sysparm_name','getRole');

ga.addParam('sysparm_group', grp);

ga.addParam('sysparm_requestedby', usr);

ga.getXML(AsyncCall);

}

function AsyncCall(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer == 'success') {

g_form.setValue('no_license', 'false');

g_form.setValue('request_license', 'Yes');

alert(answer);

}

else{

g_form.addInfoMessage('ITIL Role is required for Group Access.');

g_form.setValue('no_license', 'true');

    }

}