    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.HttpUtil;
    031import org.apache.wiki.util.MailUtil;
    032import org.apache.wiki.workflow.Outcome;
    033import org.apache.wiki.workflow.Task;
    034import org.apache.wiki.workflow.WorkflowManager;
    035
    036import javax.mail.MessagingException;
    037import javax.mail.internet.AddressException;
    038import java.util.Locale;
    039
    040
    041/**
    042 * Handles the actual profile save action.
    043 */
    044public class SaveUserProfileTask extends Task {
    045
    046    private static final long serialVersionUID = 6994297086560480285L;
    047    private static final Logger LOG = LogManager.getLogger( SaveUserProfileTask.class );
    048    private final Locale m_loc;
    049
    050    /**
    051     * Constructs a new Task for saving a user profile.
    052     */
    053    public SaveUserProfileTask( final Locale loc ) {
    054        super( TasksManager.USER_PROFILE_SAVE_TASK_MESSAGE_KEY );
    055        m_loc = loc;
    056    }
    057
    058    /**
    059     * Saves the user profile to the user database.
    060     *
    061     * @return {@link org.apache.wiki.workflow.Outcome#STEP_COMPLETE} if the task completed successfully
    062     * @throws WikiException if the save did not complete for some reason
    063     */
    064    @Override
    065    public Outcome execute( final Context context ) throws WikiException {
    066        // Retrieve user profile
    067        final UserProfile profile = ( UserProfile )getWorkflowContext().get( WorkflowManager.WF_UP_CREATE_SAVE_ATTR_SAVED_PROFILE );
    068
    069        // Save the profile (userdatabase will take care of timestamps for us)
    070        context.getEngine().getManager( UserManager.class ).getUserDatabase().save( profile );
    071
    072        // Send e-mail if user supplied an e-mail address
    073        if ( profile != null && profile.getEmail() != null ) {
    074            try {
    075                final InternationalizationManager i18n = context.getEngine().getManager( InternationalizationManager.class );
    076                final String app = context.getEngine().getApplicationName();
    077                final String to = profile.getEmail();
    078                final String subject = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc,
    079                                                 "notification.createUserProfile.accept.subject", app );
    080
    081                final String loginUrl = context.getEngine().getURL( ContextEnum.WIKI_LOGIN.getRequestContext(), null, null );
    082
    083                final String absoluteLoginUrl = HttpUtil.getAbsoluteUrl(context.getHttpRequest(), loginUrl);
    084
    085                final String content = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc,
    086                                                 "notification.createUserProfile.accept.content", app,
    087                                                 profile.getLoginName(),
    088                                                 profile.getFullname(),
    089                                                 profile.getEmail(),
    090                                                 absoluteLoginUrl );
    091                MailUtil.sendMessage( context.getEngine().getWikiProperties(), to, subject, content );
    092            } catch ( final AddressException e) {
    093                LOG.debug( e.getMessage(), e );
    094            } catch ( final MessagingException me ) {
    095                LOG.error( "Could not send registration confirmation e-mail. Is the e-mail server running?", me );
    096            }
    097        }
    098
    099        return Outcome.STEP_COMPLETE;
    100    }
    101
    102}