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.ui.login;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsUser;
032import org.opencms.i18n.CmsLocaleManager;
033import org.opencms.mail.CmsHtmlMail;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.scheduler.I_CmsScheduledJob;
037import org.opencms.util.CmsFileUtil;
038import org.opencms.util.CmsStringUtil;
039
040import java.util.ArrayList;
041import java.util.List;
042import java.util.Locale;
043import java.util.Map;
044
045import org.apache.commons.logging.Log;
046
047import org.antlr.stringtemplate.StringTemplate;
048
049/**
050 * Scheduled job for locking user  accounts which have not been logged into for longer than the configured time.<p>
051 */
052public class CmsLockInactiveAccountsJob implements I_CmsScheduledJob {
053
054    /** Logger instance for this class. */
055    private static final Log LOG = CmsLog.getLog(CmsLockInactiveAccountsJob.class);
056
057    /**
058     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(org.opencms.file.CmsObject, java.util.Map)
059     */
060    public String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
061
062        List<CmsUser> users = OpenCms.getOrgUnitManager().getUsersWithoutAdditionalInfo(cms, "", true);
063        List<String> lockedUsers = new ArrayList<String>();
064        Locale locale = CmsLocaleManager.getDefaultLocale();
065
066        boolean testOnly = Boolean.parseBoolean(parameters.get("test"));
067        for (CmsUser user : users) {
068            try {
069                if (OpenCms.getLoginManager().canLockBecauseOfInactivity(cms, user)) {
070                    if (OpenCms.getLoginManager().checkInactive(user)) {
071                        user = cms.readUser(user.getId());
072                        if (user.getAdditionalInfo().get(CmsLoginController.KEY_ACCOUNT_LOCKED) == null) {
073                            LOG.info("User is inactive: " + user.getName());
074                            if (!testOnly) {
075                                user.getAdditionalInfo().put(CmsLoginController.KEY_ACCOUNT_LOCKED, "true");
076                                cms.writeUser(user);
077                            }
078                            lockedUsers.add(user.getDisplayName(cms, locale));
079                        }
080                    }
081                }
082            } catch (Exception e) {
083                LOG.error(e.getLocalizedMessage(), e);
084            }
085        }
086        String mailto = parameters.get("mailto");
087        if ((mailto != null) && !lockedUsers.isEmpty()) {
088            List<String> mailAddresses = CmsStringUtil.splitAsList(mailto, ",");
089            OpenCms.getLocaleManager();
090
091            String header = CmsInactiveUserMessages.getReportHeader(locale);
092            String subject = CmsInactiveUserMessages.getReportSubject(locale);
093            CmsHtmlMail mail = new CmsHtmlMail();
094
095            mail.setSubject(subject);
096            for (String address : mailAddresses) {
097                mail.addTo(address.trim());
098            }
099            String templateText = new String(
100                CmsFileUtil.readFully(getClass().getResourceAsStream("locked-users-report.html")),
101                "UTF-8");
102            StringTemplate template = new StringTemplate(templateText);
103            template.setAttribute("header", header);
104            template.setAttribute("users", lockedUsers);
105            template.toString();
106            mail.setHtmlMsg(template.toString());
107            try {
108                mail.send();
109            } catch (Exception e) {
110                LOG.error(e.getLocalizedMessage(), e);
111            }
112        }
113        return "";
114
115    }
116}