Search in sources :

Example 1 with MSession

use of org.compiere.model.MSession in project adempiere by adempiere.

the class AdempiereWebUI method loginCompleted.

/* (non-Javadoc)
	 * @see org.adempiere.webui.IWebClient#loginCompleted()
	 */
public void loginCompleted() {
    Properties ctx = Env.getCtx();
    // to reload preferences when the user refresh the browser
    userPreference = loadUserPreference(Env.getAD_User_ID(ctx));
    // Set the theme according to the user preferences.  If the theme changes, the page will
    // be reloaded again - so do this first thing.
    int theme_id = userPreference.getPropertyAsInt(UserPreference.P_ZK_THEME_PREFERENCE);
    if (theme_id > 0) {
        // If theme_id == 0, don't bother setting a new MTheme. Just use the current theme.
        MTheme theme = MTheme.get(ctx, theme_id);
        if (ThemeUtils.makeCurrent(theme)) {
            // Make the theme active and reload the page.
            return;
        }
    }
    ThemeUtils.addBrowserIconAndTitle(this.getPage());
    if (loginDesktop != null) {
        loginDesktop.detach();
        loginDesktop = null;
    }
    String langLogin = Env.getContext(ctx, Env.LANGUAGE);
    if (langLogin == null || langLogin.length() <= 0) {
        langLogin = langSession;
        Env.setContext(ctx, Env.LANGUAGE, langSession);
    }
    // Validate language
    Language language = Language.getLanguage(langLogin);
    String locale = Env.getContext(ctx, AEnv.LOCALE);
    if (locale != null && locale.length() > 0 && !language.getLocale().toString().equals(locale)) {
        String adLanguage = language.getAD_Language();
        Language tmp = Language.getLanguage(locale);
        language = new Language(tmp.getName(), adLanguage, tmp.getLocale(), tmp.isDecimalPoint(), tmp.getDateFormat().toPattern(), tmp.getMediaSize());
    } else {
        Language tmp = language;
        language = new Language(tmp.getName(), tmp.getAD_Language(), tmp.getLocale(), tmp.isDecimalPoint(), tmp.getDateFormat().toPattern(), tmp.getMediaSize());
    }
    Env.verifyLanguage(ctx, language);
    //Bug
    Env.setContext(ctx, Env.LANGUAGE, language.getAD_Language());
    //	Create adempiere Session - user id in ctx
    Execution exec = Executions.getCurrent();
    Session currSess = exec.getDesktop().getSession();
    HttpSession httpSess = (HttpSession) currSess.getNativeSession();
    MSession mSession = MSession.get(ctx, exec.getRemoteAddr(), exec.getRemoteHost(), httpSess.getId());
    //enable full interface, relook into this when doing preference
    Env.setContext(ctx, "#ShowTrl", true);
    Env.setContext(ctx, "#ShowAcct", MRole.getDefault().isShowAcct());
    Env.setContext(ctx, "#ShowAdvanced", true);
    //auto commit user preference
    String autoCommit = userPreference.getProperty(UserPreference.P_AUTO_COMMIT);
    Env.setAutoCommit(ctx, "true".equalsIgnoreCase(autoCommit) || "y".equalsIgnoreCase(autoCommit));
    //auto new user preference
    String autoNew = userPreference.getProperty(UserPreference.P_AUTO_NEW);
    Env.setAutoNew(ctx, "true".equalsIgnoreCase(autoNew) || "y".equalsIgnoreCase(autoNew));
    IDesktop d = (IDesktop) currSess.getAttribute("application.desktop");
    if (d != null && d instanceof IDesktop) {
        ExecutionCarryOver eco = (ExecutionCarryOver) currSess.getAttribute(EXECUTION_CARRYOVER_SESSION_KEY);
        if (eco != null) {
            //try restore
            try {
                appDesktop = (IDesktop) d;
                ExecutionCarryOver current = new ExecutionCarryOver(this.getPage().getDesktop());
                ExecutionCtrl ctrl = ExecutionsCtrl.getCurrentCtrl();
                Visualizer vi = ctrl.getVisualizer();
                eco.carryOver();
                Collection<Component> rootComponents = new ArrayList<Component>();
                try {
                    ctrl = ExecutionsCtrl.getCurrentCtrl();
                    ((DesktopCtrl) Executions.getCurrent().getDesktop()).setVisualizer(vi);
                    //detach root component from old page
                    Page page = appDesktop.getComponent().getPage();
                    Collection<?> collection = page.getRoots();
                    Object[] objects = new Object[0];
                    objects = collection.toArray(objects);
                    for (Object obj : objects) {
                        if (obj instanceof Component) {
                            ((Component) obj).detach();
                            rootComponents.add((Component) obj);
                        }
                    }
                    appDesktop.getComponent().detach();
                    DesktopCache desktopCache = ((SessionCtrl) currSess).getDesktopCache();
                    if (desktopCache != null)
                        desktopCache.removeDesktop(Executions.getCurrent().getDesktop());
                } catch (Exception e) {
                    appDesktop = null;
                } finally {
                    eco.cleanup();
                    current.carryOver();
                }
                if (appDesktop != null) {
                    //re-attach root components
                    for (Component component : rootComponents) {
                        try {
                            component.setPage(this.getPage());
                        } catch (UiException e) {
                        // e.printStackTrace();
                        // an exception is thrown here when refreshing the page, it seems is harmless to catch and ignore it
                        // i.e.: org.zkoss.zk.ui.UiException: Not unique in the ID space of [Page z_kg_0]: zk_comp_2
                        }
                    }
                    appDesktop.setPage(this.getPage());
                    currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, current);
                }
                currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop());
            } catch (Throwable t) {
                //restore fail
                appDesktop = null;
            }
        }
    }
    if (appDesktop == null) {
        //create new desktop
        createDesktop();
        appDesktop.setClientInfo(clientInfo);
        appDesktop.createPart(this.getPage());
        currSess.setAttribute("application.desktop", appDesktop);
        ExecutionCarryOver eco = new ExecutionCarryOver(this.getPage().getDesktop());
        currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, eco);
        currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop());
    }
    if ("Y".equalsIgnoreCase(Env.getContext(ctx, BrowserToken.REMEMBER_ME)) && MSystem.isZKRememberUserAllowed()) {
        MUser user = MUser.get(ctx);
        BrowserToken.save(mSession, user);
    } else {
        BrowserToken.remove();
    }
}
Also used : ArrayList(java.util.ArrayList) MTheme(org.compiere.model.MTheme) MSession(org.compiere.model.MSession) Page(org.zkoss.zk.ui.Page) SessionCtrl(org.zkoss.zk.ui.sys.SessionCtrl) Properties(java.util.Properties) ExecutionCtrl(org.zkoss.zk.ui.sys.ExecutionCtrl) Execution(org.zkoss.zk.ui.Execution) Language(org.compiere.util.Language) DesktopCtrl(org.zkoss.zk.ui.sys.DesktopCtrl) Component(org.zkoss.zk.ui.Component) MUser(org.compiere.model.MUser) HttpSession(javax.servlet.http.HttpSession) UiException(org.zkoss.zk.ui.UiException) IDesktop(org.adempiere.webui.desktop.IDesktop) UiException(org.zkoss.zk.ui.UiException) ExecutionCarryOver(org.zkoss.zk.ui.impl.ExecutionCarryOver) Visualizer(org.zkoss.zk.ui.sys.Visualizer) DesktopCache(org.zkoss.zk.ui.sys.DesktopCache) MSession(org.compiere.model.MSession) HttpSession(javax.servlet.http.HttpSession) Session(org.zkoss.zk.ui.Session)

Example 2 with MSession

use of org.compiere.model.MSession in project adempiere by adempiere.

the class AdempiereWebUI method logout.

/* (non-Javadoc)
	 * @see org.adempiere.webui.IWebClient#logout()
	 */
public void logout() {
    appDesktop.logout();
    Executions.getCurrent().getDesktop().getSession().getAttributes().clear();
    MSession mSession = MSession.get(Env.getCtx(), false);
    if (mSession != null) {
        mSession.logout();
    }
    SessionManager.clearSession();
    super.getChildren().clear();
    Page page = this.getPage();
    page.removeComponents();
    Executions.sendRedirect("index.zul");
}
Also used : MSession(org.compiere.model.MSession) Page(org.zkoss.zk.ui.Page)

Example 3 with MSession

use of org.compiere.model.MSession in project adempiere by adempiere.

the class AdempiereServerMgr method startEnvironment.

/**
	 * 	Start Environment
	 *	@return true if started
	 */
private boolean startEnvironment() {
    Adempiere.startup(false);
    log.info("");
    //	Set Session
    MSession session = MSession.get(getCtx(), true);
    session.setWebStoreSession(false);
    session.setWebSession("Server");
    session.saveEx();
    //
    return true;
}
Also used : MSession(org.compiere.model.MSession)

Example 4 with MSession

use of org.compiere.model.MSession in project adempiere by adempiere.

the class LoginPanel method init.

private void init() {
    //this.setContentSclass(ITheme.LOGIN_WINDOW_CLASS);
    Div div = new Div();
    ThemeUtils.addSclass("ad-loginpanel-header", div);
    // TODO - localization
    Label label = new Label("Login");
    ThemeUtils.addSclass("ad-loginpanel-header-text", label);
    div.appendChild(label);
    this.appendChild(div);
    Table table = new Table();
    table.setId("grdLogin");
    ThemeUtils.addSclass("ad-loginpanel-body", table);
    this.appendChild(table);
    Tr tr = new Tr();
    table.appendChild(tr);
    Td td = new Td();
    ThemeUtils.addSclass("ad-loginpanel-header-logo", td);
    tr.appendChild(td);
    td.setDynamicProperty("colspan", "2");
    Image image = new Image();
    image.setSrc(ThemeUtils.getLargeLogo());
    ThemeUtils.addSclass("ad-loginpanel-header-logo", image);
    td.appendChild(image);
    tr = new Tr();
    tr.setId("rowUser");
    table.appendChild(tr);
    td = new Td();
    tr.appendChild(td);
    ThemeUtils.addSclass("login-label", td);
    td.appendChild(lblUserId);
    td = new Td();
    ThemeUtils.addSclass("login-field", td);
    tr.appendChild(td);
    td.appendChild(txtUserId);
    tr = new Tr();
    tr.setId("rowPassword");
    table.appendChild(tr);
    td = new Td();
    tr.appendChild(td);
    ThemeUtils.addSclass("login-label", td);
    td.appendChild(lblPassword);
    td = new Td();
    ThemeUtils.addSclass("login-field", td);
    tr.appendChild(td);
    td.appendChild(txtPassword);
    tr = new Tr();
    tr.setId("rowLanguage");
    table.appendChild(tr);
    td = new Td();
    tr.appendChild(td);
    ThemeUtils.addSclass("login-label", td);
    td.appendChild(lblLanguage);
    td = new Td();
    ThemeUtils.addSclass("login-field", td);
    tr.appendChild(td);
    td.appendChild(lstLanguage);
    if (MSystem.isZKRememberUserAllowed()) {
        tr = new Tr();
        tr.setId("rowRememberMe");
        table.appendChild(tr);
        td = new Td();
        tr.appendChild(td);
        ThemeUtils.addSclass("login-label", td);
        td.appendChild(new Label(""));
        td = new Td();
        ThemeUtils.addSclass("login-field", td);
        tr.appendChild(td);
        td.appendChild(chkRememberMe);
    }
    div = new Div();
    ThemeUtils.addSclass("ad-loginpanel-footer", div);
    ConfirmPanel pnlButtons = new ConfirmPanel(false);
    pnlButtons.addActionListener(this);
    ThemeUtils.addSclass("ad-loginpanel-footer-pnl", pnlButtons);
    pnlButtons.getButton(ConfirmPanel.A_OK).setSclass("login-btn");
    div.appendChild(pnlButtons);
    this.appendChild(div);
    this.addEventListener(TokenEvent.ON_USER_TOKEN, new EventListener<Event>() {

        @Override
        public void onEvent(Event event) throws Exception {
            String[] data = (String[]) event.getData();
            try {
                int AD_Session_ID = Integer.parseInt(data[0]);
                MSession session = new MSession(Env.getCtx(), AD_Session_ID, null);
                if (session.get_ID() == AD_Session_ID) {
                    int AD_User_ID = session.getCreatedBy();
                    MUser user = MUser.get(Env.getCtx(), AD_User_ID);
                    if (user != null && user.get_ID() == AD_User_ID) {
                        String token = data[1];
                        if (BrowserToken.validateToken(session, user, token)) {
                            if (MSystem.isZKRememberUserAllowed()) {
                                txtUserId.setValue(user.getName());
                                onUserIdChange();
                                chkRememberMe.setChecked(true);
                            }
                            if (MSystem.isZKRememberPasswordAllowed()) {
                                txtPassword.setValue(token);
                                txtPassword.setAttribute("user.token.hash", token);
                                txtPassword.setAttribute("user.token.sid", AD_Session_ID);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                //safe to ignore
                logger.log(Level.INFO, e.getLocalizedMessage(), e);
            }
        }
    });
}
Also used : Table(org.zkoss.zhtml.Table) Label(org.adempiere.webui.component.Label) MSession(org.compiere.model.MSession) Image(org.zkoss.zul.Image) WrongValueException(org.zkoss.zk.ui.WrongValueException) ApplicationException(org.adempiere.webui.exception.ApplicationException) Div(org.zkoss.zhtml.Div) Td(org.zkoss.zhtml.Td) ConfirmPanel(org.adempiere.webui.component.ConfirmPanel) Event(org.zkoss.zk.ui.event.Event) TokenEvent(org.adempiere.webui.event.TokenEvent) MUser(org.compiere.model.MUser) Tr(org.zkoss.zhtml.Tr)

Example 5 with MSession

use of org.compiere.model.MSession in project adempiere by adempiere.

the class LoginPanel method validateLogin.

/**
     *  validates user name and password when logging in
     *
    **/
public void validateLogin() {
    Login login = new Login(ctx);
    String userId = txtUserId.getValue();
    String userPassword = txtPassword.getValue();
    //check is token
    String token = (String) txtPassword.getAttribute("user.token.hash");
    if (token != null && token.equals(userPassword)) {
        userPassword = "";
        int AD_Session_ID = (Integer) txtPassword.getAttribute("user.token.sid");
        MSession session = new MSession(Env.getCtx(), AD_Session_ID, null);
        if (session.get_ID() == AD_Session_ID) {
            MUser user = MUser.get(Env.getCtx(), session.getCreatedBy());
            if (BrowserToken.validateToken(session, user, token)) {
                userPassword = user.getPassword();
            }
        }
    }
    KeyNamePair[] rolesKNPairs = login.getRoles(userId, userPassword);
    if (rolesKNPairs == null || rolesKNPairs.length == 0)
        throw new WrongValueException("User Id or Password invalid!!!");
    else {
        String langName = null;
        if (lstLanguage.getSelectedItem() != null)
            langName = (String) lstLanguage.getSelectedItem().getLabel();
        else
            langName = Language.getBaseLanguage().getName();
        Language language = findLanguage(langName);
        wndLogin.loginOk(userId, userPassword);
        // Elaine 2009/02/06
        Env.setContext(ctx, UserPreference.LANGUAGE_NAME, language.getName());
        Locales.setThreadLocal(language.getLocale());
        String timeoutText = getUpdateTimeoutTextScript();
        if (!Strings.isEmpty(timeoutText))
            Clients.response("zkLocaleJavaScript2", new AuScript(null, timeoutText));
    }
    // This temporary validation code is added to check the reported bug
    // [ adempiere-ZK Web Client-2832968 ] User context lost?
    // https://sourceforge.net/tracker/?func=detail&atid=955896&aid=2832968&group_id=176962
    // it's harmless, if there is no bug then this must never fail
    Session currSess = Executions.getCurrent().getDesktop().getSession();
    currSess.setAttribute("Check_AD_User_ID", Env.getAD_User_ID(ctx));
    // End of temporary code for [ adempiere-ZK Web Client-2832968 ] User context lost?
    Env.setContext(ctx, BrowserToken.REMEMBER_ME, chkRememberMe.isChecked());
    /* Check DB version */
    String version = DB.getSQLValueString(null, "SELECT Version FROM AD_System");
    //  Identical DB version
    if (!Adempiere.DB_VERSION.equals(version)) {
        String AD_Message = "DatabaseVersionError";
        //  Code assumes Database version {0}, but Database has Version {1}.
        //  complete message
        String msg = Msg.getMsg(ctx, AD_Message);
        msg = MessageFormat.format(msg, new Object[] { Adempiere.DB_VERSION, version });
        throw new ApplicationException(msg);
    }
}
Also used : MSession(org.compiere.model.MSession) Login(org.compiere.util.Login) AuScript(org.zkoss.zk.au.out.AuScript) ApplicationException(org.adempiere.webui.exception.ApplicationException) Language(org.compiere.util.Language) KeyNamePair(org.compiere.util.KeyNamePair) MUser(org.compiere.model.MUser) WrongValueException(org.zkoss.zk.ui.WrongValueException) MSession(org.compiere.model.MSession) Session(org.zkoss.zk.ui.Session)

Aggregations

MSession (org.compiere.model.MSession)9 MUser (org.compiere.model.MUser)3 ApplicationException (org.adempiere.webui.exception.ApplicationException)2 Language (org.compiere.util.Language)2 Page (org.zkoss.zk.ui.Page)2 Session (org.zkoss.zk.ui.Session)2 WrongValueException (org.zkoss.zk.ui.WrongValueException)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 HttpSession (javax.servlet.http.HttpSession)1 ConfirmPanel (org.adempiere.webui.component.ConfirmPanel)1 Label (org.adempiere.webui.component.Label)1 IDesktop (org.adempiere.webui.desktop.IDesktop)1 TokenEvent (org.adempiere.webui.event.TokenEvent)1 MBPBankAccount (org.compiere.model.MBPBankAccount)1 MTheme (org.compiere.model.MTheme)1 KeyNamePair (org.compiere.util.KeyNamePair)1 Login (org.compiere.util.Login)1 WebDoc (org.compiere.util.WebDoc)1 WebSessionCtx (org.compiere.util.WebSessionCtx)1