001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.tasks.auth;
020
021import org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.ContextEnum;
025import org.apache.wiki.api.exceptions.WikiException;
026import org.apache.wiki.auth.UserManager;
027import org.apache.wiki.auth.user.UserProfile;
028import org.apache.wiki.i18n.InternationalizationManager;
029import org.apache.wiki.tasks.TasksManager;
030import org.apache.wiki.util.MailUtil;
031import org.apache.wiki.workflow.Outcome;
032import org.apache.wiki.workflow.Task;
033import org.apache.wiki.workflow.WorkflowManager;
034
035import javax.mail.MessagingException;
036import javax.mail.internet.AddressException;
037import java.util.Locale;
038
039
040/**
041 * Handles the actual profile save action. 
042 */
043public class SaveUserProfileTask extends Task {
044
045    private static final long serialVersionUID = 6994297086560480285L;
046    private static final Logger LOG = LogManager.getLogger( SaveUserProfileTask.class );
047    private final Locale m_loc;
048
049    /**
050     * Constructs a new Task for saving a user profile.
051     */
052    public SaveUserProfileTask( final Locale loc ) {
053        super( TasksManager.USER_PROFILE_SAVE_TASK_MESSAGE_KEY );
054        m_loc = loc;
055    }
056
057    /**
058     * Saves the user profile to the user database.
059     *
060     * @return {@link org.apache.wiki.workflow.Outcome#STEP_COMPLETE} if the task completed successfully
061     * @throws WikiException if the save did not complete for some reason
062     */
063    @Override
064    public Outcome execute( final Context context ) throws WikiException {
065        // Retrieve user profile
066        final UserProfile profile = ( UserProfile )getWorkflowContext().get( WorkflowManager.WF_UP_CREATE_SAVE_ATTR_SAVED_PROFILE );
067
068        // Save the profile (userdatabase will take care of timestamps for us)
069        context.getEngine().getManager( UserManager.class ).getUserDatabase().save( profile );
070
071        // Send e-mail if user supplied an e-mail address
072        if ( profile != null && profile.getEmail() != null ) {
073            try {
074                final InternationalizationManager i18n = context.getEngine().getManager( InternationalizationManager.class );
075                final String app = context.getEngine().getApplicationName();
076                final String to = profile.getEmail();
077                final String subject = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc,
078                                                 "notification.createUserProfile.accept.subject", app );
079
080                final String loginUrl = context.getEngine().getURL( ContextEnum.WIKI_LOGIN.getRequestContext(), null, null );
081                final String content = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc,
082                                                 "notification.createUserProfile.accept.content", app,
083                                                 profile.getLoginName(),
084                                                 profile.getFullname(),
085                                                 profile.getEmail(),
086                                                 loginUrl );
087                MailUtil.sendMessage( context.getEngine().getWikiProperties(), to, subject, content );
088            } catch ( final AddressException e) {
089                LOG.debug( e.getMessage(), e );
090            } catch ( final MessagingException me ) {
091                LOG.error( "Could not send registration confirmation e-mail. Is the e-mail server running?", me );
092            }
093        }
094
095        return Outcome.STEP_COMPLETE;
096    }
097
098}