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                                user.setEnabled(false);
077                                cms.writeUser(user);
078                            }
079                            lockedUsers.add(user.getDisplayName(cms, locale));
080                        }
081                    }
082                }
083            } catch (Exception e) {
084                LOG.error(e.getLocalizedMessage(), e);
085            }
086        }
087        String mailto = parameters.get("mailto");
088        if ((mailto != null) && !lockedUsers.isEmpty()) {
089            List<String> mailAddresses = CmsStringUtil.splitAsList(mailto, ",");
090            OpenCms.getLocaleManager();
091
092            String header = CmsInactiveUserMessages.getReportHeader(locale);
093            String subject = CmsInactiveUserMessages.getReportSubject(locale);
094            CmsHtmlMail mail = new CmsHtmlMail();
095
096            mail.setSubject(subject);
097            for (String address : mailAddresses) {
098                mail.addTo(address.trim());
099            }
100            String templateText = new String(
101                CmsFileUtil.readFully(getClass().getResourceAsStream("locked-users-report.html")),
102                "UTF-8");
103            StringTemplate template = new StringTemplate(templateText);
104            template.setAttribute("header", header);
105            template.setAttribute("users", lockedUsers);
106            template.toString();
107            mail.setHtmlMsg(template.toString());
108            mail.setCharset("UTf-8");
109            try {
110                mail.send();
111            } catch (Exception e) {
112                LOG.error(e.getLocalizedMessage(), e);
113            }
114        }
115        return "";
116
117    }
118}