Determining if a user can read an article

Michael Todd
Tera Contributor

Hi,

I have a requirement to determine if a user has (read) access to a knowledge article via a email notification script. I am assuming there is an OTTB script include for this but struggling to find anything.

 

Appreciate any suggestions.

Thanks.

1 ACCEPTED SOLUTION

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

I'm afraid none of the other answers give you what you need, as methods like .canRead() automatically take the logged in user's sys_id to check access, and they do not accept any other parameter to check a different user. Furthermore, it is not recommended to call SNC classes, like KBKnowledgeSNC() directly. These classes always have an extended sibling which should be used, in this case it is KBKnowledge(). But as I mentioned, they only take the logged in user's ID, and therefore you should first impersonate your target user. So you will have to do something like this:

/* 
*Check if the logged in user has access to the provided knowledge article
* @Param {string} - sys_id of the knowledge article to check
* @return {boolean} - true if the user has access to the article, false if not
*/

function kbAccess(kba) {
  var kbGr = new GlideRecord('kb_knowledge');
  kbGr.get(kba);
  return (new KBKnowledge().canRead(kbGr));
}

var userToCheck = ''; // Add the sys_id of the user you want to check here
var originalUser = gs.getSession().impersonate(userToCheck ); // impersonate target user
var hasAccessToKBA = kbAccess(kba);
 gs.getSession().impersonate(originalUser ); // impersonate back to the original user

 

View solution in original post

7 REPLIES 7

Kirby R
Kilo Sage

Hi @Michael Todd ,

 

You can try to use UserCriteriaLoader. Its not part of SN official documentation though.

 

For reference:

UserCriteriaLoader - API to Evaluate User Criteria - ServiceNow Community

 

Please mark correct if this helped your issue.

Swapna Abburi
Mega Sage
Mega Sage

@Michael Todd 

You can use "KBKnowledgeSNC" script include.

new KBKnowledgeSNC().canRead()

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

I'm afraid none of the other answers give you what you need, as methods like .canRead() automatically take the logged in user's sys_id to check access, and they do not accept any other parameter to check a different user. Furthermore, it is not recommended to call SNC classes, like KBKnowledgeSNC() directly. These classes always have an extended sibling which should be used, in this case it is KBKnowledge(). But as I mentioned, they only take the logged in user's ID, and therefore you should first impersonate your target user. So you will have to do something like this:

/* 
*Check if the logged in user has access to the provided knowledge article
* @Param {string} - sys_id of the knowledge article to check
* @return {boolean} - true if the user has access to the article, false if not
*/

function kbAccess(kba) {
  var kbGr = new GlideRecord('kb_knowledge');
  kbGr.get(kba);
  return (new KBKnowledge().canRead(kbGr));
}

var userToCheck = ''; // Add the sys_id of the user you want to check here
var originalUser = gs.getSession().impersonate(userToCheck ); // impersonate target user
var hasAccessToKBA = kbAccess(kba);
 gs.getSession().impersonate(originalUser ); // impersonate back to the original user

 

Many thanks Laszlo. I was indeed thinking about how to run this script for a given user, rather than the logged in user. Will test out the proposed solution and update this post with findings.