diff --git a/Client-Side Components/Client Scripts/Dynamic Location Validation Approach/Readme.md b/Client-Side Components/Client Scripts/Dynamic Location Validation Approach/Readme.md new file mode 100644 index 0000000000..de0a46863e --- /dev/null +++ b/Client-Side Components/Client Scripts/Dynamic Location Validation Approach/Readme.md @@ -0,0 +1,4 @@ +User Location Validator +This solution ensures only users within their assigned business location can submit ServiceNow forms. The Script Include (Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js) fetches location coordinates from the user’s profile. The Client Script (Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js) compares these with the actual browser location, blocking submission if the user is outside the allowed area. Update office location in the user record to adjust the validation. + +If using a scoped application, ensure cross-scope access is allowed for Script Include calls. diff --git a/Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js b/Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js new file mode 100644 index 0000000000..2526f580a2 --- /dev/null +++ b/Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js @@ -0,0 +1,39 @@ +function onSubmit() { + var ga = new GlideAjax('UserLocationUtils'); + ga.addParam('sysparm_name', 'getUserLocationCoords'); + ga.getXMLAnswer(function(response) { + var locData = JSON.parse(response); + if (!locData) { + g_form.addErrorMessage("No assigned location found for your profile."); + return false; + } + + navigator.geolocation.getCurrentPosition(function(position) { + var userLat = position.coords.latitude; + var userLng = position.coords.longitude; + var allowedLat = locData.latitude; + var allowedLng = locData.longitude; + var locName = locData.name; + + var R = 6371; + var dLat = (userLat - allowedLat) * Math.PI / 180; + var dLng = (userLng - allowedLng) * Math.PI / 180; + var a = Math.sin(dLat / 2) ** 2 + + Math.cos(allowedLat * Math.PI / 180) * + Math.cos(userLat * Math.PI / 180) * + Math.sin(dLng / 2) ** 2; + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + var distance = R * c; + + if (distance > 10) { // 10 km tolerance + alert("You are " + distance.toFixed(2) + " km away from your registered office: " + locName); + g_form.addErrorMessage("Location validation failed."); + return false; + } else { + g_form.addInfoMessage("Location validated successfully within range of " + locName); + return true; + } + }); + }); + return false; // Wait for async location validation +} diff --git a/Server-Side Components/Script Includes/Dynamic Location Validation Approach/Readme.md b/Server-Side Components/Script Includes/Dynamic Location Validation Approach/Readme.md new file mode 100644 index 0000000000..de0a46863e --- /dev/null +++ b/Server-Side Components/Script Includes/Dynamic Location Validation Approach/Readme.md @@ -0,0 +1,4 @@ +User Location Validator +This solution ensures only users within their assigned business location can submit ServiceNow forms. The Script Include (Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js) fetches location coordinates from the user’s profile. The Client Script (Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js) compares these with the actual browser location, blocking submission if the user is outside the allowed area. Update office location in the user record to adjust the validation. + +If using a scoped application, ensure cross-scope access is allowed for Script Include calls. diff --git a/Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js b/Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js new file mode 100644 index 0000000000..5435dbd06b --- /dev/null +++ b/Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js @@ -0,0 +1,22 @@ +var UserLocationUtils = Class.create(); +UserLocationUtils.prototype = { + initialize: function() { + + }, + getUserLocationCoords: function() { + var user = gs.getUser(); + var loc = user.getRecord().location; + if (loc) { + var locGR = new GlideRecord('cmn_location'); + if (locGR.get(loc)) + return { + latitude: parseFloat(locGR.latitude), + longitude: parseFloat(locGR.longitude), + name: locGR.name.toString() + }; + } + return null; + }, + + type: 'UserLocationUtils' +};