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
1
var CheckCriteria = Class . create ( ) ;
12
2
CheckCriteria . prototype = {
13
3
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
4
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 ;
24
17
25
- // Error handling: Ensure the item parameter is provided and not null
18
+ // Early exit if item is nil or missing
26
19
if ( gs . nil ( item ) ) {
27
20
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 ;
29
31
}
30
32
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
32
34
var availableForUC = this . getUserCritria ( item , true ) ;
33
35
var notAvailableForUC = this . getUserCritria ( item , false ) ;
34
36
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
+
39
42
// 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 ) ;
47
44
} ,
48
45
49
46
/**
@@ -55,27 +52,30 @@ CheckCriteria.prototype = {
55
52
* @returns {Array<string> } - An array of user criteria sys_ids for the catalog item.
56
53
*/
57
54
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
59
56
if ( gs . nil ( item ) ) {
60
57
gs . error ( 'CheckCriteria().getUserCritria() failed: item parameter is missing or null, item: ' + item ) ;
61
- return ;
58
+ return [ ] ;
62
59
}
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
60
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
69
69
var ucCheckGr = new GlideRecord ( tableToCheck ) ;
70
70
ucCheckGr . addQuery ( 'sc_cat_item' , item ) ;
71
71
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 = [ ] ;
74
75
while ( ucCheckGr . next ( ) ) {
75
76
returnArr . push ( ucCheckGr . getValue ( 'user_criteria' ) ) ;
76
77
}
77
-
78
- // Return the array of user criteria sys_ids
78
+
79
79
return returnArr ;
80
80
} ,
81
81
0 commit comments