001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://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.security; 029 030import org.opencms.file.CmsObject; 031import org.opencms.main.CmsLog; 032import org.opencms.util.CmsFileUtil; 033 034import java.util.TreeMap; 035 036import org.apache.commons.logging.Log; 037 038/** 039 * Class with static methods for logging user-related operations in a centralized manner. 040 */ 041public class CmsUserLog { 042 043 /** The logger to be used. */ 044 private static final Log LOG = CmsLog.getLog(CmsUserLog.class); 045 046 /** 047 * Logs a successful login. 048 * 049 * @param cms the CMS context 050 * @param user the name of the user 051 */ 052 public static void logLogin(CmsObject cms, String user) { 053 054 LOG.info("login successful: " + formatUser(user) + " " + context(cms)); 055 } 056 057 /** 058 * Logs a login failure. 059 * 060 * @param cms the CMS context 061 * @param user the name of the user 062 */ 063 public static void logLoginFailure(CmsObject cms, String user) { 064 065 LOG.info("login failed: " + formatUser(user) + " " + context(cms)); 066 } 067 068 /** 069 * Logs a successful logout. 070 * 071 * @param cms the CMS context 072 */ 073 public static void logLogout(CmsObject cms) { 074 075 LOG.info("logout: " + formatUser(cms.getRequestContext().getCurrentUser().getName()) + " " + context(cms)); 076 077 } 078 079 /** 080 * Logs a password change. 081 * 082 * @param cms the CMS context 083 * @param user the user name 084 */ 085 public static void logPasswordChange(CmsObject cms, String user) { 086 087 LOG.info("password changed: " + formatUser(user) + " " + context(cms)); 088 } 089 090 /** 091 * Logs a password change originally requested through the 'reset password' button. 092 * 093 * @param cms the CMS context 094 * @param user the user name 095 */ 096 public static void logPasswordChangeForRequestedReset(CmsObject cms, String user) { 097 098 LOG.info("password changed (reset requested): " + formatUser(user) + " " + context(cms)); 099 100 } 101 102 /** 103 * Logs a password reset request. 104 * 105 * @param cms the CMS context 106 * @param user the user name 107 */ 108 public static void logPasswordResetRequest(CmsObject cms, String user) { 109 110 LOG.info("password reset request: " + user + " " + context(cms)); 111 } 112 113 /** 114 * Logs that the 'force reset password' status was set on a user. 115 * 116 * @param cms the CMS context 117 * @param user the user name 118 */ 119 public static void logSetForceResetPassword(CmsObject cms, String user) { 120 121 LOG.info("forcing password reset on next login: " + user + " " + context(cms)); 122 } 123 124 /** 125 * Logs a user switch. 126 * 127 * @param cms the current CMS context 128 * @param name the name of the user to switch to 129 */ 130 public static void logSwitchUser(CmsObject cms, String name) { 131 132 LOG.info( 133 "user switch: " 134 + formatUser(cms.getRequestContext().getCurrentUser().getName()) 135 + " => " 136 + formatUser(name) 137 + " " 138 + context(cms)); 139 140 // TODO Auto-generated method stub 141 142 } 143 144 /** 145 * Helper method for formatting context information. 146 * 147 * @param cms the CMS context 148 * @return the context information 149 */ 150 private static TreeMap<String, String> context(CmsObject cms) { 151 152 TreeMap<String, String> result = new TreeMap<>(); 153 result.put("remote_address", cms.getRequestContext().getRemoteAddress()); 154 result.put("current_user", cms.getRequestContext().getCurrentUser().getName()); 155 return result; 156 } 157 158 /** 159 * Formats a user name. 160 * 161 * @param userName the user nam 162 * @return 163 */ 164 private static String formatUser(String userName) { 165 166 return CmsFileUtil.removeLeadingSeparator(userName); 167 } 168 169}