Skip to content

Commit a10a9ba

Browse files
authored
Create checkUserCriteria.js
1 parent 690dba9 commit a10a9ba

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Script Include to check if a user has access to a catalog item based on user criteria.
3+
*
4+
* @param {string} item - The sys_id of the catalog item to check.
5+
* @param {boolean} [adminOverride] - Optional. Specifies whether an admin override should apply.
6+
* @param {string} [userToCheck] - Optional. The sys_id of the user to check access for. Defaults to the current user if not provided.
7+
*
8+
* @returns {boolean} - Returns true if the user has access to the catalog item, false otherwise.
9+
*/
10+
11+
var CheckCriteria = Class.create();
12+
CheckCriteria.prototype = {
13+
initialize: function() {},
14+
itemCriteria: function(item, adminOverride, userToCheck) {
15+
16+
// Determine the user object and user ID, using provided userToCheck or defaulting to the current user
17+
var userObj = !gs.nil(userToCheck) ? gs.getUser().getUserByID(userToCheck) : gs.getUser();
18+
var userId = !gs.nil(userToCheck) ? userToCheck : gs.getUserID();
19+
20+
// Admin override: if the user is an admin and adminOverride is not set to false, return true
21+
if(adminOverride != false && userObj.hasRole('admin')) {
22+
return true;
23+
}
24+
25+
// Error handling: Ensure the item parameter is provided and not null
26+
if (gs.nil(item)) {
27+
gs.error('CheckCriteria().itemCriteria() failed: item parameter is missing or null, item: ' + item);
28+
return;
29+
}
30+
31+
// Get the "Available for" and "Not Available for" user criteria for the catalog item
32+
var availableForUC = this.getUserCritria(item, true);
33+
var notAvailableForUC = this.getUserCritria(item, false);
34+
35+
// Check if the user matches the "Not Available for" criteria
36+
if(sn_uc.UserCriteriaLoader.userMatches(userId, notAvailableForUC)) {
37+
return false; // User does not have access
38+
}
39+
// Check if the user matches the "Available for" criteria
40+
else if (sn_uc.UserCriteriaLoader.userMatches(userId, availableForUC)) {
41+
return true; // User has access
42+
}
43+
// If user doesn't match any criteria, they have no access
44+
else {
45+
return false;
46+
}
47+
},
48+
49+
/**
50+
* Retrieves the user criteria for a catalog item.
51+
*
52+
* @param {string} item - The sys_id of the catalog item.
53+
* @param {boolean} available - If true, fetch the "Available for" criteria. If false, fetch the "Not Available for" criteria.
54+
*
55+
* @returns {Array<string>} - An array of user criteria sys_ids for the catalog item.
56+
*/
57+
getUserCritria: function(item, available) {
58+
// Error handling: Ensure the item parameter is provided and not null
59+
if (gs.nil(item)) {
60+
gs.error('CheckCriteria().getUserCritria() failed: item parameter is missing or null, item: ' + item);
61+
return;
62+
}
63+
64+
var returnArr = [];
65+
// Determine the correct table based on whether we're checking "Available for" or "Not Available for"
66+
var tableToCheck = available == false ? 'sc_cat_item_user_criteria_no_mtom' : 'sc_cat_item_user_criteria_mtom';
67+
68+
// Query the user criteria table for the catalog item
69+
var ucCheckGr = new GlideRecord(tableToCheck);
70+
ucCheckGr.addQuery('sc_cat_item', item);
71+
ucCheckGr.query();
72+
73+
// Loop through the results and collect the user criteria
74+
while (ucCheckGr.next()) {
75+
returnArr.push(ucCheckGr.getValue('user_criteria'));
76+
}
77+
78+
// Return the array of user criteria sys_ids
79+
return returnArr;
80+
},
81+
82+
type: 'CheckCriteria'
83+
};

0 commit comments

Comments
 (0)