The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Roles Comparison in Table "sys_app_module"

madhusudanshett
Kilo Contributor

Hello SNC

I want to display the modules on the page based on the logged in user role.

So I want to compare logged in user role with roles field of module.

var gr = new GlideRecord('sys_app_module');

gr.addQuery('active',true);

gr.addQuery('application.title', 'CONTAINS', 'Self Service');

gr.addQuery('roles',???)   //comparing logged in user role with roles field of module here.

Untitled.png

We have a roles field in "sys_app_module" table of type "User Roles".

User+Roles.png

Using the following code I am capturing the sys_id of the logged in user in an array.

var gr11 = new GlideRecord('sys_user_role');

  gr11.addQuery('name', gs.getUser().getRoles());

  gr11.query();

  gr11;

  var objArray2 = [];

  while(gr11.next()) {

  objArray2.push(gr11.sys_id.toString());

  }

Please help me to compare the roles field of "sys_app_module" table with the logged in user roles.

Thanks in Advance.

1 ACCEPTED SOLUTION

Hi Madhusudan,



I've set this up with a custom table just like yours and got it to work, with columns:


  • u_name (string)
  • u_active (boolean)
  • u_roles (list)


There was an error in your previous code leading to an invalid query string in your "tab.addQuery(...)" statement, I've made the correction in bold below and added a table for testing the output:



(screenshot)Untitled.png






<g:evaluate var="jvar_roles">


      gs.getUser().getRoles().toString();


</g:evaluate>



Roles<br />


<j:forEach items="${jvar_roles.toString().split(',')}" var="jvar_role">


      ${jvar_role}<br />            


</j:forEach>



<g:evaluate var="jvar_qp" object="true">


      var gr9 = new GlideRecord('sys_user_role');


      gr9.addQuery('nameIN' + gs.getUser().getRoles().toString());


      gr9.query();    


    var qry1 = '';


      var sep = '';


      while(gr9.next()) {


              qry1 += sep + 'u_roleLIKE' + gr9.sys_id;


              sep = '^OR';


      }


      //qry1;


      //gs.getUser().getRoles().toString();


      var tab = new GlideRecord('u_other_application_menu'); <!-- Custom table to store application names-->


      tab.addQuery('u_active=true^' + qry1);   <!-- Check box: SN Application Menu Item -->


      tab.query();


      //tab.getRowCount();


      tab;


</g:evaluate>



<br />Found records:${jvar_qp.getRowCount()}


<br />Application Names


<br />


<table cellspacing="50" cellpadding="20" border="1" class="background_transparent">


      <j:while test="${jvar_qp.next()}">


              <tr>


                      <td>${jvar_qp.getValue('u_name')}</td>


              </tr>


      </j:while>


</table>






Please give this a try, it has tested successfully and should work for you assuming you copy/paste it and then adapt the output variable "jvar_qp" to your needs.




Good luck,


-Brian


View solution in original post

18 REPLIES 18

We have a roles field in "sys_app_module" table of type "User Roles".


User+Roles.png



I think type "User Roles" will have Role name and Type "List" will have sys_id of the role.



Thank you Brian.


Hi Brian


My apologies for bothering you again and again.


If you could help me with this issue it will be very helpful as its an urgent issue to be resolved.


Thank you for your help.


Hi Madhusudan,



I've set this up with a custom table just like yours and got it to work, with columns:


  • u_name (string)
  • u_active (boolean)
  • u_roles (list)


There was an error in your previous code leading to an invalid query string in your "tab.addQuery(...)" statement, I've made the correction in bold below and added a table for testing the output:



(screenshot)Untitled.png






<g:evaluate var="jvar_roles">


      gs.getUser().getRoles().toString();


</g:evaluate>



Roles<br />


<j:forEach items="${jvar_roles.toString().split(',')}" var="jvar_role">


      ${jvar_role}<br />            


</j:forEach>



<g:evaluate var="jvar_qp" object="true">


      var gr9 = new GlideRecord('sys_user_role');


      gr9.addQuery('nameIN' + gs.getUser().getRoles().toString());


      gr9.query();    


    var qry1 = '';


      var sep = '';


      while(gr9.next()) {


              qry1 += sep + 'u_roleLIKE' + gr9.sys_id;


              sep = '^OR';


      }


      //qry1;


      //gs.getUser().getRoles().toString();


      var tab = new GlideRecord('u_other_application_menu'); <!-- Custom table to store application names-->


      tab.addQuery('u_active=true^' + qry1);   <!-- Check box: SN Application Menu Item -->


      tab.query();


      //tab.getRowCount();


      tab;


</g:evaluate>



<br />Found records:${jvar_qp.getRowCount()}


<br />Application Names


<br />


<table cellspacing="50" cellpadding="20" border="1" class="background_transparent">


      <j:while test="${jvar_qp.next()}">


              <tr>


                      <td>${jvar_qp.getValue('u_name')}</td>


              </tr>


      </j:while>


</table>






Please give this a try, it has tested successfully and should work for you assuming you copy/paste it and then adapt the output variable "jvar_qp" to your needs.




Good luck,


-Brian


Hi Brian


Thank you for the help.


I am able to display the Applications now. But unable to display modules under the application.


I have another query here:


RolesRestricts module access to the specified roles. If this field is left blank, the module is visible to all users who have access to the application menu.

gr.addQuery('roles', 'IN',   gs.getUser().getRoles().toString()); This query checks role of the module against the logged in user role.


How to add an OR query so that, If role field is left blank , the module is visible to all users who have access to the application menu?


Thank you very much.



Hi Madhusudan,



To include items without roles assigned, I would say to add the following to your query string:



  "^ORroles="




Without the quotes, of course.




Thanks,


-Brian