001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.main.CmsException; 033import org.opencms.main.CmsResourceInitException; 034import org.opencms.main.I_CmsResourceInit; 035import org.opencms.security.CmsOrganizationalUnit; 036import org.opencms.ui.login.CmsLoginHelper; 037 038import jakarta.servlet.http.HttpServletRequest; 039import jakarta.servlet.http.HttpServletResponse; 040 041/** 042 * Resource init handler that loads the login form with the right parameters.<p> 043 * 044 * The login uri must have following format:<br> 045 * <code>/${CONTEXT}/${SERVLET}/system/login/${OU_PATH}</code><p> 046 * 047 * for example:<br> 048 * <code>/opencms/opencms/system/login/intranet/marketing</code><p> 049 * 050 * @since 6.5.6 051 */ 052public class CmsWorkplaceLoginHandler implements I_CmsResourceInit { 053 054 /** The login handler path. */ 055 public static final String LOGIN_HANDLER = "/system/login"; 056 057 /** The login form path. */ 058 public static final String LOGIN_FORM = "/system/login/index.html"; 059 060 /** 061 * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse) 062 */ 063 public CmsResource initResource( 064 CmsResource resource, 065 CmsObject cms, 066 HttpServletRequest req, 067 HttpServletResponse res) 068 throws CmsResourceInitException { 069 070 if (resource != null) { 071 return resource; 072 } 073 074 String uri = cms.getRequestContext().getUri(); 075 // check if the resource starts with the LOGIN_HANDLER 076 if (!uri.startsWith(LOGIN_HANDLER)) { 077 return resource; 078 } 079 String storedSiteRoot = cms.getRequestContext().getSiteRoot(); 080 try { 081 // we now must switch to the root site to read the resource 082 cms.getRequestContext().setSiteRoot("/"); 083 // read the resource 084 resource = cms.readResource(LOGIN_FORM); 085 } catch (CmsException e) { 086 throw new CmsResourceInitException(e.getMessageContainer(), e); 087 } finally { 088 // restore the siteroot 089 cms.getRequestContext().setSiteRoot(storedSiteRoot); 090 // resource may be null in case of an error 091 if (resource != null) { 092 // modify the uri to the one of the real resource 093 cms.getRequestContext().setUri(cms.getSitePath(resource)); 094 } 095 } 096 // get the ou path 097 String ou = uri.substring(LOGIN_HANDLER.length()); 098 if (!ou.startsWith(CmsOrganizationalUnit.SEPARATOR)) { 099 ou = CmsOrganizationalUnit.SEPARATOR + ou; 100 } 101 if (!ou.endsWith(CmsOrganizationalUnit.SEPARATOR)) { 102 ou += CmsOrganizationalUnit.SEPARATOR; 103 } 104 req.setAttribute(CmsLoginHelper.PARAM_PREDEF_OUFQN, ou); 105 return resource; 106 } 107}