Redirecting Approver Users to the Service Portal

Demetrius Purn1
Kilo Contributor

I am hoping for a little help. Currently we have the code below that redirects a user with the snc_internal role only to the Service Portal. What I am looking to do is have two things happen. Keep the current functionality in place, but also have any user with the approver_user and snc_internal role be redirected to our Service Portal. What would be the best way to go about this?

 

/***
	 *
	 * 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 code - check for users with only snc_internal role
		var SNCInternalUserRole = "7fcaa702933002009c8579b4f47ffbde";
		//var SNCInternalUserRole = "debab85bff02110053ccffffffffffb6"; //approver_user sys_id
		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);
		//below new code - to redirect to portal
		if (user.hasRoles() && !hasSNCInternalRoleOnly && !redirectURL && !isServicePortalURL)
		
// commentiong the below line - OOTB for testing
		//if (user.hasRoles("approver_user") && !redirectURL && !isServicePortalURL)
			return;
4 REPLIES 4

MrMuhammad
Giga Sage

@Demetrius Purnell 

Please try this.

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() || gs.hasRole("approver_user, snc_internal") ) && !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");
		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;
	},
Regards,
Muhammad

if above causes redirection for admins then please update below line as

if (user.hasRoles() || (gs.hasRole("approver_user, snc_internal" )&& !gs.hasRole("admin")) && !redirectURL && !isServicePortalURL)
	return;
Regards,
Muhammad

I am going to try this and get back to you. Thank You.

Kieran Anson
Kilo Patron

Hey,

you don't need to GlideRecord the user role table, instead you can just use the GlideSession API to determine if the user has a certain role/roles.