-
Notifications
You must be signed in to change notification settings - Fork 908
Charanjeet hacktoberfest#2 #2175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Charanjet
wants to merge
8
commits into
ServiceNowDevProgram:main
from
Charanjet:Charanjeet-Hacktoberfest#2
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a7b7262
Create Readme.md
Charanjet fc57af8
Create Smart Attachment Size Limiter.js
Charanjet 9149ae9
Delete Server-Side Components/Business Rules/Smart Attachment Size Li…
Charanjet b60349f
Merge branch 'ServiceNowDevProgram:main' into Charanjeet-Hacktoberfest#2
Charanjet a9c13f7
Add UserLocationUtils for retrieving user location
Charanjet 9fa52c7
Add Readme for User Location Validator solution
Charanjet c15fe89
Add README for User Location Validator solution
Charanjet ce7e10f
Implement user location validation on form submission
Charanjet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...t-Side Components/Client Scripts/Dynamic Location Validation Approach/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
39 changes: 39 additions & 0 deletions
39
...Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Charanjet |
||
| 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 | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
...-Side Components/Script Includes/Dynamic Location Validation Approach/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
22 changes: 22 additions & 0 deletions
22
...Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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' | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add more comments to your read me file with an output ? This will help to understand How the current user location is fetched& get evaluated with defined range.