How can a person with a specific role access the View? (View Rule not working)

tk37
Tera Contributor

I would like to allow a person holding a specific role to access the following View.

(I want to allow role holders to access other people's ess views.)
・Table : sys_user
・View : ess
・Role : u_itsm_counter

 

I created the following View Rule Script, but it did not work.
I would appreciate anyone's help.

 

・Script

(function overrideView(view, is_list) {
answer = view;
if (gs.hasRole('u_itsm_counter')) {
answer = "ess";
}
})(view, is_list);

1 ACCEPTED SOLUTION

@tk37 

Finally i found the reason.

If a user selected a view manually. 

This selection will be the first priority.

 

Set the test user's view to "default view" and test it !

 

Please mark my answer as correct and helpful based on Impact.

View solution in original post

15 REPLIES 15

@tk37 

I think i found the reason!

If the user is a  admin ,  hasrole() function will always retun true(even not existed role name )!

 

use the below function to check if the user has a specific  role .

(I have not debuged this script , so if it goes wrong , debug and modify it please.)

 

 

function checkrole(role,user_id){   
            var rol = new GlideRecord('sys_user_role');
               rol.addQuery('name', role);
               rol.query();
               if (rol.next()) {
                       var hasRole = new GlideRecord('sys_user_has_role');
                       hasRole.addQuery('user', user_id);
                       hasRole.addQuery('role', rol.sys_id);
                       hasRole.query();
                       if (hasRole.next()) {
                               return true;
                       } else {
                               return false;
                       }
               }
               return false;
}

 

 

 

Please mark my answer as correct and helpful based on Impact.

tk37
Tera Contributor

Thank you for your response.
I will try it.

tk37
Tera Contributor

I tried the Script you gave me.
It worked with the Background Script, but not with the View Rule.
There may be a cause other than the Script.

@tk37 

View Rule runs on the server side,so i think the script can work well。

Can post your  whole script ?

Please mark my answer as correct and helpful based on Impact.

tk37
Tera Contributor

@newhand 

Thank you for your response.

The scripts that were implemented are as follows.

(function overrideView(view, is_list) {
    var currentUser = gs.getUserID();
	if(!checkrole('u_itsm_counter',currentUser))
	{return;}
	answer = "ess";
	
	function checkrole(role,user_id){   
            var rol = new GlideRecord('sys_user_role');
               rol.addQuery('name', role);
               rol.query();
               if (rol.next()) {
                       var hasRole = new GlideRecord('sys_user_has_role');
                       hasRole.addQuery('user', user_id);
                       hasRole.addQuery('role', rol.sys_id);
                       hasRole.query();
                       if (hasRole.next()) {
                               return true;
                       } else {
                               return false;
                       }
               }
               return false;
	}
})(view, is_list);

View Rule

tk37_0-1671584552567.png

Result

View cannot be selected.

tk37_1-1671584581417.png