logo

NJP

Redirect a specific role to SP portal with spentrypage

Import · Apr 12, 2018 · article

I had a requirement to redirect a user with 1 specific role and no other roles to the sp portal. So if a user has role x and no other role, then redirect to sp. If it has x and other roles, then redirect to backend.

properties that need to be edited:

glide.entry.page.script value: new SPEntryPage().getLoginURL()

glide.entry.first.page.script

value: new SPEntryPage().getFirstPageURL()

the SPEntryPage() is an OOB script included that contains 2 functions; getLoginURL and getFirstPageURL

For this requirement I have added a piece of code in the getFirstPageURL function.

her is the code

    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");

        //check if the role you want to send to portal is only role
        var splitting = gs.getUser().getRoles().toString();
        var arSplit = splitting.split(',');

        //if user has roles, normally redirect to backend
        if (user.hasRoles() && !redirectURL && !isServicePortalURL){
            if(arSplit.length == 1 && splitting.indexOf('YOU_ROLE_to_CHECK') > -1){ //if its the only role, we dont return, we redirect to sp. which is done at the bottom of the page. which is the oob code
            //do nothing
            }else{ //if specific role is not the only role then continue as normal
                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'

this can obviously be changed to many specific needs. I hope this will give you some ideas how to use the spentrypage script include.

Kind Regards,

Gert-Jan

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/redirect-a-specific-role-to-sp-portal-with-spentrypage/ta-p/2330172