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.apps.user; 029 030import org.opencms.db.CmsUserSettings; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsUser; 033import org.opencms.main.CmsException; 034import org.opencms.main.CmsLog; 035import org.opencms.main.OpenCms; 036import org.opencms.report.A_CmsReportThread; 037import org.opencms.report.I_CmsReport; 038import org.opencms.security.CmsPasswordEncryptionException; 039import org.opencms.security.CmsRole; 040import org.opencms.ui.A_CmsUI; 041import org.opencms.ui.apps.CmsFileExplorerConfiguration; 042import org.opencms.ui.apps.CmsPageEditorConfiguration; 043import org.opencms.ui.apps.Messages; 044import org.opencms.util.CmsUUID; 045 046import java.util.ArrayList; 047import java.util.Iterator; 048import java.util.List; 049 050import org.apache.commons.logging.Log; 051 052/** 053 * Class for the import user thread.<p> 054 */ 055public class CmsImportUserThread extends A_CmsReportThread { 056 057 /** Log instance for this class. */ 058 private static final Log LOG = CmsLog.getLog(CmsImportUserThread.class); 059 060 /**List of user. */ 061 private List<CmsUser> m_userList; 062 063 /**List of roles. */ 064 private List<CmsRole> m_roleList; 065 066 /**List of groups. */ 067 private List<String> m_groupList; 068 069 /**Current ou. */ 070 private String m_ou; 071 072 /**indicates if mail to user should be send. */ 073 private boolean m_sendMail; 074 075 /** 076 * public constructor.<p> 077 * 078 * @param cms CmsObject 079 * @param ou ou name 080 * @param userList List of user to import 081 * @param groups groups to add user to 082 * @param roles roles to add user to 083 * @param sendmail send mail to user? 084 */ 085 protected CmsImportUserThread( 086 CmsObject cms, 087 String ou, 088 List<CmsUser> userList, 089 List<String> groups, 090 List<CmsRole> roles, 091 boolean sendmail) { 092 093 super(cms, "importUser"); 094 m_userList = userList; 095 m_roleList = roles; 096 m_groupList = groups; 097 m_ou = ou; 098 m_sendMail = sendmail; 099 initHtmlReport(A_CmsUI.get().getLocale()); 100 } 101 102 /** 103 * @see org.opencms.report.A_CmsReportThread#getReportUpdate() 104 */ 105 @Override 106 public String getReportUpdate() { 107 108 return getReport().getReportUpdate(); 109 } 110 111 /** 112 * @see java.lang.Thread#run() 113 */ 114 @Override 115 public void run() { 116 117 getReport().println(Messages.get().container(Messages.RPT_USERIMPORT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE); 118 getReport().println(); 119 getReport().println( 120 Messages.get().container(Messages.RPT_USERIMPORT_FILE_CONTAINS_1, String.valueOf(m_userList.size())), 121 I_CmsReport.FORMAT_DEFAULT); 122 Iterator<CmsUser> itUsers = m_userList.iterator(); 123 while (itUsers.hasNext()) { 124 CmsUser user = itUsers.next(); 125 CmsUser createdUser = null; 126 if (!isAlreadyAvailable(user.getName())) { 127 128 String password = user.getPassword(); 129 130 if (password.indexOf("_") == -1) { 131 try { 132 133 password = OpenCms.getPasswordHandler().digest(password); 134 135 if (m_sendMail) { 136 CmsUserEditDialog.sendMail(getCms(), user.getPassword(), user, m_ou, true, false); 137 } 138 } catch (CmsPasswordEncryptionException e) { 139 // 140 } 141 } else { 142 password = password.substring(password.indexOf("_") + 1); 143 if (m_sendMail) { 144 CmsUserEditDialog.sendMail(getCms(), "your old password", user, m_ou, true, false); 145 } 146 } 147 148 try { 149 createdUser = getCms().importUser( 150 new CmsUUID().toString(), 151 m_ou + user.getName(), 152 password, 153 user.getFirstname(), 154 user.getLastname(), 155 user.getEmail(), 156 user.getFlags(), 157 System.currentTimeMillis(), 158 user.getAdditionalInfo()); 159 160 if (!m_groupList.isEmpty()) { 161 162 Iterator<String> itGroups = m_groupList.iterator(); 163 while (itGroups.hasNext()) { 164 try { 165 getCms().addUserToGroup(createdUser.getName(), itGroups.next()); 166 } catch (CmsException e) { 167 // 168 } 169 } 170 } 171 172 if (!m_roleList.isEmpty()) { 173 Iterator<CmsRole> itRoles = m_roleList.iterator(); 174 while (itRoles.hasNext()) { 175 176 OpenCms.getRoleManager().addUserToRole( 177 getCms(), 178 itRoles.next().forOrgUnit(m_ou), 179 createdUser.getName()); 180 181 } 182 183 } 184 String startAppId = CmsPageEditorConfiguration.APP_ID; 185 if (OpenCms.getRoleManager().hasRole(getCms(), createdUser.getName(), CmsRole.WORKPLACE_USER)) { 186 startAppId = CmsFileExplorerConfiguration.APP_ID; 187 } 188 CmsUserSettings settings = new CmsUserSettings(createdUser); 189 settings.setStartView(startAppId); 190 settings.setStartProject("Offline"); 191 settings.save(getCms()); 192 } catch (CmsException e) { 193 LOG.error("Unable to create user", e); 194 } 195 if (createdUser != null) { 196 getReport().println( 197 Messages.get().container(Messages.RPT_USERIMPORT_IMPORT_SUCCESFULL_1, createdUser.getName()), 198 I_CmsReport.FORMAT_OK); 199 } 200 } else { 201 getReport().println( 202 Messages.get().container(Messages.RPT_USERIMPORT_IMPORT_ALREADY_IN_OU_1, user.getName()), 203 I_CmsReport.FORMAT_ERROR); 204 } 205 } 206 getReport().println(Messages.get().container(Messages.RPT_USERIMPORT_END_0), I_CmsReport.FORMAT_DEFAULT); 207 } 208 209 /** 210 * Checks if the given user name is already available inside the current ou.<p> 211 * 212 * @param userName the user name to check 213 * @return <code>true</code> if the user name is already available, otherwise return <code>false</code> 214 */ 215 protected boolean isAlreadyAvailable(String userName) { 216 217 List<CmsUser> availableUsers; 218 try { 219 availableUsers = OpenCms.getOrgUnitManager().getUsers(getCms(), m_ou, false); 220 } catch (CmsException e) { 221 availableUsers = new ArrayList<CmsUser>(); 222 } 223 Iterator<CmsUser> itAvailableUsers = availableUsers.iterator(); 224 while (itAvailableUsers.hasNext()) { 225 if (userName.equals(itAvailableUsers.next().getSimpleName())) { 226 return true; 227 } 228 } 229 return false; 230 } 231 232}