cmslogin.java

来自「找了很久才找到到源代码」· Java 代码 · 共 914 行 · 第 1/3 页

JAVA
914
字号
                        LOG.warn(Messages.get().getBundle().key(
                            Messages.LOG_LOGIN_NO_STARTUP_PROJECT_2,
                            m_username,
                            settings.getStartProject()), e);
                    }
                } else {
                    // there was an error during login

                    if (org.opencms.security.Messages.ERR_LOGIN_FAILED_DISABLED_2 == getLoginException().getMessageContainer().getKey()) {
                        // the user account is disabled
                        m_message = Messages.get().container(Messages.GUI_LOGIN_FAILED_DISABLED_0);
                    } else if (org.opencms.security.Messages.ERR_LOGIN_FAILED_TEMP_DISABLED_4 == getLoginException().getMessageContainer().getKey()) {
                        // the user account is temporarily disabled because of too many login failures
                        m_message = Messages.get().container(Messages.GUI_LOGIN_FAILED_TEMP_DISABLED_0);
                    } else if (org.opencms.security.Messages.ERR_LOGIN_FAILED_WITH_MESSAGE_1 == getLoginException().getMessageContainer().getKey()) {
                        // all logins have been diasabled be the Administration
                        CmsLoginMessage loginMessage = OpenCms.getLoginManager().getLoginMessage();
                        if (loginMessage != null) {
                            m_message = Messages.get().container(
                                Messages.GUI_LOGIN_FAILED_WITH_MESSAGE_1,
                                loginMessage.getMessage());
                        }
                    }
                    if (m_message == null) {
                        // any other error - display default message
                        m_message = Messages.get().container(Messages.GUI_LOGIN_FAILED_0);
                    }
                }
            }
        } else if (Boolean.valueOf(m_actionLogout).booleanValue()) {
            m_action = ACTION_LOGOUT;
            // after logout this will automatically redirect to the login form again
            logout();
            return null;
        }

        if (m_action == ACTION_LOGIN) {
            // clear message
            m_message = null;
            // login is successful, check if the requested resource can be read
            CmsUriSplitter splitter = new CmsUriSplitter(m_requestedResource, true);
            String resource = splitter.getPrefix();
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(resource)) {
                // bad resource name, use workplace as default
                resource = CmsFrameset.JSP_WORKPLACE_URI;
            }
            if (!getCmsObject().existsResource(resource, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
                // requested resource does either not exist or is not readable by user
                if (CmsFrameset.JSP_WORKPLACE_URI.equals(resource)) {
                    // we know the Workplace exists, so the user does not have access to the Workplace
                    // probalbly this is a "Guest" user in a default setup where "Guest" has no access to the Workplace
                    m_message = Messages.get().container(Messages.GUI_LOGIN_FAILED_NO_WORKPLACE_PERMISSIONS_0);
                    m_action = ACTION_DISPLAY;
                } else if (getCmsObject().existsResource(CmsFrameset.JSP_WORKPLACE_URI)) {
                    // resource does either not exist or is not readable, but general workplace permissions are granted
                    m_message = Messages.get().container(Messages.GUI_LOGIN_UNKNOWN_RESOURCE_1, m_requestedResource);
                    m_requestedResource = CmsFrameset.JSP_WORKPLACE_URI;
                } else {
                    // resource does not exist and no general workplace permissions granted
                    m_message = Messages.get().container(
                        Messages.GUI_LOGIN_FAILED_NO_TARGET_PERMISSIONS_1,
                        m_requestedResource);
                    m_action = ACTION_DISPLAY;
                }
            }
            if (m_action == ACTION_DISPLAY) {
                // the login was invalid
                m_requestedResource = null;
                // destroy the generated session
                HttpSession session = getRequest().getSession(false);
                if (session != null) {
                    session.invalidate();
                }
            } else {
                // successfully logged in, so set the cookie
                setCookieData();
            }
        }

        return displayLoginForm();
    }

    /**
     * Gets the login info from the cookies.<p>
     */
    public void getCookieData() {

        // get the user name cookie
        Cookie userNameCookie = getCookie(COOKIE_USERNAME);
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userNameCookie.getValue())) {
            // only set the data is needed
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_username)) {
                m_username = userNameCookie.getValue();
            }
        }
        if ("null".equals(m_username)) {
            m_username = null;
        }
        // get the user name cookie
        Cookie ouFqnCookie = getCookie(COOKIE_OUFQN);
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(ouFqnCookie.getValue())) {
            // only set the data is needed
            if (m_oufqn == null) {
                m_oufqn = ouFqnCookie.getValue();
            }
        }
        if ("null".equals(m_oufqn)) {
            m_oufqn = null;
        }
    }

    /**
     * @see org.opencms.jsp.CmsJspLoginBean#getFormLink()
     */
    public String getFormLink() {

        if (getRequest().getAttribute(PARAM_PREDEF_OUFQN) == null) {
            return super.getFormLink();
        }
        return link("/system/login" + (String)getRequest().getAttribute(PARAM_PREDEF_OUFQN));
    }

    /**
     * Sets the login cookies.<p>
     */
    public void setCookieData() {

        // set the user name cookie
        Cookie userNameCookie = getCookie(COOKIE_USERNAME);
        userNameCookie.setValue(m_username);
        setCookie(userNameCookie);

        // set the user name cookie
        Cookie ouFqnCookie = getCookie(COOKIE_OUFQN);
        ouFqnCookie.setValue(m_oufqn);
        setCookie(ouFqnCookie);
    }

    /**
     * Appends the JavaScript for the login screen
     * to the given HTML buffer.<p>
     * 
     * @param html the html buffer to append the script to
     * @param message the message to display after an unsuccessful login
     */
    protected void appendDefaultLoginScript(StringBuffer html, CmsMessageContainer message) {

        html.append("<script type=\"text/javascript\">\n");
        if (message != null) {
            html.append("function showAlert() {\n");
            html.append("\talert(\"");
            html.append(CmsStringUtil.escapeJavaScript(message.key(m_locale)));
            html.append("\");\n");
            html.append("}\n");
        }
        html.append("var orgUnitShow = false;\n");
        html.append("function orgUnitSelection() {\n");
        html.append("\tif (!orgUnitShow) {\n");
        html.append("\t\tdocument.getElementById('ouSelId').style.display = 'block';\n");
        html.append("\t\tdocument.getElementById('ouLabelId').style.display = 'block';\n");
        html.append("\t\tdocument.getElementById('ouBtnId').value = '");
        html.append(Messages.get().getBundle(m_locale).key(Messages.GUI_LOGIN_ORGUNIT_SELECT_OFF_0));
        html.append("';\n");
        html.append("\t} else {\n");
        html.append("\t\tdocument.getElementById('ouSelId').style.display = 'none';\n");
        html.append("\t\tdocument.getElementById('ouLabelId').style.display = 'none';\n");
        html.append("\t\tdocument.getElementById('ouBtnId').value = '");
        html.append(Messages.get().getBundle(m_locale).key(Messages.GUI_LOGIN_ORGUNIT_SELECT_ON_0));
        html.append("';\n");
        html.append("\t}\n");
        html.append("\torgUnitShow = !orgUnitShow;\n");
        html.append("\tdocument.getElementById('titleId').style.display = 'block';\n");
        html.append("\tdocument.getElementById('titleIdOu').style.display = 'none';\n");
        html.append("}\n");

        html.append("function doOnload() {\n");
        html.append("\tdocument.");
        html.append(PARAM_FORM);
        html.append(".");
        html.append(PARAM_USERNAME);
        html.append(".select();\n");
        html.append("\tdocument.");
        html.append(PARAM_FORM);
        html.append(".");
        html.append(PARAM_USERNAME);
        html.append(".focus();\n");
        if (message != null) {
            html.append("\tshowAlert();\n");
        }
        html.append("}\n");

        html.append("</script>\n");
    }

    /**
     * Appends the JavaScript that opens the Workplace window after a successful login
     * to the given HTML buffer.<p>
     * 
     * @param html the html buffer to append the script to
     * @param requestedResource the requested resource to open in a new window
     * @param message the message to display if the originally requested resource is not available
     */
    protected void appendWorkplaceOpenerScript(StringBuffer html, String requestedResource, CmsMessageContainer message) {

        String winId = "OpenCms" + System.currentTimeMillis();

        html.append("<script type=\"text/javascript\">\n");

        html.append("function doOnload() {\n");

        // display missing resource warning if required
        if (message != null) {
            html.append("\talert(\"");
            html.append(CmsStringUtil.escapeJavaScript(message.key(m_locale)));
            html.append("\");\n");
        }

        // display login message if required
        CmsLoginMessage loginMessage = OpenCms.getLoginManager().getLoginMessage();
        if ((loginMessage != null) && (loginMessage.isActive())) {
            String msg;
            if (loginMessage.isLoginForbidden()) {
                // login forbidden for normal users, current user must be Administrator
                msg = Messages.get().container(
                    Messages.GUI_LOGIN_SUCCESS_WITH_MESSAGE_2,
                    loginMessage.getMessage(),
                    new Date(loginMessage.getTimeEnd())).key(m_locale);
            } else {
                // just display the message
                msg = loginMessage.getMessage();
            }
            html.append("\talert(\"");
            html.append(CmsStringUtil.escapeJavaScript(msg));
            html.append("\");\n");
        }

        html.append("\tvar openUri = \"");
        html.append(link(requestedResource));
        html.append("\";\n");
        html.append("\tvar workplaceWin = openWorkplace(openUri, \"");
        html.append(winId);
        html.append("\");\n");
        html.append("\tif (window.name != \"");
        html.append(winId);
        html.append("\") {\n");
        html.append("\t\twindow.opener = workplaceWin;\n");
        html.append("\t\tif (workplaceWin != null) {\n");
        html.append("\t\t\twindow.close();\n");
        html.append("\t\t}\n");
        html.append("\t}\n");
        html.append("}\n");

        html.append("function openWorkplace(url, name) {\n");
        html.append("\tvar isInWin = (window.name.match(/^OpenCms\\d+$/) != null);\n");
        html.append("\tif (window.innerHeight) {\n");
        // Mozilla
        html.append("\t\tvar winHeight = window.innerHeight;\n");
        html.append("\t\tvar winWidth = window.innerWidth;\n");
        html.append("\t} else if (document.documentElement && document.documentElement.clientHeight) {\n");
        // IE 6 "strict" mode
        html.append("\t\tvar winHeight = document.documentElement.clientHeight;\n");
        html.append("\t\tvar winWidth = document.documentElement.clientWidth;\n");
        html.append("\t} else if (document.body && document.body.clientHeight) {\n");
        // IE 5, IE 6 "relaxed" mode
        html.append("\t\tvar winHeight = document.body.clientWidth;\n");
        html.append("\t\tvar winWidth = document.body.clientHeight;\n");
        html.append("\t}\n");
        html.append("\tif (window.screenY) {\n");
        // Mozilla
        html.append("\t\tvar winTop = window.screenY;\n");
        html.append("\t\tvar winLeft = window.screenX;\n");
        html.append("\t\tif (! isInWin) {\n");
        html.append("\t\t\twinTop += 25;\n");
        html.append("\t\t\twinLeft += 25;\n");
        html.append("\t\t}\n");
        html.append("\t} else if (window.screenTop) {\n");
        // IE
        html.append("\t\tvar winTop = window.screenTop;\n");
        html.append("\t\tvar winLeft = window.screenLeft;\n");
        html.append("\t}\n");
        html.append("\n");

        if (requestedResource.startsWith(CmsWorkplace.VFS_PATH_WORKPLACE)) {
            html.append("\tvar openerStr = \"width=\" + winWidth + \",height=\" + winHeight + \",left=\" + winLeft + \",top=\" + winTop + \",scrollbars=no,location=no,toolbar=no,menubar=no,directories=no,status=yes,resizable=yes\";\n");
        } else {
            html.append("\tvar openerStr = \"width=\" + winWidth + \",height=\" + winHeight + \",left=\" + winLeft + \",top=\" + winTop + \",scrollbars=yes,location=yes,toolbar=yes,menubar=yes,directories=no,status=yes,resizable=yes\";\n");
        }
        html.append("\tvar OpenCmsWin = window.open(url, name, openerStr);\n");
        html.append("\n");
        html.append("\ttry{\n");
        html.append("\t\tif (! OpenCmsWin.opener) {\n");
        html.append("\t\t\tOpenCmsWin.opener = self;\n");
        html.append("\t\t}\n");
        html.append("\t\tif (OpenCmsWin.focus) {\n");
        html.append("\t\t\tOpenCmsWin.focus();\n");
        html.append("\t\t}\n");
        html.append("\t} catch (e) {}\n");
        html.append("\n");
        html.append("\treturn OpenCmsWin;\n");
        html.append("}\n");

        html.append("</script>\n");
    }

    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?