Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

gjb
Giga Contributor

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

Comments
Akshya
Tera Expert

Very helpful!

Thanks,
Akshya

Robert Duca
Tera Contributor

i need to check for the new HR client roles and no other roles they need to go to /ESC.

There seem to be two specifically:

  • sn_hr_core.hrsm_employee
  • sn_hr_sp.hrsp_employee

If any user has one or both of these roles only they should be directed to /esc

If they have either of the above, both or none and any other roles they should go to UI16

 

Nishant16
Tera Expert

Hello Robert

did you find a solution for this how to redirect user with specific roles?

Steve2000
Tera Contributor

Very very helpful, thank you for this script 

Version history
Last update:
‎04-12-2018 01:38 AM
Updated by: