Add condition on UI action- check if logged in user is part of a group

Varun Sai
Tera Contributor

I have a requirement to make the UI action visible only when logged in users group is part of certain groups. I created a system property which stores the sys_ids of the groups and want to call this system property in a script Include which I can add on a UI Action.

Note: The reason I am not adding the condition on UI action is it has a character limit of 40 char on UI action condition and I have other conditions as well along with group visibility it was not fitting all the conditions here.

 

Script Include:

getCheckCondition: function(curRec) {
        var userGrp = gs.getProperty('sn_user.groups');
        answerGrp = 'sys_idIN'+userGrp;
        var grIncRec = new GlideRecord("incident");
        grIncRec.addQuery('sys_id', curRec.sys_id);
        grIncRec.query();  
        if (grIncRec.next()) {
            if ((grIncRec.state != '7') && (grIncRec.correlation_id == '') && (grIncRec.correlation_display == '') && (grIncRec.u_transfer_id == false) && gs.getUser().isMemberOf(answer)) {
                return true;
            }
        }
    },
 
In the UI action I just want to call 'getCheckCondition' function from the Script Include to on the UI Action.
For the above script it's not working as expected for the groups for the logged in user. Would really appreciate any pointers or where I am going wrong.

 

 

1 ACCEPTED SOLUTION

Whereas that may "work", it is far from being efficient.  You are looping through and making a query for each Group sys_id.   A better script would be something like this:

(function() {
  var user = gs.getUserID();
  var ids = gs.getProperty("whatever_name_it _is");
  
  var ga = new GlideAggregate("sys_user_grmember");
  ga.addEncodedQuery("group.sys_idIN" + ids + "^user=" + user);
  ga.addAggregate('COUNT');
  ga.query();
  while(ga.next()){
    gs.print(ga.getAggregate("COUNT"));
    gs.print(ga.getAggregate("COUNT") > 0);  //is the User in any of the Groups?
  }  
})();

 

One query to get the number of records returned.  The result would be:

JimCoyne_0-1712459195838.png

 

But again, would not do it this way.  Look at my response to the original post on what I believe is a much more elegant solution.

View solution in original post

8 REPLIES 8

Rajesh_Bhise
Tera Guru

Hello @Varun Sai ,

 

I did the same except incident logic inclusion in the script and got successful results as below:

 

1. Created system property with group's syd_id's. 

 

Rajesh_T_0-1712451905317.png

2. Created background script to check if current logged in user is part of the group which is mentioned in the system property. I got result as "True".

 

Rajesh_T_1-1712452028133.png

 

You can use this script in your script include and get results successful.

 

Please DO NOT forget to mark this solution as correct and Helpful if it satisfied your query. This enhances the interest to provide solutions on community.

 

Thank You,

Rajesh

 

 

Whereas that may "work", it is far from being efficient.  You are looping through and making a query for each Group sys_id.   A better script would be something like this:

(function() {
  var user = gs.getUserID();
  var ids = gs.getProperty("whatever_name_it _is");
  
  var ga = new GlideAggregate("sys_user_grmember");
  ga.addEncodedQuery("group.sys_idIN" + ids + "^user=" + user);
  ga.addAggregate('COUNT');
  ga.query();
  while(ga.next()){
    gs.print(ga.getAggregate("COUNT"));
    gs.print(ga.getAggregate("COUNT") > 0);  //is the User in any of the Groups?
  }  
})();

 

One query to get the number of records returned.  The result would be:

JimCoyne_0-1712459195838.png

 

But again, would not do it this way.  Look at my response to the original post on what I believe is a much more elegant solution.

Hi @Jim Coyne ,

I was able to get the results successfully in background script. But, when I try to implement in the script include with other queries I am not getting the results. I may be going wrong somewhere. 

 

getCheckCondition: function(curRec){
  var user = gs.getUserID();
  var ids = gs.getProperty("sn_user.groups");
  var ga = new GlideAggregate("sys_user_grmember");
  ga.addEncodedQuery("group.sys_idIN" + ids + "^user=" + user);
  ga.addAggregate('COUNT');
  ga.query();
  if(ga.next()){
  var grIncRec = new GlideRecord("incident");
        grIncRec.addQuery('sys_id', curRec.sys_id);
        grIncRec.query();  
        if (grIncRec.next()) {
            if ((grIncRec.state != '7') && (grIncRec.correlation_id == '') && (grIncRec.correlation_display == '') && (grIncRec.u_transfer_id == false)) {
                return true;
            }
        }
    }, })();

 

Did you see my other post describing how to do it with a Role instead?  That is a much better way of doing it:

  • Simple: no code or System Properties to maintain
  • easily maintained by simply add/removing the Role to/from Groups

I was just trying to show that the script example was not a good one.  I do NOT recommend going this route.