From 05b105028c9ca83870f4623c0bd93de0d8415dc7 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Tue, 30 Oct 2012 18:13:59 +0100
Subject: [PATCH] runAs Feature

---
 hsarweb/src/de/hsadmin/web/HomeModule.java         |    2 
 hsarweb/src/de/hsadmin/web/UnixUserModule.java     |    2 
 hsarweb/src/de/hsadmin/web/EMailAddressModule.java |    2 
 hsarweb/src/de/hsadmin/web/AbstractModule.java     |   57 ++++++++++++++++++
 hsarweb/src/de/hsadmin/web/DatabaseUserModule.java |    2 
 hsarweb/conf/WEB-INF/web.xml                       |   11 ++-
 hsarweb/src/de/hsadmin/web/DomainModule.java       |    2 
 hsarweb/src/de/hsadmin/web/MainApplication.java    |   38 ++++++++++--
 hsarweb/src/de/hsadmin/web/Remote.java             |    4 
 hsarweb/src/de/hsadmin/web/EMailAliasModule.java   |    2 
 hsarweb/src/de/hsadmin/web/DatabaseModule.java     |    2 
 11 files changed, 104 insertions(+), 20 deletions(-)

diff --git a/hsarweb/conf/WEB-INF/web.xml b/hsarweb/conf/WEB-INF/web.xml
index e9a46ec..2acd982 100644
--- a/hsarweb/conf/WEB-INF/web.xml
+++ b/hsarweb/conf/WEB-INF/web.xml
@@ -57,6 +57,7 @@
 		</init-param>
 	</filter>
 	
+
     <filter-mapping>
         <filter-name>CAS Validation Filter</filter-name>
 		<url-pattern>/*</url-pattern>
@@ -66,7 +67,7 @@
         <filter-name>CAS Authentication Filter</filter-name>
 		<url-pattern>/*</url-pattern>
     </filter-mapping>
-		 
+
 	<servlet>
 		<servlet-name>Logout Servlet</servlet-name>
 		<servlet-class>de.hsadmin.logout.LogoutServlet</servlet-class>
@@ -93,11 +94,15 @@
 	
 	<servlet-mapping>
 		<servlet-name>HSAdmin Web</servlet-name>
-		<url-pattern>/*</url-pattern>
+		<url-pattern>/MainApplication/*</url-pattern>
+	</servlet-mapping>
+	
+	<servlet-mapping>
+		<servlet-name>HSAdmin Web</servlet-name>
+		<url-pattern>/VAADIN/*</url-pattern>
 	</servlet-mapping>
 	
 	<welcome-file-list>
-		<welcome-file>index.html</welcome-file>
 		<welcome-file>index.jsp</welcome-file>
 	</welcome-file-list>
 </web-app>
\ No newline at end of file
diff --git a/hsarweb/src/de/hsadmin/web/AbstractModule.java b/hsarweb/src/de/hsadmin/web/AbstractModule.java
index 4b37670..537f414 100644
--- a/hsarweb/src/de/hsadmin/web/AbstractModule.java
+++ b/hsarweb/src/de/hsadmin/web/AbstractModule.java
@@ -4,12 +4,15 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import com.vaadin.data.Property;
+import com.vaadin.data.Property.ValueChangeEvent;
 import com.vaadin.terminal.Sizeable;
 import com.vaadin.terminal.ThemeResource;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.Form;
 import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Select;
 import com.vaadin.ui.VerticalLayout;
 import com.vaadin.ui.Window;
 import com.vaadin.ui.Button.ClickEvent;
@@ -28,23 +31,73 @@
 	private VerticalLayout layout;
 	private Component component;
 	private ComponentFactory componentFactory;
+	private Select selRunAs;
 
 	public Component getComponent() {
 		return layout;
 	}
 
 	public void reload() throws HsarwebException {
+		if (selRunAs != null) {
+			selRunAs.select(application.getRunAs());
+			selRunAs.setScrollToSelectedItem(true);
+		}
 		componentFactory.loadData();
 	}
 
-	private void initLayout() {
+	private void initLayout()  throws HsarwebException {
 		layout = new VerticalLayout();
 		layout.setSizeFull();
 		final Module thisModule = this;
 		final ModuleConfig moduleConfig = getModuleConfig();
 		final LocaleConfig localeConfig = application.getLocaleConfig();
-		if (this instanceof SearchAble || this instanceof InsertAble) {
+		if (this instanceof SearchAble || this instanceof InsertAble ||
+				!("USER".equals(application.getLoginUserRole()) || "NONE".equals(application.getLoginUserRole()))) {
 			HorizontalLayout toolbar = new HorizontalLayout();
+			if (!("USER".equals(application.getLoginUserRole()) || "NONE".equals(application.getLoginUserRole()))) {
+				selRunAs = new Select();
+				selRunAs.setWidth(100.0f, Sizeable.UNITS_PIXELS);
+				selRunAs.setImmediate(true);
+				selRunAs.setNewItemsAllowed(false);
+				selRunAs.setNullSelectionAllowed(false);
+				if (!application.getLoginUserRole().startsWith("PAC")) {
+//					if (application.getLoginUserRole().startsWith("PAC")) {
+//						
+//					}
+					selRunAs.addItem(application.getLogin());
+					Object custListObj = application.getRemote().callSearch("member", new HashMap<String, String>());
+					if (custListObj instanceof Object[]) {
+						Object[] custList = (Object[]) custListObj;
+						for (Object custObj : custList) {
+							if (custObj instanceof Map<?, ?>) {
+								Map<?, ?> custHash = (Map<?, ?>)custObj;
+								selRunAs.addItem(custHash.get("membercode"));
+							}
+						}
+					}
+				}
+				Object pacListObj = application.getRemote().callSearch("pac", new HashMap<String, String>());
+				if (pacListObj instanceof Object[]) {
+					Object[] pacList = (Object[]) pacListObj;
+					for (Object pacObj : pacList) {
+						if (pacObj instanceof Map<?, ?>) {
+							Map<?, ?> pacHash = (Map<?, ?>)pacObj;
+							selRunAs.addItem(pacHash.get("name"));
+						}
+					}
+				}
+				selRunAs.select(application.getRunAs());
+				selRunAs.setScrollToSelectedItem(true);
+				selRunAs.addListener(new Property.ValueChangeListener() {
+					private static final long serialVersionUID = 1L;
+					@Override
+					public void valueChange(ValueChangeEvent event) {
+						Property property = event.getProperty();
+						application.setRunAs(property.getValue().toString());
+					}
+				});
+				toolbar.addComponent(selRunAs);
+			}
 			if (this instanceof InsertAble) {
 				Button btNew = new Button(moduleConfig.getLabel("new"));
 				ThemeResource icon = new ThemeResource("../runo/icons/16/document-add.png");
diff --git a/hsarweb/src/de/hsadmin/web/DatabaseModule.java b/hsarweb/src/de/hsadmin/web/DatabaseModule.java
index 3ccea15..8a70b38 100644
--- a/hsarweb/src/de/hsadmin/web/DatabaseModule.java
+++ b/hsarweb/src/de/hsadmin/web/DatabaseModule.java
@@ -29,7 +29,7 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig(getModuleIdent(), application.getLocale());
-		String login = application.getLogin();
+		String login = application.getRunAs();
 		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
 		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
 		idProp.setReadOnly(true);
diff --git a/hsarweb/src/de/hsadmin/web/DatabaseUserModule.java b/hsarweb/src/de/hsadmin/web/DatabaseUserModule.java
index c42d5a5..6f80135 100644
--- a/hsarweb/src/de/hsadmin/web/DatabaseUserModule.java
+++ b/hsarweb/src/de/hsadmin/web/DatabaseUserModule.java
@@ -24,7 +24,7 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig(getModuleIdent(), application.getLocale());
-		String login = application.getLogin();
+		String login = application.getRunAs();
 		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
 		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
 		idProp.setReadOnly(true);
diff --git a/hsarweb/src/de/hsadmin/web/DomainModule.java b/hsarweb/src/de/hsadmin/web/DomainModule.java
index ef39e5f..d1ddfef 100644
--- a/hsarweb/src/de/hsadmin/web/DomainModule.java
+++ b/hsarweb/src/de/hsadmin/web/DomainModule.java
@@ -23,7 +23,7 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig("domain", application.getLocale());
-		String login = application.getLogin();
+		String login = application.getRunAs();
 		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
 		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
 		idProp.setReadOnly(true);
diff --git a/hsarweb/src/de/hsadmin/web/EMailAddressModule.java b/hsarweb/src/de/hsadmin/web/EMailAddressModule.java
index 8bdc8d5..3e99c6f 100644
--- a/hsarweb/src/de/hsadmin/web/EMailAddressModule.java
+++ b/hsarweb/src/de/hsadmin/web/EMailAddressModule.java
@@ -22,7 +22,7 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig("emailaddress", application.getLocale());
-		String login = application.getLogin();
+		String login = application.getRunAs();
 		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
 		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
 		idProp.setReadOnly(true);
diff --git a/hsarweb/src/de/hsadmin/web/EMailAliasModule.java b/hsarweb/src/de/hsadmin/web/EMailAliasModule.java
index 21769e4..51a4139 100644
--- a/hsarweb/src/de/hsadmin/web/EMailAliasModule.java
+++ b/hsarweb/src/de/hsadmin/web/EMailAliasModule.java
@@ -22,7 +22,7 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig("emailalias", application.getLocale());
-		String login = application.getLogin();
+		String login = application.getRunAs();
 		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
 		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
 		idProp.setReadOnly(true);
diff --git a/hsarweb/src/de/hsadmin/web/HomeModule.java b/hsarweb/src/de/hsadmin/web/HomeModule.java
index d71e536..e6bc473 100644
--- a/hsarweb/src/de/hsadmin/web/HomeModule.java
+++ b/hsarweb/src/de/hsadmin/web/HomeModule.java
@@ -95,7 +95,7 @@
 		ThemeResource icon = new ThemeResource(moduleConfig.getLabel("change_password_icon"));
 		button.setIcon(icon);
 		Map<String, String> whereHash = new HashMap<String, String>();
-		whereHash.put("name", application.getLogin());
+		whereHash.put("name", application.getRunAs());
 		Long key = -1L;
 		try {
 			Object object = application.getRemote().callSearch(moduleConfig.getRemoteName(), whereHash);
diff --git a/hsarweb/src/de/hsadmin/web/MainApplication.java b/hsarweb/src/de/hsadmin/web/MainApplication.java
index 5a883c9..373238f 100644
--- a/hsarweb/src/de/hsadmin/web/MainApplication.java
+++ b/hsarweb/src/de/hsadmin/web/MainApplication.java
@@ -46,6 +46,9 @@
 	private Remote remote;
 	private Map<String, Module> modules;
 	private Locale requestLocale;
+	private String role = "NONE";
+	private String runAs = null;
+	private TabSheet tabSheet;
 
 
 	@Override
@@ -56,7 +59,6 @@
 		}
 		localeConfig = new LocaleConfig(locale, "main");
 		remote = new Remote(this);
-		String role = "NONE";
 		try {
 			Object rolesArrayObj = remote.callSearch("role", null);
 			if (rolesArrayObj != null && rolesArrayObj instanceof Object[]) {
@@ -69,8 +71,8 @@
 			showSystemException(e);
 		}
 		Window mainWindow = new Window(localeConfig.getText("applicationtitle"));
-		TabSheet tabs = new TabSheet();
-		tabs.setSizeFull();
+		tabSheet = new TabSheet();
+		tabSheet.setSizeFull();
 		String modulesParamString = localeConfig.getText("modules." + role);
 		modules = new HashMap<String, Module>();
 		Module firstModule = null;
@@ -84,13 +86,13 @@
 				ModuleConfig moduleConfig = module.getModuleConfig();
 				String label = moduleConfig.getLabel("moduletitle");
 				modules.put(label, module);
-				tabs.addTab((Component) module.getComponent(), label, new ThemeResource(moduleConfig.getLabel("moduleicon")));
+				tabSheet.addTab((Component) module.getComponent(), label, new ThemeResource(moduleConfig.getLabel("moduleicon")));
 			} catch (Exception e) {
 				showSystemException(e);
 			}
 		}
-		tabs.addListener(this);
-		mainWindow.setContent(tabs);
+		tabSheet.addListener(this);
+		mainWindow.setContent(tabSheet);
 		setMainWindow(mainWindow);
 		setErrorHandler(new Terminal.ErrorListener() {
 			private static final long serialVersionUID = 1L;
@@ -104,6 +106,10 @@
 		} catch (HsarwebException e) {
 			showSystemException(e);
 		}
+	}
+	
+	public String getLoginUserRole() {
+		return role;
 	}
 
 	public String getProxyTicket() {
@@ -186,4 +192,24 @@
 		getMainWindow().showNotification("System-Fehler", "<br />Bitte informieren Sie den Support<br/ >" + e.getMessage(), Notification.TYPE_ERROR_MESSAGE);			
 	}
 
+	public String getRunAs() {
+		if (runAs == null) {
+			return getLogin();
+		}
+		return runAs;
+	}
+
+	public void setRunAs(String runAs) {
+		this.runAs = runAs;
+		Component selectedTab = tabSheet.getSelectedTab();
+		Tab tab = tabSheet.getTab(selectedTab);
+		Module module = modules.get(tab.getCaption());
+		try {
+			module.reload();
+		} catch (HsarwebException e) {
+			e.printStackTrace();
+			showSystemException(e);
+		}
+	}
+
 }
diff --git a/hsarweb/src/de/hsadmin/web/Remote.java b/hsarweb/src/de/hsadmin/web/Remote.java
index 512d7de..b9771a6 100644
--- a/hsarweb/src/de/hsadmin/web/Remote.java
+++ b/hsarweb/src/de/hsadmin/web/Remote.java
@@ -36,7 +36,7 @@
 
 	private Object xmlrpcCall(String module, String operation, Map<String, String> param1) throws HsarwebException {
 		Object[] params = new Object[3];
-		params[0] = app.getLogin();
+		params[0] = app.getRunAs();
 		params[1] = app.getProxyTicket();
 		params[2] = param1 != null ? param1 : new HashMap<String, String>();
 		return xmlrpcCall(module + "." + operation, params);
@@ -44,7 +44,7 @@
 
 	private Object xmlrpcCall(String module, String operation, Map<String, String> param1, Map<String, String> param2) throws HsarwebException {
 		Object[] params = new Object[4];
-		params[0] = app.getLogin();
+		params[0] = app.getRunAs();
 		params[1] = app.getProxyTicket();
 		params[2] = param1 != null ? param1 : new HashMap<String, String>();
 		params[3] = param2 != null ? param2 : new HashMap<String, String>();
diff --git a/hsarweb/src/de/hsadmin/web/UnixUserModule.java b/hsarweb/src/de/hsadmin/web/UnixUserModule.java
index 09732f5..7cc59e8 100644
--- a/hsarweb/src/de/hsadmin/web/UnixUserModule.java
+++ b/hsarweb/src/de/hsadmin/web/UnixUserModule.java
@@ -22,7 +22,7 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig("user", application.getLocale());
-		String login = application.getLogin();
+		String login = application.getRunAs();
 		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
 		PropertyConfig pacProp = new PropertyConfig(moduleConfig, "pac", String.class, PropertyTableColumn.HIDDEN, new SelectPropertyFieldFactory());
 		pacProp.setSelectValues(new PropertySelectValues() {

--
Gitblit v1.9.0-SNAPSHOT