|
| 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