Redirecting users without role to portal upon login
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 05:50 AM
Hi
I need to redirect users without a role to a service portal when they login.
To do so I have followed this guide. I have:
1. created "glide.entry.first.page.script" with value "new SPEntryPage().getFirstPageURL()"
2. created "glide.entry.page.script" with value "new SPEntryPage().getLoginURL()".
However, when I try to login with as a user with no role on https://instance.service-now.com/login.do I am still directed to the back-end, to https://instance.service-now.com/now/nav/ui/classic/params/target/home.do.
If I edit the URL to https://instance.service-now.com and hit enter I am directed to the portal, to https://instance.service-now.com/sp/. If i edit the URL to https://instance.service-now.com again and hit enter I am directed to the back-end, to https://instance.service-now.com/now/nav/ui/home. And this goes back and forth.
I am not sure how to fix this and what is wrong.
Any ideas?
- Labels:
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 06:23 AM
Hi,
Can you please try by adding the below script in spentry script include page.
When there is no role it will redirect to the sp portal.
initialize: function() {
this.logVariables = false; // for debugging
var comp = gs.getUser().getCompanyID();
if(comp){
var getSuffix = new GlideRecord('core_company');
getSuffix.get((comp));
//Remember to remove this
this.portal = (getSuffix.u_portal.url_suffix) ? '/' + getSuffix.u_portal.url_suffix + '/' : '/sp/';
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 07:29 AM
I dont quite understand what you want me to do.
Can you elaborate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2022 06:36 AM
Hi Lars,
This was a pain for us to figure out as well. We built this solution a few years ago. It still works, though I have not checked to see if I could upgrade it based on the newer versions of ServiceNow.
We do not redirect people to a specific portal. We redirect them to a portal that has an "unauthorized access" message. Our solution was to create a Public Page (sys_public) and a UI Script (sys_ui_script). The public page is branded to look exactly like the "unauthorized access" message portal. The ui script runs on load of the Platform UI and checks to see if they have the role needed to access the Platform UI. If the current user does not have that access, they are redirected to the UI Page.
Hope this helps to lead you in the right direction.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 02:37 AM
Can you try this out and check?
/**
*
* Service Portal sample script include to indicate
* 1. which login page should be used
* 2. the starting page after the user is authenticated
*
* Script is configured using system properties
* PROPERTY VALUE
* glide.entry.page.script new SPEntryPage().getLoginURL();
* glide.entry.first.page.script new SPEntryPage().getFirstPageURL();
*
* functions can return a path or null if no overrides are necessary
*
**/
var SPEntryPage = Class.create();
SPEntryPage.prototype = {
initialize: function() {
this.logVariables = false; // for debugging
this.portal = "/esc/"; // The URL suffix specified in the sp_portal record
},
/***
*
* Referred to by property: glide.entry.page.script
*
**/
getLoginURL: function() {
// When requesting a page directly (eg: /problem_list.do)
// The platform session_timeout.do sets the login page to welcome.do
// Since we are handling the login, clear that value
var session = gs.getSession();
var nt = session.getProperty("nav_to");
var sPage = session.getProperty("starting_page");
if (nt == "welcome.do")
session.clearProperty("nav_to");
if (!sPage && !nt)
session.putProperty("starting_page", gs.getProperty("glide.login.home"));
// Send the user to the portal's login page
var portalGR = new GlideRecord("sp_portal");
portalGR.addQuery("url_suffix", this.portal.replace(/\//g, ""));
portalGR.addNotNullQuery("login_page");
portalGR.query();
if (portalGR.next())
return this.portal + "?id=" + portalGR.login_page.id;
// Send to the a default login page
return this.portal + "?id=login";
},
/***
*
* Referred to by property: glide.entry.first.page.script
*
**/
getFirstPageURL: function() {
var session = gs.getSession();
this.logProperties('before', session);
// has roles and is not a Service Portal page - go to UI16
var nt = session.getProperty("nav_to");
var isServicePortalURL = new GlideSPScriptable().isServicePortalURL(nt);
var redirectURL = session.getProperty("login_redirect");
//if (user.hasRoles() && !redirectURL && !isServicePortalURL)
if (((user.hasRole('sn_hr_core.basic') || user.hasRole('scrum_user') || user.hasRole('knowledge')) && !redirectURL && !isServicePortalURL)
|| gs.getImpersonatingUserName() !== gs.getUserName() && gs.getImpersonatingUserName() !== null)
return;
// user may have logged in from a frame, the /login_redirect.do page will bust out of it
if (!redirectURL) {
// redirectURL is nav_to
// if nav_to == "welcome.do" then use starting_page
var sPage = session.getProperty("starting_page");
if (sPage && nt == "welcome.do")
nt = sPage;
// Avoid a redirect loop to the home page
var ep = gs.getProperty("glide.login.home");
if (nt) {
if (ep == nt)
nt = null;
}
// PRB726860: if page is still welcome.do, go to glide.login.home preserving frameset
if (nt == "welcome.do") {
session.putProperty("nav_to", ep);
return;
}
session.putProperty("login_redirect", nt || "true");
return "/login_redirect.do?sysparm_stack=no";
}
session.clearProperty("login_redirect");
var returnUrl = this.portal;
if (redirectURL && redirectURL != "true") {
var spUrl = new GlideSPScriptable().mapUrlToSPUrl(redirectURL);
returnUrl = spUrl ? this.portal + "?" + spUrl : redirectURL;
}
this.logProperties('after', session);
if (!this.logVariables) {
gs.log('redirectURL: ' + redirectURL);
gs.log('User: ' + user.getName());
gs.log('is internal: ' + (!user.hasRoles()));
gs.log('returnUrl: ' + returnUrl);
}
return returnUrl;
},
logProperties: function(beforeOrAfter, session) {
if (!this.logVariables)
return;
gs.log('SPEntryPage: Redirect ------------------------------- ' + beforeOrAfter);
gs.log('session.starting_page: ' + session.getProperty("starting_page"));
gs.log('session.nav_to: ' + session.getProperty("nav_to"));
gs.log('session.login_redirect: ' + session.getProperty("login_redirect"));
gs.log('gs.fURI: ' + session.getURI());
},
type: 'SPEntryPage'
};