001package org.apache.wiki.tasks.auth; 002 003import org.apache.log4j.Logger; 004import org.apache.wiki.api.core.ContextEnum; 005import org.apache.wiki.api.core.Engine; 006import org.apache.wiki.api.exceptions.WikiException; 007import org.apache.wiki.auth.UserManager; 008import org.apache.wiki.auth.user.UserProfile; 009import org.apache.wiki.i18n.InternationalizationManager; 010import org.apache.wiki.tasks.TasksManager; 011import org.apache.wiki.util.MailUtil; 012import org.apache.wiki.workflow.Outcome; 013import org.apache.wiki.workflow.Task; 014import org.apache.wiki.workflow.WorkflowManager; 015 016import javax.mail.MessagingException; 017import javax.mail.internet.AddressException; 018import java.util.Locale; 019 020 021/** 022 * Handles the actual profile save action. 023 */ 024public class SaveUserProfileTask extends Task { 025 026 private static final long serialVersionUID = 6994297086560480285L; 027 private static final Logger LOG = Logger.getLogger( SaveUserProfileTask.class ); 028 private final Engine m_engine; 029 private final Locale m_loc; 030 031 /** 032 * Constructs a new Task for saving a user profile. 033 * 034 * @param engine the wiki engine 035 */ 036 public SaveUserProfileTask( final Engine engine, final Locale loc ) { 037 super( TasksManager.USER_PROFILE_SAVE_TASK_MESSAGE_KEY ); 038 m_engine = engine; 039 m_loc = loc; 040 } 041 042 /** 043 * Saves the user profile to the user database. 044 * 045 * @return {@link org.apache.wiki.workflow.Outcome#STEP_COMPLETE} if the task completed successfully 046 * @throws WikiException if the save did not complete for some reason 047 */ 048 @Override 049 public Outcome execute() throws WikiException { 050 // Retrieve user profile 051 final UserProfile profile = ( UserProfile )getWorkflowContext().get( WorkflowManager.WF_UP_CREATE_SAVE_ATTR_SAVED_PROFILE ); 052 053 // Save the profile (userdatabase will take care of timestamps for us) 054 m_engine.getManager( UserManager.class ).getUserDatabase().save( profile ); 055 056 // Send e-mail if user supplied an e-mail address 057 if ( profile != null && profile.getEmail() != null ) { 058 try { 059 final InternationalizationManager i18n = m_engine.getManager( InternationalizationManager.class ); 060 final String app = m_engine.getApplicationName(); 061 final String to = profile.getEmail(); 062 final String subject = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc, 063 "notification.createUserProfile.accept.subject", app ); 064 065 final String content = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc, 066 "notification.createUserProfile.accept.content", app, 067 profile.getLoginName(), 068 profile.getFullname(), 069 profile.getEmail(), 070 m_engine.getURL( ContextEnum.WIKI_LOGIN.getRequestContext(), null, null ) ); 071 MailUtil.sendMessage( m_engine.getWikiProperties(), to, subject, content ); 072 } catch ( final AddressException e) { 073 LOG.debug( e.getMessage(), e ); 074 } catch ( final MessagingException me ) { 075 LOG.error( "Could not send registration confirmation e-mail. Is the e-mail server running?", me ); 076 } 077 } 078 079 return Outcome.STEP_COMPLETE; 080 } 081 082}