IDEMPIERE-356 Change Role.

This commit is contained in:
Heng Sin Low 2012-08-21 08:47:45 +08:00
parent d0f745b345
commit b3d83f4d7d
13 changed files with 243 additions and 33 deletions

View File

@ -0,0 +1,15 @@
-- 02-ago-2012 11:56:30 COT
-- Message for Change Role
INSERT INTO AD_Message (MsgType,MsgText,AD_Message_ID,EntityType,AD_Message_UU,Value,IsActive,Updated,CreatedBy,UpdatedBy,AD_Client_ID,AD_Org_ID,Created) VALUES ('I','Change Role',200010,'D','3270b7fc-6899-4c6d-9d76-bb754ecbee61','changeRole','Y',TO_DATE('2012-08-02 11:56:28','YYYY-MM-DD HH24:MI:SS'),0,0,0,0,TO_DATE('2012-08-02 11:56:28','YYYY-MM-DD HH24:MI:SS'))
;
-- 02-ago-2012 11:56:30 COT
INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy ) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=200010 AND NOT EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Message_ID=t.AD_Message_ID)
;
UPDATE AD_System
SET LastMigrationScriptApplied='860_IDEMPIERE-356.sql'
WHERE LastMigrationScriptApplied<'860_IDEMPIERE-356.sql'
OR LastMigrationScriptApplied IS NULL
;

View File

@ -0,0 +1,15 @@
-- 02-ago-2012 11:56:30 COT
-- Message for Change Role
INSERT INTO AD_Message (MsgType,MsgText,AD_Message_ID,EntityType,AD_Message_UU,Value,IsActive,Updated,CreatedBy,UpdatedBy,AD_Client_ID,AD_Org_ID,Created) VALUES ('I','Change Role',200010,'D','3270b7fc-6899-4c6d-9d76-bb754ecbee61','changeRole','Y',TO_TIMESTAMP('2012-08-02 11:56:28','YYYY-MM-DD HH24:MI:SS'),0,0,0,0,TO_TIMESTAMP('2012-08-02 11:56:28','YYYY-MM-DD HH24:MI:SS'))
;
-- 02-ago-2012 11:56:30 COT
INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy ) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=200010 AND NOT EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Message_ID=t.AD_Message_ID)
;
UPDATE AD_System
SET LastMigrationScriptApplied='860_IDEMPIERE-356.sql'
WHERE LastMigrationScriptApplied<'860_IDEMPIERE-356.sql'
OR LastMigrationScriptApplied IS NULL
;

View File

@ -72,6 +72,18 @@ import org.compiere.process.SvrProcess;
*/
public final class Env
{
public static final String AD_ROLE_ID = "#AD_Role_ID";
public static final String AD_USER_ID = "#AD_User_ID";
public static final String AD_ORG_ID = "#AD_Org_ID";
public static final String AD_CLIENT_ID = "#AD_Client_ID";
public static final String AD_ORG_NAME = "#AD_Org_Name";
public static final String M_WAREHOUSE_ID = "#M_Warehouse_ID";
private final static ContextProvider clientContextProvider = new DefaultContextProvider();
private static List<IEnvEventListener> eventListeners = new ArrayList<IEnvEventListener>();
@ -863,7 +875,7 @@ public final class Env
*/
public static int getAD_Client_ID (Properties ctx)
{
return Env.getContextAsInt(ctx, "#AD_Client_ID");
return Env.getContextAsInt(ctx, AD_CLIENT_ID);
} // getAD_Client_ID
/**
@ -873,7 +885,7 @@ public final class Env
*/
public static int getAD_Org_ID (Properties ctx)
{
return Env.getContextAsInt(ctx, "#AD_Org_ID");
return Env.getContextAsInt(ctx, AD_ORG_ID);
} // getAD_Org_ID
/**
@ -883,7 +895,7 @@ public final class Env
*/
public static int getAD_User_ID (Properties ctx)
{
return Env.getContextAsInt(ctx, "#AD_User_ID");
return Env.getContextAsInt(ctx, AD_USER_ID);
} // getAD_User_ID
/**
@ -893,7 +905,7 @@ public final class Env
*/
public static int getAD_Role_ID (Properties ctx)
{
return Env.getContextAsInt(ctx, "#AD_Role_ID");
return Env.getContextAsInt(ctx, AD_ROLE_ID);
} // getAD_Role_ID
/**************************************************************************

View File

@ -848,22 +848,22 @@ public class Login
if (m_ctx == null || org == null)
throw new IllegalArgumentException("Required parameter missing");
if (Env.getContext(m_ctx,"#AD_Client_ID").length() == 0)
if (Env.getContext(m_ctx,Env.AD_CLIENT_ID).length() == 0)
throw new UnsupportedOperationException("Missing Context #AD_Client_ID");
if (Env.getContext(m_ctx,"#AD_User_ID").length() == 0)
if (Env.getContext(m_ctx,Env.AD_USER_ID).length() == 0)
throw new UnsupportedOperationException("Missing Context #AD_User_ID");
if (Env.getContext(m_ctx,"#AD_Role_ID").length() == 0)
if (Env.getContext(m_ctx,Env.AD_ROLE_ID).length() == 0)
throw new UnsupportedOperationException("Missing Context #AD_Role_ID");
// Org Info - assumes that it is valid
Env.setContext(m_ctx, "#AD_Org_ID", org.getKey());
Env.setContext(m_ctx, "#AD_Org_Name", org.getName());
Env.setContext(m_ctx, Env.AD_ORG_ID, org.getKey());
Env.setContext(m_ctx, Env.AD_ORG_NAME, org.getName());
Ini.setProperty(Ini.P_ORG, org.getName());
// Warehouse Info
if (warehouse != null)
{
Env.setContext(m_ctx, "#M_Warehouse_ID", warehouse.getKey());
Env.setContext(m_ctx, Env.M_WAREHOUSE_ID, warehouse.getKey());
Ini.setProperty(Ini.P_WAREHOUSE, warehouse.getName());
}
@ -1476,4 +1476,46 @@ public class Login
return retValue;
} // getRoles
public KeyNamePair[] getClients() {
if (Env.getContext(m_ctx,"#AD_User_ID").length() == 0){
throw new UnsupportedOperationException("Missing Context #AD_User_ID");
}
int AD_User_ID = Env.getContextAsInt(m_ctx, "#AD_User_ID");
KeyNamePair[] retValue = null;
ArrayList<KeyNamePair> clientList = new ArrayList<KeyNamePair>();
StringBuffer sql= new StringBuffer("SELECT DISTINCT cli.AD_Client_ID, cli.Name, u.AD_User_ID, u.Name");
sql.append(" FROM AD_User_Roles ur")
.append(" INNER JOIN AD_User u on (ur.AD_User_ID=u.AD_User_ID)")
.append(" INNER JOIN AD_Client cli on (ur.AD_Client_ID=cli.AD_Client_ID)")
.append(" WHERE ur.IsActive='Y'")
.append(" AND u.IsActive='Y'")
.append(" AND u.AD_User_ID=?");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(),null);
pstmt.setInt(1, AD_User_ID);
rs = pstmt.executeQuery();
while (rs.next() && rs != null) {
int AD_Client_ID = rs.getInt(1);
String Name = rs.getString(2);
KeyNamePair p = new KeyNamePair(AD_Client_ID, Name);
clientList.add(p);
}
retValue = new KeyNamePair[clientList.size()];
clientList.toArray(retValue);
} catch (SQLException ex) {
log.log(Level.SEVERE, sql.toString(), ex);
retValue = null;
} finally {
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return retValue;
}
} // Login

View File

@ -19,9 +19,13 @@ package org.adempiere.webui;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.adempiere.util.ServerContext;
@ -44,6 +48,7 @@ import org.compiere.model.MUser;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
import org.compiere.util.Language;
import org.zkoss.web.Attributes;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
@ -73,12 +78,14 @@ import org.zkoss.zul.Window;
*/
public class AdempiereWebUI extends Window implements EventListener<Event>, IWebClient
{
public static final String APPLICATION_DESKTOP_KEY = "application.desktop";
/**
*
*/
private static final long serialVersionUID = 3744725245132180915L;
private static final long serialVersionUID = 5616730124927184116L;
private static final String SAVED_CONTEXT = "saved.context";
public static final String APPLICATION_DESKTOP_KEY = "application.desktop";
public static final String APP_NAME = "iDempiere";
@ -115,11 +122,18 @@ public class AdempiereWebUI extends Window implements EventListener<Event>, IWeb
public void onCreate()
{
this.getPage().setTitle(ThemeManager.getBrowserTitle());
Properties ctx = Env.getCtx();
langSession = Env.getContext(ctx, Env.LANGUAGE);
SessionManager.setSessionApplication(this);
Session session = Executions.getCurrent().getDesktop().getSession();
Map<String, Object>map = (Map<String, Object>) session.removeAttribute(SAVED_CONTEXT);
if (map != null && !map.isEmpty())
{
onChangeRole(map);
return;
}
Properties ctx = Env.getCtx();
langSession = Env.getContext(ctx, Env.LANGUAGE);
if (session.getAttribute(SessionContextListener.SESSION_CTX) == null || !SessionManager.isUserLoggedIn(ctx))
{
loginDesktop = new WLogin(this);
@ -379,6 +393,17 @@ public class AdempiereWebUI extends Window implements EventListener<Event>, IWeb
}
private void onChangeRole(Map<String, Object> map) {
Locale locale = (Locale) map.get("locale");
Properties properties = (Properties) map.get("context");
SessionManager.setSessionApplication(this);
loginDesktop = new WLogin(this);
loginDesktop.createPart(this.getPage());
loginDesktop.changeRole(locale, properties);
}
/**
* @param userId
* @return UserPreference
@ -398,4 +423,40 @@ public class AdempiereWebUI extends Window implements EventListener<Event>, IWeb
public static boolean isEventThreadEnabled() {
return eventThreadEnabled;
}
@Override
public void changeRole(MUser user) {
Properties properties = new Properties();
Env.setContext(properties, Env.AD_CLIENT_ID, Env.getAD_Client_ID(Env.getCtx()));
Env.setContext(properties, Env.AD_ORG_ID, Env.getAD_Org_ID(Env.getCtx()));
Env.setContext(properties, Env.AD_USER_ID, user.getAD_User_ID());
Env.setContext(properties, Env.AD_ROLE_ID, Env.getAD_Role_ID(Env.getCtx()));
Env.setContext(properties, Env.AD_ORG_NAME, Env.getContext(Env.getCtx(), Env.AD_ORG_NAME));
Env.setContext(properties, Env.M_WAREHOUSE_ID, Env.getContext(Env.getCtx(), Env.M_WAREHOUSE_ID));
Env.setContext(properties, BrowserToken.REMEMBER_ME, Env.getContext(Env.getCtx(), BrowserToken.REMEMBER_ME));
Env.setContext(properties, UserPreference.LANGUAGE_NAME, Env.getContext(Env.getCtx(), UserPreference.LANGUAGE_NAME));
Env.setContext(properties, Env.LANGUAGE, Env.getContext(Env.getCtx(), Env.LANGUAGE));
Env.setContext(properties, AEnv.LOCALE, Env.getContext(Env.getCtx(), AEnv.LOCALE));
Locale locale = (Locale) Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
appDesktop.logout();
HttpServletRequest httpRequest = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
Session session = Executions.getCurrent().getDesktop().getSession();
session.getAttributes().clear();
AEnv.logout();
((SessionCtrl)session).invalidateNow();
Env.getCtx().clear();
Map<String, Object> map = new HashMap<String, Object>();
map.put("context", properties);
map.put("locale", locale);
HttpSession newSession = httpRequest.getSession(true);
newSession.setAttribute(SAVED_CONTEXT, map);
properties.setProperty(SessionContextListener.SERVLET_SESSION_ID, newSession.getId());
Executions.sendRedirect("index.zul");
}
}

View File

@ -14,6 +14,7 @@ package org.adempiere.webui;
import org.adempiere.webui.desktop.IDesktop;
import org.adempiere.webui.util.UserPreference;
import org.compiere.model.MUser;
/**
*
@ -50,5 +51,10 @@ public interface IWebClient {
* @return UserPreference
*/
public UserPreference getUserPreference();
/**
* change Role
*/
public void changeRole(MUser user);
}

View File

@ -14,6 +14,9 @@
package org.adempiere.webui;
import java.util.Locale;
import java.util.Properties;
import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.part.AbstractUIPart;
import org.adempiere.webui.theme.ITheme;
@ -47,6 +50,7 @@ public class WLogin extends AbstractUIPart
private IWebClient app;
private Borderlayout layout;
private Window browserWarningWindow;
private LoginWindow loginWindow;
public WLogin(IWebClient app)
{
@ -77,7 +81,7 @@ public class WLogin extends AbstractUIPart
vb.setAlign("center");
vb.setStyle("background-color: transparent;");
LoginWindow loginWindow = new LoginWindow(app);
loginWindow = new LoginWindow(app);
loginWindow.setParent(vb);
if (!AEnv.isBrowserSupported())
@ -181,4 +185,8 @@ public class WLogin extends AbstractUIPart
public Component getComponent() {
return layout;
}
public void changeRole(Locale locale, Properties properties) {
loginWindow.changeRole(locale, properties);
}
}

View File

@ -492,7 +492,7 @@ public class LoginPanel extends Window implements EventListener
Language language = findLanguage(langName);
Env.setContext(ctx, UserPreference.LANGUAGE_NAME, language.getName()); // Elaine 2009/02/06
wndLogin.loginOk(userId, userPassword, chkSelectRole.isChecked(), clientsKNPairs);
wndLogin.loginOk(userId, chkSelectRole.isChecked(), clientsKNPairs);
Locale locale = language.getLocale();
currSess.setAttribute(Attributes.PREFERRED_LOCALE, locale);

View File

@ -477,6 +477,20 @@ public class RolePanel extends Window implements EventListener, Deferrable
Env.setContext(m_ctx, "#SalesRep_ID", user.getAD_User_ID() );
}
}
public void changeRole(Properties ctx) {
int AD_Client_ID = Env.getAD_Client_ID(ctx);
lstClient.setValue(AD_Client_ID);
updateRoleList();
int AD_Role_ID = Env.getAD_Role_ID(ctx);
lstRole.setValue(AD_Role_ID);
updateOrganisationList();
int AD_Org_ID = Env.getAD_Org_ID(ctx);
lstOrganisation.setValue(AD_Org_ID);
updateWarehouseList();
int M_Warehouse_ID = Env.getContextAsInt(ctx, Env.M_WAREHOUSE_ID);
lstWarehouse.setValue(M_Warehouse_ID);
}
/**
* validate Roles

View File

@ -46,13 +46,15 @@ import org.zkoss.zul.Vbox;
*/
public class UserPanel extends Vbox implements EventListener<Event>
{
private static final long serialVersionUID = -45350536628290540L;
/**
*
*/
private static final long serialVersionUID = 6605639697034780065L;
private Properties ctx;
private ToolBarButton logout = new ToolBarButton();
private ToolBarButton role = new ToolBarButton();
private ToolBarButton changeRole = new ToolBarButton();
private ToolBarButton preference = new ToolBarButton();
private Label lblUserNameValue = new Label();
@ -76,7 +78,8 @@ public class UserPanel extends Vbox implements EventListener<Event>
Vbox vbox = new Vbox();
this.appendChild(vbox);
lblUserNameValue.setValue(getUserName() + "@" + getClientName() + "." + getOrgName());
lblUserNameValue.setValue(getUserName() + "@" + getClientName() + "." + getOrgName()+"/"+this.getRoleName());
lblUserNameValue.addEventListener(Events.ON_CLICK, this);
lblUserNameValue.setStyle("text-align:right");
LayoutUtils.addSclass("desktop-header-font", lblUserNameValue);
vbox.appendChild(lblUserNameValue);
@ -94,11 +97,11 @@ public class UserPanel extends Vbox implements EventListener<Event>
sep.setBar(true);
sep.setParent(hbox);
role.setLabel(this.getRoleName());
role.addEventListener(Events.ON_CLICK, this);
role.setStyle("text-align:right");
LayoutUtils.addSclass("desktop-header-font", role);
role.setParent(hbox);
changeRole.setLabel(Msg.getMsg(Env.getCtx(), "changeRole"));
changeRole.addEventListener(Events.ON_CLICK, this);
changeRole.setStyle("text-align:right");
LayoutUtils.addSclass("desktop-header-font", changeRole);
changeRole.setParent(hbox);
sep = new Separator("vertical");
sep.setBar(true);
@ -151,12 +154,17 @@ public class UserPanel extends Vbox implements EventListener<Event>
{
SessionManager.logoutSession();
}
else if (role == event.getTarget())
else if (lblUserNameValue == event.getTarget())
{
String roleInfo = MRole.getDefault().toStringX(Env.getCtx());
roleInfo = roleInfo.replace(Env.NL, "<br>");
Messagebox.showDialog(roleInfo, Msg.getMsg(ctx, "RoleInfo"), Messagebox.OK, Messagebox.INFORMATION);
}
else if (changeRole == event.getTarget())
{
MUser user = MUser.get(ctx);
SessionManager.changeRole(user);
}
else if (preference == event.getTarget())
{
if (preferencePopup != null)

View File

@ -194,10 +194,14 @@ public class SessionContextListener implements ExecutionInit,
public void complete(Component comp, Event evt) throws Exception
{
//in servlet thread
if (ServerContext.getCurrentInstance().isEmpty() || !isContextValid())
{
setupExecutionContextFromSession(Executions.getCurrent());
}
try {
if (ServerContext.getCurrentInstance().isEmpty() || !isContextValid())
{
setupExecutionContextFromSession(Executions.getCurrent());
}
} catch (IllegalStateException e) {
//safe to ignore session already invalidated
}
}
/**

View File

@ -22,6 +22,7 @@ import java.util.Properties;
import org.adempiere.webui.AdempiereWebUI;
import org.adempiere.webui.IWebClient;
import org.adempiere.webui.desktop.IDesktop;
import org.compiere.model.MUser;
import org.compiere.util.Env;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Execution;
@ -94,4 +95,11 @@ public class SessionManager
if (app != null)
app.logout();
}
public static void changeRole(MUser user){
IWebClient app = getSessionApplication();
if (app != null)
app.changeRole(user);
}
}

View File

@ -23,14 +23,19 @@
package org.adempiere.webui.window;
import java.util.Locale;
import java.util.Properties;
import org.adempiere.webui.IWebClient;
import org.adempiere.webui.component.FWindow;
import org.adempiere.webui.panel.LoginPanel;
import org.adempiere.webui.panel.RolePanel;
import org.compiere.model.MUser;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Login;
import org.zkoss.util.Locales;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
@ -75,7 +80,7 @@ public class LoginWindow extends FWindow implements EventListener
pnlLogin = new LoginPanel(ctx, this);
}
public void loginOk(String userName, String password, boolean show, KeyNamePair[] clientsKNPairs)
public void loginOk(String userName, boolean show, KeyNamePair[] clientsKNPairs)
{
pnlRole = new RolePanel(ctx, this, userName, show, clientsKNPairs);
this.getChildren().clear();
@ -118,4 +123,16 @@ public class LoginWindow extends FWindow implements EventListener
}
}
}
public void changeRole(Locale locale, Properties ctx)
{
Env.setCtx(ctx);
getDesktop().getSession().setAttribute(Attributes.PREFERRED_LOCALE, locale);
Locales.setThreadLocal(locale);
Login login = new Login(Env.getCtx());
loginOk(MUser.getNameOfUser(Env.getAD_User_ID(ctx)), true, login.getClients());
getDesktop().getSession().setAttribute("Check_AD_User_ID", Env.getAD_User_ID(ctx));
pnlRole.changeRole(ctx);
}
}