|
| 1 | +var GenerateSCIMPayload = Class.create(); |
| 2 | +GenerateSCIMPayload.prototype = { |
| 3 | + initialize: function() { |
| 4 | + }, |
| 5 | + |
| 6 | + // Function to generate a random string for new group names |
| 7 | + generateRandomString: function(length) { |
| 8 | + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 9 | + var result = ''; |
| 10 | + for (var i = 0; i < length; i++) { |
| 11 | + result += characters.charAt(Math.floor(Math.random() * characters.length)); |
| 12 | + } |
| 13 | + return "test group " + result; |
| 14 | + }, |
| 15 | + |
| 16 | + // Function to retrieve the groups where the user is a member |
| 17 | + getUserGroups: function(userSysId) { |
| 18 | + var groupNames = []; |
| 19 | + var grGroupMember = new GlideRecord('sys_user_grmember'); // Group Member table |
| 20 | + grGroupMember.addQuery('user', userSysId); // Get groups of the user |
| 21 | + grGroupMember.query(); |
| 22 | + |
| 23 | + while (grGroupMember.next()) { |
| 24 | + var groupName = grGroupMember.group.getDisplayValue(); // Get group name |
| 25 | + groupNames.push({ |
| 26 | + name: groupName |
| 27 | + }); |
| 28 | + } |
| 29 | + return groupNames; |
| 30 | + }, |
| 31 | + |
| 32 | + // Function to retrieve groups where the user is NOT a member |
| 33 | + getGroupsNotMemberOf: function(userSysId) { |
| 34 | + var groupNames = []; |
| 35 | + var grGroup = new GlideRecord('sys_user_group'); // Group table |
| 36 | + |
| 37 | + // Subquery to find groups the user is a member of |
| 38 | + var subquery = new GlideRecord('sys_user_grmember'); |
| 39 | + subquery.addQuery('user', userSysId); |
| 40 | + subquery.query(); |
| 41 | + |
| 42 | + var groupIds = []; |
| 43 | + while (subquery.next()) { |
| 44 | + groupIds.push(subquery.group.sys_id.toString()); |
| 45 | + } |
| 46 | + |
| 47 | + // Query for groups where the user is NOT a member |
| 48 | + grGroup.addQuery('sys_id', 'NOT IN', groupIds); |
| 49 | + grGroup.query(); |
| 50 | + |
| 51 | + while (grGroup.next()) { |
| 52 | + groupNames.push({ |
| 53 | + name: grGroup.name.toString() // Only store the group name |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + return groupNames; |
| 58 | + }, |
| 59 | + |
| 60 | + // Function to generate entitlements for current user groups, excluding a set number |
| 61 | + generateEntitlementsFromCurrentGroups: function(userSysId, groupsToRemove) { |
| 62 | + var currentGroups = this.getUserGroups(userSysId); |
| 63 | + var countToKeep = Math.max(0, currentGroups.length - groupsToRemove); // Ensure non-negative value |
| 64 | + var keptGroups = currentGroups.slice(0, countToKeep); // Keep the first groups, up to the count |
| 65 | + |
| 66 | + var entitlements = []; |
| 67 | + for (var i = 0; i < keptGroups.length; i++) { |
| 68 | + entitlements.push({ |
| 69 | + "value": keptGroups[i].name // Add kept group names to entitlements |
| 70 | + }); |
| 71 | + } |
| 72 | + return entitlements; |
| 73 | + }, |
| 74 | + |
| 75 | + // Function to generate entitlements for groups the user will "join" |
| 76 | + generateEntitlementsForNewGroups: function(userSysId, groupsToAdd) { |
| 77 | + var availableGroups = this.getGroupsNotMemberOf(userSysId); |
| 78 | + var entitlements = []; |
| 79 | + |
| 80 | + for (var i = 0; i < groupsToAdd; i++) { |
| 81 | + if (availableGroups.length > 0) { |
| 82 | + var randomIndex = Math.floor(Math.random() * availableGroups.length); |
| 83 | + var selectedGroup = availableGroups.splice(randomIndex, 1)[0]; |
| 84 | + entitlements.push({ |
| 85 | + "value": selectedGroup.name // Add selected group names to entitlements |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return entitlements; |
| 91 | + }, |
| 92 | + |
| 93 | + // Main function to generate entitlements |
| 94 | + generateEntitlements: function(userSysId, groupsToRemove, groupsToAdd, newGroupsToCreate) { |
| 95 | + // Initialize the object |
| 96 | + var userObj = { |
| 97 | + "schemas": [ |
| 98 | + "urn:ietf:params:scim:schemas:extension:servicenow:2.0:User", |
| 99 | + "urn:ietf:params:scim:schemas:core:2.0:User", |
| 100 | + "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" |
| 101 | + ], |
| 102 | + "id": userSysId, |
| 103 | + "meta": { |
| 104 | + "resourceType": "User", |
| 105 | + "created": "2006-07-11T21:16:15Z", |
| 106 | + "lastModified": new Date().toISOString(), // Current date for lastModified |
| 107 | + "location": "https://<instance-name>.service-now.com/api/now/scim/Users/" + userSysId //to be updated with your instance name |
| 108 | + }, |
| 109 | + "userName": "jvittolo", // Replace with actual user name if needed |
| 110 | + "name": { |
| 111 | + "familyName": "Vittolo", |
| 112 | + "givenName": "Jamessss" |
| 113 | + }, |
| 114 | + "displayName": "Jamessss Vittolo", |
| 115 | + "title": "VP, Client Services", |
| 116 | + "active": true, |
| 117 | + "emails": [ |
| 118 | + { |
| 119 | + |
| 120 | + "type": "work" |
| 121 | + } |
| 122 | + ], |
| 123 | + "entitlements": [] |
| 124 | + }; |
| 125 | + |
| 126 | + // Step 1: Generate entitlements for existing groups (excluding some) |
| 127 | + var currentGroupEntitlements = this.generateEntitlementsFromCurrentGroups(userSysId, groupsToRemove); |
| 128 | + userObj.entitlements = userObj.entitlements.concat(currentGroupEntitlements); |
| 129 | + |
| 130 | + // Step 2: Generate entitlements for new groups the user will "join" |
| 131 | + var newGroupEntitlements = this.generateEntitlementsForNewGroups(userSysId, groupsToAdd); |
| 132 | + userObj.entitlements = userObj.entitlements.concat(newGroupEntitlements); |
| 133 | + |
| 134 | + // Step 3: Create new groups (non-existing) and add to entitlements |
| 135 | + for (var j = 0; j < newGroupsToCreate; j++) { |
| 136 | + var newGroupName = this.generateRandomString(12); |
| 137 | + userObj.entitlements.push({ |
| 138 | + "value": newGroupName // Add new group names directly to entitlements |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + // Return the modified object |
| 143 | + return userObj; |
| 144 | + }, |
| 145 | + |
| 146 | + // Function to simulate removal of all group memberships |
| 147 | + removeAllGroupMemberships: function(userSysId) { |
| 148 | + // Initialize the object with all original properties |
| 149 | + var userObj = { |
| 150 | + "schemas": [ |
| 151 | + "urn:ietf:params:scim:schemas:extension:servicenow:2.0:User", |
| 152 | + "urn:ietf:params:scim:schemas:core:2.0:User", |
| 153 | + "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" |
| 154 | + ], |
| 155 | + "id": userSysId, |
| 156 | + "meta": { |
| 157 | + "resourceType": "User", |
| 158 | + "created": "2006-07-11T21:16:15Z", |
| 159 | + "lastModified": new Date().toISOString(), // Current date for lastModified |
| 160 | + "location": "https://<instance-name>.service-now.com/api/now/scim/Users/" + userSysId |
| 161 | + }, |
| 162 | + "userName": "jvittolo", // Replace with actual user name if needed |
| 163 | + "name": { |
| 164 | + "familyName": "Vittolo", |
| 165 | + "givenName": "Jamessss" |
| 166 | + }, |
| 167 | + "displayName": "Jamessss Vittolo", |
| 168 | + "title": "VP, Client Services", |
| 169 | + "active": true, |
| 170 | + "emails": [ |
| 171 | + { |
| 172 | + |
| 173 | + "type": "work" |
| 174 | + } |
| 175 | + ], |
| 176 | + "entitlements": [] // Empty entitlements array |
| 177 | + }; |
| 178 | + |
| 179 | + return userObj; // Return the full object with empty entitlements |
| 180 | + }, |
| 181 | + |
| 182 | + type: 'GenerateSCIMPayload' |
| 183 | +}; |
0 commit comments