PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src-modules/org/opencms/workplace/CmsLoginHelper.java

http://github.com/alkacon/opencms-core
Java | 230 lines | 120 code | 24 blank | 86 comment | 23 complexity | e8b1db0beb8ec6be9cf619a4dec56b20 MD5 | raw file
Possible License(s): MIT, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * This library is part of OpenCms -
  3. * the Open Source Content Management System
  4. *
  5. * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * For further information about Alkacon Software GmbH & Co. KG, please see the
  18. * company website: http://www.alkacon.com
  19. *
  20. * For further information about OpenCms, please see the
  21. * project website: http://www.opencms.org
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. package org.opencms.workplace;
  28. import org.opencms.db.CmsUserSettings;
  29. import org.opencms.file.CmsProject;
  30. import org.opencms.i18n.CmsEncoder;
  31. import org.opencms.main.CmsException;
  32. import org.opencms.main.OpenCms;
  33. import org.opencms.security.CmsRole;
  34. import org.opencms.security.CmsSecurityException;
  35. import org.opencms.site.CmsSite;
  36. import org.opencms.util.CmsStringUtil;
  37. import java.util.Iterator;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import javax.servlet.http.HttpSession;
  41. import javax.servlet.jsp.PageContext;
  42. /**
  43. * Handles front-end login of users to the OpenCms workplace into the given site and project.<p>
  44. *
  45. * @since 7.0.3
  46. */
  47. public class CmsLoginHelper extends CmsWorkplace {
  48. /** The login exception. */
  49. private CmsException m_loginException;
  50. /**
  51. * Public constructor with JSP variables.<p>
  52. *
  53. * @param context the JSP page context
  54. * @param req the JSP request
  55. * @param res the JSP response
  56. */
  57. public CmsLoginHelper(PageContext context, HttpServletRequest req, HttpServletResponse res) {
  58. super(context, req, res);
  59. }
  60. /**
  61. * Returns the loginException.<p>
  62. *
  63. * @return the loginException
  64. */
  65. public CmsException getLoginException() {
  66. return m_loginException;
  67. }
  68. /**
  69. * Returns the formatted stack trace.<p>
  70. *
  71. * @return the formatted stack trace
  72. */
  73. public String getStacktrace() {
  74. String stacktrace = CmsException.getStackTraceAsString(getLoginException());
  75. stacktrace = CmsEncoder.escapeXml(stacktrace);
  76. return stacktrace;
  77. }
  78. /**
  79. * Logs the user into the given project and site.<p>
  80. *
  81. * Check the {@link #getLoginException()} for the error message.<p>
  82. *
  83. * @param userName the user name
  84. * @param password the password
  85. * @param projectName the optional project name, if <code>null</code> the default project is used
  86. * @param siteRoot the site of the resource, if <code>null</code> the default site is used
  87. * @param resourceName the resource to display
  88. *
  89. * @return <code>true</code> if the login has been successful
  90. */
  91. public boolean login(String userName, String password, String projectName, String siteRoot, String resourceName) {
  92. if (getCms().getRequestContext().getCurrentUser().isGuestUser()) {
  93. if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName) || CmsStringUtil.isEmptyOrWhitespaceOnly(password)) {
  94. return false;
  95. }
  96. // login the user
  97. try {
  98. getCms().loginUser(userName, password, getCms().getRequestContext().getRemoteAddress());
  99. } catch (CmsException e) {
  100. m_loginException = e;
  101. return false;
  102. }
  103. }
  104. // the user is logged in
  105. CmsUserSettings userSettings = new CmsUserSettings(getCms());
  106. // set the project
  107. try {
  108. if (CmsStringUtil.isEmptyOrWhitespaceOnly(projectName)) {
  109. // use the default project of the user
  110. projectName = userSettings.getStartProject();
  111. }
  112. // read the project
  113. CmsProject project = getCms().readProject(projectName);
  114. if (OpenCms.getOrgUnitManager().getAllAccessibleProjects(getCms(), project.getOuFqn(), false).contains(
  115. project)) {
  116. // user has access to the project, set this as current project
  117. getCms().getRequestContext().setCurrentProject(project);
  118. } else {
  119. throw new CmsSecurityException(
  120. Messages.get().container(Messages.ERR_PROJECT_NOT_ACCESSIBLE_2, userName, projectName));
  121. }
  122. } catch (CmsException e) {
  123. m_loginException = e;
  124. }
  125. if (m_loginException == null) {
  126. // set the site
  127. try {
  128. if (CmsStringUtil.isEmptyOrWhitespaceOnly(siteRoot)) {
  129. // set the default site root of the user
  130. siteRoot = userSettings.getStartSite();
  131. }
  132. // set the site root if accessible
  133. String oldSite = getCms().getRequestContext().getSiteRoot();
  134. try {
  135. getCms().getRequestContext().setSiteRoot("");
  136. getCms().readResource(siteRoot);
  137. } finally {
  138. getCms().getRequestContext().setSiteRoot(oldSite);
  139. }
  140. boolean hasAccess = false;
  141. CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
  142. Iterator<CmsSite> accessibles = OpenCms.getSiteManager().getAvailableSites(getCms(), false).iterator();
  143. while (accessibles.hasNext() && !hasAccess && (site != null)) {
  144. CmsSite accessible = accessibles.next();
  145. if (accessible.getSiteRoot().equals(site.getSiteRoot())) {
  146. hasAccess = true;
  147. }
  148. }
  149. if (hasAccess) {
  150. // user has access to the site, set this as current site
  151. getCms().getRequestContext().setSiteRoot(siteRoot);
  152. } else {
  153. throw new CmsSecurityException(
  154. Messages.get().container(Messages.ERR_SITE_NOT_ACCESSIBLE_2, userName, siteRoot));
  155. }
  156. } catch (CmsException e) {
  157. m_loginException = e;
  158. }
  159. }
  160. // try to read the resource to display
  161. try {
  162. getCms().readResource(resourceName);
  163. } catch (CmsException e) {
  164. m_loginException = e;
  165. }
  166. if (m_loginException != null) {
  167. // if an error occurred during login, invalidate the session
  168. HttpSession session = getJsp().getRequest().getSession(false);
  169. if (session != null) {
  170. session.invalidate();
  171. }
  172. return false;
  173. }
  174. // only for content creators so that direct edit works
  175. if (OpenCms.getRoleManager().hasRole(getCms(), CmsRole.ELEMENT_AUTHOR)) {
  176. // get / create the workplace settings
  177. CmsWorkplaceSettings wpSettings = getSettings();
  178. if (wpSettings == null) {
  179. // create the settings object
  180. wpSettings = new CmsWorkplaceSettings();
  181. wpSettings = initWorkplaceSettings(getCms(), wpSettings, false);
  182. }
  183. // set the settings for the workplace
  184. wpSettings.setSite(getCms().getRequestContext().getSiteRoot());
  185. wpSettings.setProject(getCms().getRequestContext().getCurrentProject().getUuid());
  186. wpSettings.setUser(getCms().getRequestContext().getCurrentUser());
  187. HttpSession session = getJsp().getRequest().getSession(true);
  188. storeSettings(session, wpSettings);
  189. }
  190. return true;
  191. }
  192. /**
  193. * @see org.opencms.workplace.CmsWorkplace#checkRole()
  194. */
  195. @Override
  196. protected void checkRole() {
  197. // do not check
  198. }
  199. /**
  200. * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
  201. */
  202. @Override
  203. protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
  204. // empty
  205. }
  206. }