Skip to content

Commit b45fd85

Browse files
authored
Item based user criteria (#1074)
* Create checkUserCriteria.js * Create README.md * Refactoring checkUserCriteria.js
1 parent d2ec8cd commit b45fd85

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# CheckCriteria Script Include
2+
3+
This script include is used to check if a user has access to a specific catalog item based on "Available for" and "Not Available for" user criteria in ServiceNow. It supports admin overrides and custom user checks.
4+
5+
6+
## Usage
7+
8+
The `CheckCriteria` script include provides a method `itemCriteria` which checks if a user meets the criteria to access a catalog item.
9+
10+
### Syntax
11+
12+
```javascript
13+
var check = new CheckCriteria();
14+
var result = check.itemCriteria(item, adminOverride, userToCheck);
15+
```
16+
17+
### Parameters
18+
19+
1. **`item`** (string):
20+
- The sys_id of the catalog item you want to check access for.
21+
- This parameter is **required**.
22+
23+
2. **`adminOverride`** (boolean, optional):
24+
- Specifies whether admin override should be taken into account.
25+
- If `true`, users with the `admin` role will always have access to the item, even if they do not match the user criteria.
26+
- Defaults to `false` if not provided.
27+
28+
3. **`userToCheck`** (string, optional):
29+
- The user ID of the user you want to check access for.
30+
- If not provided, the currently logged-in user (`gs.getUser()`) will be used by default.
31+
32+
### Return
33+
34+
- **`true`** if the user has access to the catalog item.
35+
- **`false`** if the user does not have access to the catalog item.
36+
37+
### Example
38+
39+
```javascript
40+
var check = new CheckCriteria();
41+
42+
// Example 1: Check if the current user has access to the catalog item
43+
var hasAccess = check.itemCriteria('12345abcdef'); // '12345abcdef' is the sys_id of the catalog item
44+
45+
// Example 2: Check access for a specific user with an admin override
46+
var hasAccess = check.itemCriteria('12345abcdef', true, 'abc123user'); // 'abc123user' is the user ID of the user
47+
```
48+
49+
In the first example, the script checks if the current user can access the specified catalog item. In the second example, it checks if the specified user can access the item and allows admin override.
50+
51+
## Error Handling
52+
53+
- If the `item` parameter is not provided or is `null`, an error message will be logged in the system logs.
54+
- The script also logs errors when unable to retrieve user criteria for the catalog item.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var CheckCriteria = Class.create();
2+
CheckCriteria.prototype = {
3+
initialize: function() {},
4+
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;
17+
18+
// Early exit if item is nil or missing
19+
if (gs.nil(item)) {
20+
gs.error('CheckCriteria().itemCriteria() failed: item parameter is missing or null, item: ' + item);
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;
31+
}
32+
33+
// Fetch "Available for" and "Not Available for" user criteria
34+
var availableForUC = this.getUserCritria(item, true);
35+
var notAvailableForUC = this.getUserCritria(item, false);
36+
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+
42+
// Check if the user matches the "Available for" criteria
43+
return sn_uc.UserCriteriaLoader.userMatches(userId, availableForUC);
44+
},
45+
46+
/**
47+
* Retrieves the user criteria for a catalog item.
48+
*
49+
* @param {string} item - The sys_id of the catalog item.
50+
* @param {boolean} available - If true, fetch the "Available for" criteria. If false, fetch the "Not Available for" criteria.
51+
*
52+
* @returns {Array<string>} - An array of user criteria sys_ids for the catalog item.
53+
*/
54+
getUserCritria: function(item, available) {
55+
// Early exit if item is nil or missing
56+
if (gs.nil(item)) {
57+
gs.error('CheckCriteria().getUserCritria() failed: item parameter is missing or null, item: ' + item);
58+
return [];
59+
}
60+
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
69+
var ucCheckGr = new GlideRecord(tableToCheck);
70+
ucCheckGr.addQuery('sc_cat_item', item);
71+
ucCheckGr.query();
72+
73+
// Store user criteria sys_ids in an array
74+
var returnArr = [];
75+
while (ucCheckGr.next()) {
76+
returnArr.push(ucCheckGr.getValue('user_criteria'));
77+
}
78+
79+
return returnArr;
80+
},
81+
82+
type: 'CheckCriteria'
83+
};

0 commit comments

Comments
 (0)