Validate user against a user criteria

larstange
Mega Sage

Have you every wanted to take advantage of user criterias in other places of servicenow than is available OOTB? - eg. to control who can access an element in the CMS or a content of a widget in Service Portal?

Here on Share is a script include which allows exactly that.

Use by calling:

var ucu = new UserCriteriaUtil('USER CRITERIA SYS_ID');

var answer = ucu.check();

It will return true if the current user complies to the user criteria.

All condition types (including scripts) are supported - except the feature where you can add custom fields (Extending User Criteria)

Credits go out to my old colleague jespersandberg-how who coded the function originally based on an idea of mine.

8 REPLIES 8

sarfraz3
Mega Expert

Hi Lars,


I have built a custom portal in which I want to use the same. I want to show the catalog items with reference to what is defined in the user criteria in jelly.
Can you please help me with the approach .



Thanks in advance


larstange
Mega Sage

Hi



I would need some more details form you. I have never worked with the CMS - I skipped that one and went with the Service Portal instead....


Hi Lars,



IN dynamic page I am querying the following inside g:evaluate tag


1.table:sc_cat_item


2.active:true


3.category:parent(getting fro url using Rp.getParameter)



Now by this I am able to show all the items of certain category to users. But how do I link user criteria with this query so that user can see available items for them mentioned in user criteria.



So to sum it up HOW do we use that in g:evaluate and if we cannot use it in g evaluate how can we implement that. from some other thread(UserCriteria - Server side script evaluation ) I got this


<j:if test="${GlideappCatalogItem.get(req.sys_id).canView() == true}">


all html and jexl inside


</j:if>



But this is not working...if I use this I get nothing in the view. which means it is not going inside the loop. Please help


Hi



In your g:evaluate create a function to check agains a user criteria



function checkUC(uc_id) {


        var ucu = new UserCriteriaUtil(uc_id);


        return ucu.check();


}


Then loop through all items in the category and query to get all the user criterias related to each catalog item in the category which you are displaying (assuming you are only using "available for"



var canView = false;



var avail = new GlideRecord('sc_cat_item_user_criteria_mtom');


avail.sc_cat_item = "Cat ITem ID";


avail.query();



while (avail.next()) {


        if (checkUC(avail.user_criteria) {


                  canView = true;


                  break; //We don't need to check more user criterias


        }


}



If the "canView" attribute is true, you display the catalog item