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