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.file.CmsRequestContext;
032import org.opencms.main.CmsLog;
033import org.opencms.util.CmsFileUtil;
034
035import java.util.TreeMap;
036
037import org.apache.commons.logging.Log;
038
039/**
040 * Class with static methods for logging user-related operations in a centralized manner.
041 */
042public class CmsUserLog {
043
044    /** The logger to be used. */
045    private static final Log LOG = CmsLog.getLog(CmsUserLog.class);
046
047    /**
048     * Logs a successful login.
049     *
050     * @param cms the CMS context
051     * @param user the name of the user
052     */
053    public static void logLogin(CmsObject cms, String user) {
054
055        LOG.info("login successful: " + formatUser(user) + " " + context(cms));
056    }
057
058    /**
059     * Logs a login failure.
060     *
061     * @param cms the CMS context
062     * @param user the name of the user
063     */
064    public static void logLoginFailure(CmsObject cms, String user) {
065
066        LOG.info("login failed: " + formatUser(user) + " " + context(cms));
067    }
068
069    /**
070     * Logs a successful logout.
071     *
072     * @param cms the CMS context
073     */
074    public static void logLogout(CmsObject cms) {
075
076        LOG.info("logout: " + formatUser(cms.getRequestContext().getCurrentUser().getName()) + " " + context(cms));
077
078    }
079
080    /**
081     * Logs a password change.
082     *
083     * @param cms the CMS context
084     * @param user the user name
085     */
086    public static void logPasswordChange(CmsObject cms, String user) {
087
088        LOG.info("password changed: " + formatUser(user) + " " + context(cms));
089    }
090
091    /**
092     * Logs a password change originally requested through the 'reset password' button.
093     *
094     * @param cms the CMS context
095     * @param user the user name
096     */
097    public static void logPasswordChangeForRequestedReset(CmsObject cms, String user) {
098
099        LOG.info("password changed (reset requested): " + formatUser(user) + " " + context(cms));
100
101    }
102
103    /**
104     * Logs a password reset request.
105     *
106     * @param cms the CMS context
107     * @param user the user name
108     */
109    public static void logPasswordResetRequest(CmsObject cms, String user) {
110
111        LOG.info("password reset request: " + user + " " + context(cms));
112    }
113
114    /**
115     * Logs when a second factor was added.
116     *
117     * @param requestContext the request context
118     * @param name the user name
119     */
120    public static void logSecondFactorAdded(CmsRequestContext requestContext, String name) {
121
122        LOG.info("second factor added: " + formatUser(name) + " " + context(requestContext));
123    }
124
125    /**
126     * Logs when a second factor was modified.
127     *
128     * @param requestContext the request context
129     * @param name the user name
130     */
131    public static void logSecondFactorInfoModified(CmsRequestContext requestContext, String name) {
132
133        LOG.info("second factor information modified: " + formatUser(name) + " " + context(requestContext));
134    }
135
136    /**
137     * Logs when a second factor was removed.
138     *
139     * @param requestContext the request context
140     * @param name the user name
141     */
142    public static void logSecondFactorReset(CmsRequestContext requestContext, String name) {
143
144        LOG.info("second factor reset: " + formatUser(name) + " " + context(requestContext));
145    }
146
147    /**
148     * Logs that the 'force reset password' status was set on a user.
149     *
150     * @param cms the CMS context
151     * @param user the user name
152     */
153    public static void logSetForceResetPassword(CmsObject cms, String user) {
154
155        LOG.info("forcing password reset on next login: " + user + " " + context(cms));
156    }
157
158    /**
159     * Logs a user switch.
160     *
161     * @param cms the current CMS context
162     * @param name the name of the user to switch to
163     */
164    public static void logSwitchUser(CmsObject cms, String name) {
165
166        LOG.info(
167            "user switch: "
168                + formatUser(cms.getRequestContext().getCurrentUser().getName())
169                + " => "
170                + formatUser(name)
171                + " "
172                + context(cms));
173
174        // TODO Auto-generated method stub
175    }
176
177    /**
178     * Helper method for formatting context information.
179     *
180     * @param cms the CMS context
181     * @return the context information
182     */
183    private static TreeMap<String, String> context(CmsObject cms) {
184
185        return context(cms.getRequestContext());
186    }
187
188    /**
189     * Helper method for formatting context information.
190     *
191     * @param requestContext the request context
192     * @return the context information
193     */
194    private static TreeMap<String, String> context(CmsRequestContext requestContext) {
195
196        TreeMap<String, String> result = new TreeMap<>();
197        result.put("remote_address", requestContext.getRemoteAddress());
198        result.put("current_user", requestContext.getCurrentUser().getName());
199        return result;
200    }
201
202    /**
203     * Formats a user name.
204     *
205     * @param userName the user nam
206     * @return the formatted user name
207     */
208    private static String formatUser(String userName) {
209
210        return CmsFileUtil.removeLeadingSeparator(userName);
211    }
212
213}