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