1 40 package org.dspace.eperson; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 import java.sql.Timestamp ; 45 import java.util.Calendar ; 46 47 import javax.mail.MessagingException ; 48 49 import org.apache.log4j.Logger; 50 import org.dspace.authorize.AuthorizeException; 51 import org.dspace.core.ConfigurationManager; 52 import org.dspace.core.Context; 53 import org.dspace.core.Email; 54 import org.dspace.core.Utils; 55 import org.dspace.storage.rdbms.DatabaseManager; 56 import org.dspace.storage.rdbms.TableRow; 57 58 72 public class AccountManager 73 { 74 75 private static Logger log = Logger.getLogger(AccountManager.class); 76 77 78 protected AccountManager() 79 { 80 } 81 82 95 public static void sendRegistrationInfo(Context context, String email) 96 throws SQLException , IOException , MessagingException , 97 AuthorizeException 98 { 99 sendInfo(context, email, true, true); 100 } 101 102 115 public static void sendForgotPasswordInfo(Context context, String email) 116 throws SQLException , IOException , MessagingException , 117 AuthorizeException 118 { 119 sendInfo(context, email, false, true); 120 } 121 122 142 public static EPerson getEPerson(Context context, String token) 143 throws SQLException , AuthorizeException 144 { 145 String email = getEmail(context, token); 146 147 if (email == null) 148 { 149 return null; 150 } 151 152 EPerson ep = EPerson.findByEmail(context, email); 153 154 return ep; 155 } 156 157 167 public static String getEmail(Context context, String token) 168 throws SQLException 169 { 170 TableRow rd = DatabaseManager.findByUnique(context, "RegistrationData", 171 "token", token); 172 173 if (rd == null) 174 { 175 return null; 176 } 177 178 183 return rd.getStringColumn("email"); 184 } 185 186 196 public static void deleteToken(Context context, String token) 197 throws SQLException 198 { 199 DatabaseManager.deleteByValue(context, "RegistrationData", "token", 200 token); 201 } 202 203 221 protected static TableRow sendInfo(Context context, String email, 222 boolean isRegister, boolean send) throws SQLException , IOException , 223 MessagingException , AuthorizeException 224 { 225 TableRow rd = DatabaseManager.findByUnique(context, "registrationdata", 227 "email", email); 228 229 if (rd == null) 231 { 232 rd = DatabaseManager.create(context, "RegistrationData"); 233 rd.setColumn("token", Utils.generateHexKey()); 234 235 rd.setColumn("email", email); 238 DatabaseManager.update(context, rd); 239 240 if (log.isDebugEnabled()) 244 { 245 log.debug("Created callback " 246 + rd.getIntColumn("registrationdata_id") 247 + " with token " + rd.getStringColumn("token") 248 + " with email \"" + email + "\""); 249 } 250 } 251 252 if (send) 253 { 254 sendEmail(email, isRegister, rd); 255 } 256 257 return rd; 258 } 259 260 278 private static void sendEmail(String email, boolean isRegister, TableRow rd) 279 throws MessagingException , IOException 280 { 281 String base = ConfigurationManager.getProperty("dspace.url"); 282 283 String specialLink = new StringBuffer ().append(base).append( 285 base.endsWith("/") ? "" : "/").append( 286 isRegister ? "register" : "forgot").append("?") 287 .append("token=").append(rd.getStringColumn("token")) 288 .toString(); 289 290 Email bean = ConfigurationManager.getEmail(isRegister ? "register" 291 : "change_password"); 292 bean.addRecipient(email); 293 bean.addArgument(specialLink); 294 bean.send(); 295 296 if (log.isInfoEnabled()) 298 { 299 log.info("Sent " + (isRegister ? "registration" : "account") 300 + " information to " + email); 301 } 302 } 303 304 309 private static Timestamp getDefaultExpirationDate() 310 { 311 Calendar calendar = Calendar.getInstance(); 312 313 calendar.setTime(new java.util.Date ()); 314 315 calendar.add(Calendar.WEEK_OF_YEAR, 52); 317 318 return new java.sql.Timestamp (calendar.getTime().getTime()); 319 } 320 } 321 | Popular Tags |