<commit_before>package cz.metacentrum.perun.webgui.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayInteger;
import cz.metacentrum.perun.webgui.client.mainmenu.MainMenu;
import cz.metacentrum.perun.webgui.client.resources.PerunEntity;
import cz.metacentrum.perun.webgui.json.GetEntityById;
import cz.metacentrum.perun.webgui.json.JsonCallbackEvents;
import cz.metacentrum.perun.webgui.model.*;
import cz.metacentrum.perun.webgui.tabs.TabManager;
import cz.metacentrum.perun.webgui.tabs.facilitiestabs.FacilityDetailTabItem;
import cz.metacentrum.perun.webgui.tabs.groupstabs.GroupDetailTabItem;
import cz.metacentrum.perun.webgui.tabs.securitytabs.SecurityTeamDetailTabItem;
import cz.metacentrum.perun.webgui.tabs.vostabs.VoDetailTabItem;

import java.util.ArrayList;

/**
 * Class for session of the Web GUI.
 * Session is passed to almost all other classes in constructor.
 *
 * Contains main Client classes like UiElements, WebGui, MainMenu.
 * Also stores user's information returned from RPC during login.
 * Provides authorization by keeping list of editable entities for user.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

public class PerunWebSession {

	// Main GUI classes
	private UiElements uiElements;
	private TabManager tabManager;
	private WebGui webGui;

	// User's authz data
	private PerunPrincipal perunPrincipal = null; // contain all users auth returned from RPC

	//Keepers of user's roles
	private boolean perunAdmin = false;
	private boolean groupAdmin = false;
	private boolean voAdmin = false;
	private boolean facilityAdmin = false;
	private boolean voObserver = false; // is not vo admin
	private boolean self = false; // is not admin
	private boolean securityAdmin = false;
	private boolean sponsor = false; // sponsor

	// User roles constants
	static public final String PERUN_ADMIN_PRINCIPAL_ROLE = "PERUNADMIN";
	static public final String GROUP_ADMIN_PRINCIPAL_ROLE = "GROUPADMIN";
	static public final String VO_ADMIN_PRINCIPAL_ROLE = "VOADMIN";
	static public final String FACILITY_ADMIN_PRINCIPAL_ROLE = "FACILITYADMIN";
	static public final String USER_ROLE = "SELF";
	static public final String VO_OBSERVER_PRINCIPAL_ROLE = "VOOBSERVER";
	static public final String SECURITY_ADMIN_PRINCIPAL_ROLE = "SECURITYADMIN";

	// Entities which can the user edit
	private ArrayList<Integer> editableGroups = new ArrayList<Integer>();
	private ArrayList<Integer> editableVos = new ArrayList<Integer>();
	private ArrayList<Integer> editableFacilities = new ArrayList<Integer>();
	private ArrayList<Integer> editableUsers = new ArrayList<Integer>();
	private ArrayList<Integer> editableSecTeams = new ArrayList<Integer>();
	private ArrayList<Integer> editableSponsoredUsers = new ArrayList<Integer>();


	// entities which user can view (Observer role)
	private ArrayList<Integer> viewableVos = new ArrayList<Integer>();

	// Currently active entities - user is editing them now
	private VirtualOrganization activeVo;
	private Group activeGroup;
	private Facility activeFacility;
	private User activeUser;
	private SecurityTeam activeSecurityTeam;

	// History of entities which user edited
	private ArrayList<GeneralObject> entitiesHistoryList = new ArrayList<GeneralObject>();

	private BasicOverlayType configuration;

	// RPC URL
	private String rpcUrl = "";

	// Only instance
	static private PerunWebSession INSTANCE;

	/**
	 * Returns the instance of PerunWebSession
	 */

	static public PerunWebSession getInstance() {
		if(INSTANCE == null){
			INSTANCE = new PerunWebSession();
		}
		return INSTANCE;
	}

	/**
	 * Creates new instance of the Session
	 */
	private PerunWebSession() {
	}

	/**
	 * Returns the URL of the RPC
	 * @return URL
	 */
	public String getRpcUrl() {

		if(!rpcUrl.isEmpty()){
			return rpcUrl;
		}

		String rpcType = getRpcServer();
		if(rpcType == null){
			UiElements.generateAlert("Unable to find Perun server", "Path to Perun server can't be determined, you" +
					"probably used wrong URL.");
		}

		String modifier = PerunWebConstants.INSTANCE.perunRpcUrlModifier();
		if (modifier == null || modifier.equalsIgnoreCase("${gui.url.modifier}")) {
			rpcUrl = "/"+rpcType+"/rpc/jsonp/";
		} else {
			rpcUrl = "/"+rpcType+"/rpc"+modifier+"/jsonp/";
		}

		return rpcUrl;

	}

	/**
	 * Returns RPC type determined as first part of path in current page URL.
	 *
	 * @return RPC type
	 */
	public native String getRpcServer() /*-{
		return $wnd.RPC_SERVER;
	}-*/;

	/**
	 * Returns the UI elements
	 *
	 * @return return class which contains UI elements - menus,log,pages,tabs
	 */
	public UiElements getUiElements() {
		return this.uiElements;
	}

	/**
	 * Sets the UI elements handler class.
	 * Use only once during loading GUI!<commit_msg>GUI: Fix local run URL replacement in devel mode<commit_after>package cz.metacentrum.perun.webgui.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayInteger;
import cz.metacentrum.perun.webgui.client.mainmenu.MainMenu;
import cz.metacentrum.perun.webgui.client.resources.PerunEntity;
import cz.metacentrum.perun.webgui.json.GetEntityById;
import cz.metacentrum.perun.webgui.json.JsonCallbackEvents;
import cz.metacentrum.perun.webgui.model.*;
import cz.metacentrum.perun.webgui.tabs.TabManager;
import cz.metacentrum.perun.webgui.tabs.facilitiestabs.FacilityDetailTabItem;
import cz.metacentrum.perun.webgui.tabs.groupstabs.GroupDetailTabItem;
import cz.metacentrum.perun.webgui.tabs.securitytabs.SecurityTeamDetailTabItem;
import cz.metacentrum.perun.webgui.tabs.vostabs.VoDetailTabItem;

import java.util.ArrayList;

/**
 * Class for session of the Web GUI.
 * Session is passed to almost all other classes in constructor.
 *
 * Contains main Client classes like UiElements, WebGui, MainMenu.
 * Also stores user's information returned from RPC during login.
 * Provides authorization by keeping list of editable entities for user.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

public class PerunWebSession {

	// Main GUI classes
	private UiElements uiElements;
	private TabManager tabManager;
	private WebGui webGui;

	// User's authz data
	private PerunPrincipal perunPrincipal = null; // contain all users auth returned from RPC

	//Keepers of user's roles
	private boolean perunAdmin = false;
	private boolean groupAdmin = false;
	private boolean voAdmin = false;
	private boolean facilityAdmin = false;
	private boolean voObserver = false; // is not vo admin
	private boolean self = false; // is not admin
	private boolean securityAdmin = false;
	private boolean sponsor = false; // sponsor

	// User roles constants
	static public final String PERUN_ADMIN_PRINCIPAL_ROLE = "PERUNADMIN";
	static public final String GROUP_ADMIN_PRINCIPAL_ROLE = "GROUPADMIN";
	static public final String VO_ADMIN_PRINCIPAL_ROLE = "VOADMIN";
	static public final String FACILITY_ADMIN_PRINCIPAL_ROLE = "FACILITYADMIN";
	static public final String USER_ROLE = "SELF";
	static public final String VO_OBSERVER_PRINCIPAL_ROLE = "VOOBSERVER";
	static public final String SECURITY_ADMIN_PRINCIPAL_ROLE = "SECURITYADMIN";

	// Entities which can the user edit
	private ArrayList<Integer> editableGroups = new ArrayList<Integer>();
	private ArrayList<Integer> editableVos = new ArrayList<Integer>();
	private ArrayList<Integer> editableFacilities = new ArrayList<Integer>();
	private ArrayList<Integer> editableUsers = new ArrayList<Integer>();
	private ArrayList<Integer> editableSecTeams = new ArrayList<Integer>();
	private ArrayList<Integer> editableSponsoredUsers = new ArrayList<Integer>();


	// entities which user can view (Observer role)
	private ArrayList<Integer> viewableVos = new ArrayList<Integer>();

	// Currently active entities - user is editing them now
	private VirtualOrganization activeVo;
	private Group activeGroup;
	private Facility activeFacility;
	private User activeUser;
	private SecurityTeam activeSecurityTeam;

	// History of entities which user edited
	private ArrayList<GeneralObject> entitiesHistoryList = new ArrayList<GeneralObject>();

	private BasicOverlayType configuration;

	// RPC URL
	private String rpcUrl = "";

	// Only instance
	static private PerunWebSession INSTANCE;

	/**
	 * Returns the instance of PerunWebSession
	 */

	static public PerunWebSession getInstance() {
		if(INSTANCE == null){
			INSTANCE = new PerunWebSession();
		}
		return INSTANCE;
	}

	/**
	 * Creates new instance of the Session
	 */
	private PerunWebSession() {
	}

	/**
	 * Returns the URL of the RPC
	 * @return URL
	 */
	public String getRpcUrl() {

		if(!rpcUrl.isEmpty()){
			return rpcUrl;
		}

		String rpcType = getRpcServer();
		if(rpcType == null){
			UiElements.generateAlert("Unable to find Perun server", "Path to Perun server can't be determined, you" +
					"probably used wrong URL.");
		}

		String modifier = PerunWebConstants.INSTANCE.perunRpcUrlModifier();
		if (modifier == null || modifier.equalsIgnoreCase("@gui.url.modifier@")) {
			rpcUrl = "/"+rpcType+"/rpc/jsonp/";
		} else {
			rpcUrl = "/"+rpcType+"/rpc"+modifier+"/jsonp/";
		}

		return rpcUrl;

	}

	/**
	 * Returns RPC type determined as first part of path in current page URL.
	 *
	 * @return RPC type
	 */
	public native String getRpcServer() /*-{
		return $wnd.RPC_SERVER;
	}-*/;

	/**
	 * Returns the UI elements
	 *
	 * @return return class which contains UI elements - menus,log,pages,tabs
	 */
	public UiElements getUiElements() {
		return this.uiElements;
	}

	/**
	 * Sets the UI elements handler class.
	 * Use only once during loading GUI!
{'fraction_non_alphanumeric': 0.09718456725755996, 'fraction_numerical': 0.0, 'mean_word_length': 4.638447971781305, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399723', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2852, 'n_words': 841}