Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Redirect users who connect to the system based on their ROLE to the ESC Portal or SOW

ItayB
Tera Contributor

Hello everyone,
In order to solve the question I wrote in this post, I went to the Script Includes table and from there I searched for "SPEntryPage", I edited the "getFirstPageURL" method like this:

 

ItayB_0-1752586833342.png

 

 

And in addition, in System Properties I defined it as follows:

 

ItayB_1-1752586876898.png

 

 

But when I try to connect to the system from a user who has an ITIL ROLE, I get to this page and not the SOW:

 

ItayB_2-1752586927718.png

 

 

This is the FULL Script in "SPEntryPage" :
- - - - - - - - 

/**
 *
 *  Service Portal script include to indicate
 *  1. which login page should be used
 *  2. the starting page after the user is authenticated
 *
 *  Configured using system properties:
 *  glide.entry.page.script         = new SPEntryPage().getLoginURL();
 *  glide.entry.first.page.script   = new SPEntryPage().getFirstPageURL();
 *
 */
var SPEntryPage = Class.create();

SPEntryPage.prototype = {

    initialize: function() {
        this.logVariables = false; // for debugging
        this.portal = this.getDefaultPortal(); // The URL suffix for default portal
    },

    getDefaultPortal: function() {
        var gr = new GlideRecord("sp_portal");
        gr.addQuery("default", true);
        gr.query();
        if (gr.next())
            return "/" + gr.getValue("url_suffix") + "/";
        return "/esc/";
    },

    getLoginURL: function() {
        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"));

        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;

        return this.portal + "?id=login";
    },

    getFirstPageURL: function() {
        var user = gs.getUser();
        var roles = user.getRoles();
        var session = gs.getSession();

        if (user.hasRole('itil')) {
            return '/now/sow/home';
        }

        if (!user.hasRole('itil')) {
            return '/esc/';
        }

        var nt = session.getProperty("nav_to");
        var isServicePortalURL = new GlideSPScriptable().isServicePortalURL(nt);

        if (user.hasRoles() && !isServicePortalURL && redirectURL)
            return;

        if (!redirectURL) {
            var sPage = session.getProperty("starting_page");
            if (sPage && nt == "welcome.do")
                nt = sPage;

            var ep = gs.getProperty("glide.login.home");
            if (nt && ep == nt)
                nt = null;

            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");
        session.clearProperty("nav_to");

        if (redirectURL && redirectURL.indexOf("sys_attachment.do") > -1)
            return redirectURL;

        var returnUrl = this.portal;

        if (redirectURL && redirectURL != "true") {
            var spUrl = new GlideSPScriptable().mapUrlToSPUrl(redirectURL);
            returnUrl = spUrl ? this.portal + "?" + spUrl : redirectURL;

            if (!user.hasRoles() && !spUrl && redirectURL.indexOf("home_splash.do") > -1)
                returnUrl = this.portal;
        }

        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'
};

 

- - - - - - - - 

 

I would appreciate your assistance in this matter,
Thank you very much.

2 REPLIES 2

ItayB
Tera Contributor

I changed my script to this one:

/**
 *
 *  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 = this.getDefaultPortal(); // The URL suffix for default portal
    },

    /***
     *
     * Fetch the default portal suffix
     *
     **/
    getDefaultPortal: function() {
        var gr = new GlideRecord("sp_portal");
        gr.addQuery("default", true);
        gr.query();
        if (gr.next())
            return "/" + gr.getValue("url_suffix") + "/";

        return "/esc/";
    },
    /***
     *
     * 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 (gs.hasRole('itil') && !redirectURL && !isServicePortalURL)
            return 'now/sow';
       
        if (!gs.hasRole('itil') && !redirectURL && !isServicePortalURL)
            return '/esc/';

        if (gs.hasRoles() && !redirectURL && !isServicePortalURL)
            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");
        session.clearProperty("nav_to");
        if (redirectURL && redirectURL.indexOf("sys_attachment.do") > -1)
            return redirectURL;
        var returnUrl = this.portal;
        if (redirectURL && redirectURL != "true") {
            var spUrl = new GlideSPScriptable().mapUrlToSPUrl(redirectURL);
            returnUrl = spUrl ? this.portal + "?" + spUrl : redirectURL;
            if (!user.hasRoles() && !spUrl && redirectURL.indexOf("home_splash.do") > -1)
                returnUrl = this.portal;
        }

        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'
};



Its almost work but the problem now, that its work only when i clean the cache, otherwise its connected to the ESC usually.

sonalt
Kilo Contributor

If I want to redirect itil user to backend ui how can I modify SPEntrypage script include