- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2022 10:51 PM
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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 09:37 PM
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 12:29 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 04:19 PM
Thank you for your response.
I will try it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 12:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 03:43 AM
View Rule runs on the server side,so i think the script can work well。
Can post your whole script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 05:04 PM
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
Result
View cannot be selected.