How to direct users with the approver_user role to the employee center upon log in

Daniel R2
Kilo Sage

 

Hi, Please see the whole SPEntry script include below. We have edited the SPEntry script include to ensure that any user with the snc_internal role that does not have any other role, they will be automatically directed to the employee center and they will not be able to access the backend. Please note all users will have the snc_internal role, however It is configured, that any user with the snc_internal role and any additional role they will be directed to the backend.

However there is an additional requirement. Any user with the approver_user role and snc_internal role and NO agent role, they should also be directed to the employee center upon log on, however it is still possible for them to access, and redirect to the backend if they need to.

 

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

			var SNCInternalUserRole = "7************************"; // Sys id of snc_internal role must be put here
		var SNCInternalRole = new GlideRecord("sys_user_has_role");
		SNCInternalRole.addQuery("user",gs.getUserID());
		var roleCount = 0;
		var hasSNCInternalRole = false;
		SNCInternalRole.query();
		while(SNCInternalRole.next()){
			roleCount++;
			if(SNCInternalRole.getValue("role") == SNCInternalUserRole){
				hasSNCInternalRole = true;
			}
		}
			var hasSNCInternalRoleOnly = (roleCount == 1 && hasSNCInternalRole);
		
        if (!sPage && !nt && !hasSNCInternalRoleOnly)
            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");
		
		// NEW - Check for users with only snc_internal role
		var SNCInternalUserRole = "7fcaa702933002009c8579b4f47ffbde"; // Sys id of snc_internal role must be put here
		var SNCInternalRole = new GlideRecord("sys_user_has_role");
		SNCInternalRole.addQuery("user",gs.getUserID());
		var roleCount = 0;
		var hasSNCInternalRole = false;
		SNCInternalRole.query();
		while(SNCInternalRole.next()){
			roleCount++;
			if(SNCInternalRole.getValue("role") == SNCInternalUserRole){
				hasSNCInternalRole = true;
			}
		}
		var hasSNCInternalRoleOnly = (roleCount == 1 && hasSNCInternalRole);

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

		if(hasSNCInternalRoleOnly){
			return "/esc";
		}
		
        // 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");
        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'
};

 

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi Daniel,

 

I've noticed that you're asking a lot of questions on the forums, which is great, but I wanted to mention to take a moment and review all your past posts and if replies have been Helpful, please mark them as Helpful. If certain replies helped guide you towards a solution, please mark those as Accept Solution.

 

With that said, from your profile, do you work for the ServiceNow partner IBM? If so, you have access to a lot of great peers there that can help much faster than we can on the forums as you can screen share, etc. I'd recommend using your peer network as well.

 

Such as here, someone knows some coding because they've added that section to this script. From your post, we don't see where you...have tried anything. What are you thinking you need to do here? What have you tried? What has worked? Hasn't worked?

 

I'm unsure if you're a new developer, but we'd love to maybe help explain things further for you to learn and so it's best for you to explain your thinking instead of posting requirements on the forums without your input and thoughts.

 

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Rupesh Ram Chi1
Tera Contributor

@Daniel R2 Thanks for sharing the article, if we change OOTB script include. will it impact system upgrade in future ? any idea ?