Login redirect

Mattikus4901
Tera Contributor

I need to have all users that have the snc_external role to be redirected to the /cp. 

I am including my spEntryPage script. 

All users are still being redirected, regardless of role to the SOW. 

Is there something wrong with my script?

 

/** 
 *
 *  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 SPEntryPagev1().getLoginURL(); 
*  glide.entry.first.page.script   new SPEntryPagev1().getFirstPageURL();
* functions can return a path or null if no overrides are necessary
**/
var SPEntryPagev1 = Class.create();

SPEntryPagev1.prototype = {

initialize: function() {
    this.logVariables = true// 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 "/sp/";
},
/***
 *
 * 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);

    /*if (user.hasRole("snc_external")) {
        // Redirect users with the "snc_external" role to /cp
        var returnUrl = "/cp"; 
        } */

    // Checking roles to redirect
    if(user.hasRole('admin'))
    {
    gs.log("!Admin Redirection!");//new
        return "/?.do";
    }
    else if (user.hasRoles('snc_external') && !redirectURL && !isServicePortalURL)
    {
    gs.log("!External Redirection!");//new
        return "/cp/";
    }
    else if (user.hasRole() && !redirectURL && !isServicePortalURL)
    {
    gs.log("!Internal Redirection!");//new
        return "/?.do";
    }
    else if (user.hasRole('') && !redirectURL && !isServicePortalURL)
    {
    gs.log("!No Role Redirection!");
        return "/cp/";
    } 
    else 
    {
        // For users without the "snc_external" role, return a different URL
        var nt = session.getProperty("nav_to");
        var isServicePortalURL = new GlideSPScriptable().isServicePortalURL(nt);
        var redirectURL = session.getProperty("login_redirect");

        // Rest of your existing code for handling redirection

        // ...

        if (!redirectURL) {
            // rest of your existing code for handling redirection
            // ...

            session.putProperty("login_redirect", nt || "true");
            returnUrl = "/login_redirect.do?sysparm_stack=no";
        }

        session.clearProperty("login_redirect");
        session.clearProperty("nav_to");
    }

    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('SPEntryPagev1: 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: 'SPEntryPagev1'
};
1 REPLY 1

athm
Tera Guru

From a quick glance, try this in a subprod first, it should work as expected, I just tested.
1. Revert the script to OOB.
2. Then add the conditional 'if' block in the script include at the location specified. This should work as expected.
3. Now if you have more additional redirects based on other roles, just add those if blocks under the same

var redirectURL = session.getProperty("login_redirect");

//Redirect external users
if (user.hasRole("snc_external"))
return '/cp';

//If there are more roles/pages to redirect users, add them below
/*
Ex: 
if(user.hasRole("some_role_here"))
return '/your_page_URI_here';
*/

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