How to check if a user id is in a property and set a var to true

Eli7
Tera Expert

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! 

2 ACCEPTED SOLUTIONS

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...!!

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

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.

View solution in original post

6 REPLIES 6

Peter Bodelier
Giga Sage

Hi @Eli7,

 

Why not simply use a role for this?


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter,

I thought about that also but would have to create a new group and add only those user and not so simple for this customer. Also this is only a temporary set up during POC trial when the 4 users are satisfied it will be opened for all users.

Any idea how I should code this. I am thinking onload I get the current user but then not sure how to check this in the property.

Thanks!

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...!!

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Thanks Vishal, i had to change .includes to use indexof and it works beautfully. Appreciate your help a ton!!