- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2023 05:21 AM
Hi All,
I need to limit access to a feature to 4 users. I want create a property and add the allowed userID or user sys id to the property, then check if user is in the property and set var isAccessAllowed to true. I need to do this in a service portal widget.
Has anyone done something similar and can share some code ideas or code snippets?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2023 05:36 AM - edited 10-06-2023 05:41 AM
Hi @Eli7
Can you try logic something like below :
var currentUser = gs.getUserID();
var allowedUser = gs.getProperty('name_of_property').split(','); //will get array
var isAllowed = allowedUser.includes(currentUser); ///check if user is in array
if (isAllowed) {
// to do
} else {
//to do
}
I'm not an expert in portal but just a thought...!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2023 06:20 AM
Hi @Eli7,
What exactly do you need to do in the widget?
Should the widget not be displayed at all, if a user is not one of the 4? Or is it some functionality within the widget?
If it shouldn't be show I would use the script @Vishal Birajdar provided (slightly modified, as below) in a user criteria, and attach that to the widget. (Maybe even without system property, but just choose the users in the user criteria)
var currentUser = gs.getUserID();
var allowedUser = gs.getProperty('name_of_property').split(','); //will get array
var isAllowed = allowedUser.includes(currentUser); ///check if user is in array
if (isAllowed) {
return true;
} else {
return false;
}
Otherwise the script could set a data variable in the widget which you can leverage in the HTML to show/hide elements as needed.
var currentUser = gs.getUserID();
var allowedUser = gs.getProperty('name_of_property').split(','); //will get array
var isAllowed = allowedUser.includes(currentUser); ///check if user is in array
if (isAllowed) {
data.allowed = true;
} else {
data.allowed = false;
}
<div ng-if="data.allowed">
content
</div>
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2023 06:20 AM
Hi @Eli7,
What exactly do you need to do in the widget?
Should the widget not be displayed at all, if a user is not one of the 4? Or is it some functionality within the widget?
If it shouldn't be show I would use the script @Vishal Birajdar provided (slightly modified, as below) in a user criteria, and attach that to the widget. (Maybe even without system property, but just choose the users in the user criteria)
var currentUser = gs.getUserID();
var allowedUser = gs.getProperty('name_of_property').split(','); //will get array
var isAllowed = allowedUser.includes(currentUser); ///check if user is in array
if (isAllowed) {
return true;
} else {
return false;
}
Otherwise the script could set a data variable in the widget which you can leverage in the HTML to show/hide elements as needed.
var currentUser = gs.getUserID();
var allowedUser = gs.getProperty('name_of_property').split(','); //will get array
var isAllowed = allowedUser.includes(currentUser); ///check if user is in array
if (isAllowed) {
data.allowed = true;
} else {
data.allowed = false;
}
<div ng-if="data.allowed">
content
</div>
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2023 07:07 AM
Thanks a lot Peter!