Untitled

                Never    
var PartnerPortalRedirection = Class.create();
PartnerPortalRedirection.prototype = {
	_log: Object,

	/*
	 * Method to determine if a user, given a Url path, must be redirected elsewhere.
	 * This is used by the login script (installation exit for saml 2.0),
	 * and also with a global UI scripts.
	 * The purpose is to prevent external users from seeing the instance view (sidebar and header) upon login
	 * even if they change the url on the address bar.
	 * Internal users should continue as expected.
	 *
	 * Rules:
	 * 1. No interferance if the user is admin, maint or has the role instance_view
	 * 2. Only users which have the role partner_user are allowed to see any urls on the portal home. Else, send to onboard
	 * 3. Only users who have not yet sumbitted the onboarding form are allowed to see the onboard record producer. Else, send to account_review.
	 *
	 * @param {String} path The url pathname user is trying to access
	 * @param {String} sys_id of the user who is trying to access the resource
	 * @return {Object} Json object with the format { needRedirect : {Boolean}}, redirectTo : {String} }
	 */
	execute: function(path, userId) {

		this._log.redirection('SI', this.type, 'execute begin with ', path, userId.toString());
		var result;
		var pa = this._getPartnerAccount(userId);
		if (this._hasRole(userId, 'admin') || this._hasRole(userId, 'maint') || this._hasRole(userId, gs.getProperty('partnerportal.environment.rolename.instanceview'))) {
			this._log.redirection('SI', this.type, 'execute', 'user has instance access');
			result = {
				needRedirect: false,
				redirectTo: ''
			};
		} else if (this._isPublicPage(path)) {
			this._log.redirection('SI', this.type, 'execute', 'Is Public Page');
			result = {
				needRedirect: false,
				redirectTo: ''
			};
		} else if (pa.next()) { // if partner account is avilable check for account status profile status and resign status.

			if (pa.u_partner_registration_status == "off_boarded") { // check for account status if off-boarded redirect them to Oops page(account_review)
				this._log.redirection('SI', this.type, 'execute', 'Account offboarded');
				result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));
			} else if (this._hasRole(userId, 'partner_user')) { //check for user access, if user has partner user role check for mandate profile, resign

				//override for expand partnership del users
				var expDelContact = new DelegaterService();
				var expDelData = expDelContact.getDelegationInfo();
				if (expDelData.isDelegatedContact && expDelData.delegatedTasks.length > 0) { //only if the delegated contact and has delegated tasks
					if ( /*STRY1432591*/ expDelData.delegatedTasks.indexOf('PMTA') != -1 && (pa.u_partner_registration_substatus == 're_sign_application' || pa.u_requires_pmta_re_sign == true || pa.u_requires_pmta_re_sign == "true")) {
						return this._hasPMTAResign(pa, path);
					} else {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.onboarding') + "?expand=true");
					}
				} else {
					var tools = new ContactTools();
					if (tools.mandateProfileUpdate(userId)) {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.profileupdateMandatory'));
						this._log.redirection('SI', this.type, 'execute', 'Is Manadate profile update');
					} else if (path.indexOf(gs.getProperty('partnerportal.ui.urlsuffix.expandPartnership')) >= 0) {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.expandPartnership'));
					} else if (pa.u_partner_registration_substatus == 're_sign_application' && (pa.getValue('u_primary_contact_user') == gs.getUser().getID())) {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.onboarding'));
						this._log.redirection('SI', this.type, 'execute', 'Requires PMTA and NDA resign');
					} else if (pa.u_requires_pmta_re_sign == 'true' || pa.u_requires_pmta_re_sign == true) {
						this._log.redirection('SI', this.type, 'execute', 'Requires PMTA resign');
						return this._hasPMTAResign(pa, path);
					} else {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.portalhome'));
						this._log.redirection('SI', this.type, 'execute', 'user has homepage access');
					}
				}
			} else {
				this._log.redirection('SI', this.type, 'execute', 'check for access');
				result = this._evaluatePartnerAccess(pa, path);

			}
		} else {
			this._log.warning('SI', this.type, 'execute', 'Partner account not found for user');
			if (path.indexOf('logged_out') == -1) {
				result = {
					needRedirect: true,
					redirectTo: 'logout.do'
				};
			} else {
				result = {
					needRedirect: false,
					redirectTo: ''
				};
			}
		}
		this._log.redirection('SI', this.type, 'execute end with ', result);
		return result;
	},

	_hasRole: function(userId, role) {
		this._log.redirection('SI', this.type, '_hasRole begin with', userId, role);

		var result = false;

		/*
		At this point, this user does not have a session, and so gs.hasRole will not work.
		We will look in sys_user_has_role table first, as this method of lookup is agnostic to whether the role is inherited.
		*/
		var allRoles = [];
		var roleGr = new GlideRecord('sys_user_has_role');
		roleGr.addQuery('user', userId);
		roleGr.query();

		while (roleGr.next()) {
			if (role == roleGr.role.getDisplayValue().toString()) {
				result = true;
				break;
			}
		}

		/*
		If not yet found, we need to check two possibilities which are not entries in the table used above
		1. This is the 'system' user (recipient of events being fired)
		2. This is a user with 'maint' role (hopping in)

		For those, we will use the below method.
		Please note that while this method works for these 2 scenarios, it cannot be used for assigned roles
		because it ignores any roles that were inherited.
		*/
		if (!result) {
			var user = gs.getUser().getUserByID(userId);

			var roles = user.getUserRoles().toArray();
			for (var i = 0; i < roles.length; i++) {
				if (roles[i] == role) {
					result = true;
					break;
				}
			}
		}

		this._log.redirection('SI', this.type, '_hasRole end with', result);
		return result;
	},

	_getPartnerAccount: function(userId) {
		this._log.redirection('SI', this.type, '_getPartnerAccount begin for ', userId.toString());

		var userGr = new GlideRecord('sys_user');
		userGr.get(userId);
		/*Phase 4.0 - To enable TPP payment option to all other contacts as well*/
		//result.addQuery('u_primary_contact_user', userId);
		var paGr = new GlideRecord('u_partner_account');
		paGr.addQuery('sys_id', userGr.u_account);
		paGr.query();
		this._log.redirection('SI', this.type, '_getPartnerAccount end with ', paGr);
		return paGr;
	},

	_getTechnologyPartnershipStatus: function(partnerAccount) {
		this._log.redirection('SI', this.type, '_getTechnologyPartnershipStatus begin for Partner Account', partnerAccount.u_legal_name);

		var result = new GlideRecord('u_account_partnership');
		result.addQuery('u_partner_account', partnerAccount.sys_id);
		result.addQuery('u_program_partnership.u_name', 'Technology');
		result.query();
		result.next();
		this._log.redirection('SI', this.type, '_getTechnologyPartnershipStatus end with ', result);
		return result;
	},

	_evaluatePartnerAccess: function(pa, path) {
		this._log.redirection('SI', this.type, '_evaluatePartnerAccess begin with', path);
		var result;
		var onboardingComplete = (pa.u_negotiated_contract && pa.u_stage == 'program_info_entered') || pa.u_stage == 'partner_agreements_accepted';
		var technologyPartnership = this._getTechnologyPartnershipStatus(pa);
		// check for credit_check path to allow applicants to fill out credit check application
		if (this._checkForCCAPath(path)) {
			this._log.redirection('SI', this.type, '_evaluatePartnerAccess', 'Credit Check Application Condition: Start');
			var permitAccess = CreditCheckHelper.accessCCAModule(); // if primary contact or member admin, and display CCA widget on acct is true
			if (permitAccess) result = {
				needRedirect: false,
				redirectTo: ''
			};
			else result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));

		} else if (onboardingComplete && path.indexOf(gs.getProperty('partnerportal.ui.urlsuffix.thankyou')) == -1) {
			this._log.redirection('SI', this.type, 'execute', 'onboardingComplete');
			if (technologyPartnership.u_approval_status == 'accepted' && pa.u_surf_account_number && pa.u_partner_registration_status == 'program_approved') {
				var rec = new GlideRecord('u_tpp_details');
				rec.addQuery('u_partner_account', pa.sys_id);
				rec.addQuery('u_type', 'registration');
				rec.addQuery('u_fee_payment_status', 'pending_payment');
				rec.addQuery('u_active', true);
				rec.query();
				if (rec.next()) {
					this._log.redirection('SI', this.type, 'execute', 'Pending Payment');
					if (rec.u_status == 'synced_invalid' && rec.u_purchase_status == 'failure') {
						this._log.redirection('SI', this.type, 'execute', 'technical failure in technology fee payment, sending to account review');
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));
					} else {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.tppPayment'));
					}
				} else {
					this._log.redirection('SI', this.type, 'execute', 'tpp details record is not properly tagged, sending to account review');
					result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));
				}
			} else if (technologyPartnership.u_approval_status == 'pending_renewal' && pa.u_surf_account_number && pa.u_partner_registration_status == 'fully_approved') {
				this._log.redirection('SI', this.type, 'execute', 'pending_renewal');
				var rec = new GlideRecord('u_tpp_details');
				rec.addQuery('sys_id', pa.u_tpp);
				rec.addQuery('u_type', 'registration_renewal');
				rec.addQuery('u_fee_payment_status', 'pending_payment');
				rec.query();
				if (rec.next()) {
					if (rec.u_status == 'synced_invalid' && rec.u_purchase_status == 'failure') {
						this._log.redirection('SI', this.type, 'execute', 'technical failure in technology fee payment, sending to account review');
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));
					} else {
						result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.tppRenewalPayment'));
					}
				} else {
					this._log.redirection('SI', this.type, 'execute', 'tpp details record is not properly tagged, sending to account review');
					result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));
				}
			} else {
				this._log.redirection('SI', this.type, 'execute', 'user is awaiting approval, sending to account review');
				result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.accountreview'));
			}
		} else if (path.indexOf(gs.getProperty('partnerportal.ui.urlsuffix.thankyou')) == -1) {
			this._log.redirection('SI', this.type, 'execute', 'user is onboarding');
			result = this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.onboarding'));
		} else {
			this._log.redirection('SI', this.type, 'execute', 'user just finished onboarding');
			result = {
				needRedirect: false,
				redirectTo: ''
			};
		}
		this._log.redirection('SI', this.type, '_evaluatePartnerAccess end with ', result);
		return result;
	},

	_forceSuffix: function(path, suffix) {
		this._log.redirection('SI', this.type, '_forceSuffix begin with ', path, suffix);

		var result;
		var fullSuffix = gs.getProperty('glide.servlet.uri') + suffix;
		var pathArray = path.split('/');

		pathArray.splice(0, 3); // deletes the contents up to https://hostname/
		var pathSuffix = pathArray.join('/'); // gives the path followed by hostname
		pathSuffix = decodeURIComponent(pathSuffix);

		var knowledgeURL = gs.getProperty('glide.servlet.uri') + gs.getProperty('pp.knowledge.articles.url.suffix');
		if (path.startsWith(knowledgeURL)) { // STRY0470399: automatically allow links pointing to PP knowledge base articles
			this._log.redirection('SI', this.type, '_forceSuffix', 'user trying to access kb article: ', path);
			result = {
				needRedirect: false,
				redirectTo: ''
			};
		} else if (!pathSuffix.startsWith(suffix)) {
			this._log.redirection('SI', this.type, '_forceSuffix', 'user was trying to go somewhere else than', fullSuffix, 'redirecting back there');
			result = {
				needRedirect: true,
				redirectTo: suffix
			};
		} else {
			result = {
				needRedirect: false,
				redirectTo: ''
			};
		}

		this._log.redirection('SI', this.type, '_forceSuffix end with ', result);
		return result;
	},

	_hasPendingDelegations: function(userId) {
		this._log.redirection('SI', this.type, '__hasPendingDelegations execute begin userID is: ', userId);
		var delegation = {
			'required': false
		};
		var gr = new GlideRecord("u_contact");
		var delegationgr = new GlideRecord('u_delegater_mapping');
		gr.addQuery("u_user", userId);
		gr.query();
		if (gr.next()) {
			delegationgr.addQuery('u_contact', gr.sys_id);
			delegationgr.addQuery('u_delegated_status', 'pending');
			delegationgr.orderBy('sys_created_on');
			delegationgr.query();
			if (delegationgr.next()) {
				delegation.required = true;
				delegation.redirectTo = delegationgr.u_delegated_task.toString().toLowerCase() + '.do';
				this._log.redirection('SI', this.type, '__hasPendingDelegations execute delegation.redirectTo ', delegation.redirectTo);
				return delegation;
			} else {
				return delegation;
			}
		} else {
			return delegation;
		}

	},

	/**
	 * _checkForCCAPath() check if path contains URL suffix that is related to Credit Check Application
	 * @param {string} path
	 * @return {boolean}
	 */
	_checkForCCAPath: function(path) {
		this._log.redirection('SI', this.type, '_checkForCCAPath: start');

		// comma seperated str of URL suffixes for Credit Check Application functionality
		var suffixesProp = gs.getProperty('partnerportal.ui.urlsuffix.resellerCreditCheck');
		var suffixesArr = suffixesProp.split(',');
		for (var i = 0; i < suffixesArr.length; i++) {
			if (path.indexOf(suffixesArr[i]) != -1) {
				this._log.redirection('SI', this.type, '_checkForCCAPath: return true: /' + suffixesArr[i]);
				return true;
			}
		}

		this._log.redirection('SI', this.type, '_checkForCCAPath: return false');
		return false;
	},
	_hasPMTAResign: function(pa, path) {
		this._log.redirection('SI', this.type, '_hasPMTAResign execute Begin ');
		var delegated = this._hasPendingDelegations(gs.getUser().getID()); // check whether the user is delegated or not
		var accept_later = new LegalClickThroughUtil().isAcceptLater();
		if (accept_later) {
			return this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.portalhome'));
		}
		var status = pa.status;
		var isPrimayContact = (pa.getValue('u_primary_contact_user') == gs.getUser().getID()); // check wheter the user primary or not
		this._log.redirection('SI', this.type, "_hasPMTAResign execute Begin isPrimayContact: " + isPrimayContact + " delagated " + delegated.required);
		if (delegated.required || isPrimayContact) { // if logged in user is delegated user/primary user redirect him to accept page.
			return this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.onboarding'));
		} else //if logged in user is normal user do nothing
		{
			return this._forceSuffix(path, gs.getProperty('partnerportal.ui.urlsuffix.portalhome'));
		}
	},

	_hasDelegatedPmtaResign: function(userId) {
		this._log.redirection('SI', this.type, '_hasDelegatedPmtaResign execute Begin ');
		var delegation = {
			'required': false
		};
		var gr = new GlideRecord("u_contact");
		var delegationgr = new GlideRecord('u_delegater_mapping');
		gr.addQuery("u_user", userId);
		gr.query();
		if (gr.next()) {
			delegationgr.addQuery('u_contact', gr.sys_id);
			delegationgr.addQuery('u_delegated_status', 'pending');
			delegationgr.addQuery('u_delegated_task', 'PMTA');
			delegationgr.orderBy('sys_created_on');
			delegationgr.query();
			if (delegationgr.next()) {
				delegation.required = true;
				delegation.redirectTo = delegationgr.u_delegated_task.toString().toLowerCase() + '.do';
				this._log.redirection('SI', this.type, '_hasDelegatedPmtaResign execute Begin delegationgr' + delegationgr.u_delegated_task.toString().toLowerCase());
				return delegation;
			} else {
				return delegation;
			}
		} else {
			return delegation;
		}

	},

	_isPublicPage: function(path) {
		var n1 = path.search(/.com\//i);
		var n2 = path.search(/.do/i);
		var pageName = path.substring(n1 + 5, n2) + '';
		var _blackList = gs.getProperty('BlackListed.PublicPages.forPartners') + "";
		var pageArray = _blackList.split(',');


		if (n1 != -1 && n2 != -1) {
			var gr = new GlideRecord('sys_public');
			gr.addQuery('page', path.substring(n1 + 5, n2));
			gr.addQuery('active', true);
			gr.query();
			if (gr.next() && pageArray.indexOf(pageName) == -1) {
				return true;
			}
		}
		return false;
	},



	initialize: function() {
		this._log = new PartnerPortalLogger();
	},

	type: 'PartnerPortalRedirection'
};

Raw Text