Skip to content

Commit 5f924c4

Browse files
authored
Refactoring checkUserCriteria.js
1 parent 90d19e2 commit 5f924c4

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

Script Includes/Check User Criteria for Catalog Item/checkUserCriteria.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
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-
111
var CheckCriteria = Class.create();
122
CheckCriteria.prototype = {
133
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();
194

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-
}
5+
/**
6+
* Checks if the user meets the criteria to access the catalog item.
7+
*
8+
* @param {string} item - The sys_id of the catalog item.
9+
* @param {boolean} [adminOverride] - Optional. Whether admin role should override the criteria check. Defaults to false if not provided.
10+
* @param {string} [userToCheck] - Optional. The user ID of the user whose access is being checked. Defaults to the current user if not specified.
11+
*
12+
* @returns {boolean} - True if the user has access to the catalog item, otherwise false.
13+
*/
14+
itemCriteria: function(item, adminOverride, userToCheck) {
15+
// Set default value for adminOverride if not provided
16+
adminOverride = (typeof adminOverride !== 'undefined') ? adminOverride : false;
2417

25-
// Error handling: Ensure the item parameter is provided and not null
18+
// Early exit if item is nil or missing
2619
if (gs.nil(item)) {
2720
gs.error('CheckCriteria().itemCriteria() failed: item parameter is missing or null, item: ' + item);
28-
return;
21+
return false;
22+
}
23+
24+
// Get the user object and user ID, defaulting to the current user if userToCheck is not provided
25+
var userObj = !gs.nil(userToCheck) ? gs.getUser().getUserByID(userToCheck) : gs.getUser();
26+
var userId = userObj.getID();
27+
28+
// Admin override: if the user is an admin and adminOverride is true, return true
29+
if (adminOverride && userObj.hasRole('admin')) {
30+
return true;
2931
}
3032

31-
// Get the "Available for" and "Not Available for" user criteria for the catalog item
33+
// Fetch "Available for" and "Not Available for" user criteria
3234
var availableForUC = this.getUserCritria(item, true);
3335
var notAvailableForUC = this.getUserCritria(item, false);
3436

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-
}
37+
// Check if the user matches the "Not Available for" criteria first
38+
if (sn_uc.UserCriteriaLoader.userMatches(userId, notAvailableForUC)) {
39+
return false;
40+
}
41+
3942
// 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-
}
43+
return sn_uc.UserCriteriaLoader.userMatches(userId, availableForUC);
4744
},
4845

4946
/**
@@ -55,27 +52,30 @@ CheckCriteria.prototype = {
5552
* @returns {Array<string>} - An array of user criteria sys_ids for the catalog item.
5653
*/
5754
getUserCritria: function(item, available) {
58-
// Error handling: Ensure the item parameter is provided and not null
55+
// Early exit if item is nil or missing
5956
if (gs.nil(item)) {
6057
gs.error('CheckCriteria().getUserCritria() failed: item parameter is missing or null, item: ' + item);
61-
return;
58+
return [];
6259
}
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';
6760

68-
// Query the user criteria table for the catalog item
61+
// Define table name constants
62+
var TABLE_AVAILABLE = 'sc_cat_item_user_criteria_mtom';
63+
var TABLE_NOT_AVAILABLE = 'sc_cat_item_user_criteria_no_mtom';
64+
65+
// Select appropriate table based on availability flag
66+
var tableToCheck = available ? TABLE_AVAILABLE : TABLE_NOT_AVAILABLE;
67+
68+
// Query user criteria from the appropriate table
6969
var ucCheckGr = new GlideRecord(tableToCheck);
7070
ucCheckGr.addQuery('sc_cat_item', item);
7171
ucCheckGr.query();
72-
73-
// Loop through the results and collect the user criteria
72+
73+
// Store user criteria sys_ids in an array
74+
var returnArr = [];
7475
while (ucCheckGr.next()) {
7576
returnArr.push(ucCheckGr.getValue('user_criteria'));
7677
}
77-
78-
// Return the array of user criteria sys_ids
78+
7979
return returnArr;
8080
},
8181

0 commit comments

Comments
 (0)