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.auth; 020 021import jakarta.mail.MessagingException; 022import org.apache.commons.lang3.StringUtils; 023import org.apache.logging.log4j.LogManager; 024import org.apache.logging.log4j.Logger; 025import org.apache.wiki.api.core.Engine; 026import org.apache.wiki.auth.user.UserProfile; 027import org.apache.wiki.i18n.InternationalizationManager; 028import org.apache.wiki.util.MailUtil; 029 030import java.text.DateFormat; 031import java.util.Date; 032import java.util.Locale; 033import java.util.Properties; 034 035 036/** 037 * Tells the owners of the accounts by e-mail what {@link AccountLifecycle} does to them. The texts are the 038 * <code>notification.credentials.*</code> ones of the templates bundle. Nobody is told who has no e-mail address. 039 * 040 * @since 3.0.1 041 */ 042public final class AccountLifecycleMailer implements AccountLifecycle.Notifier { 043 044 private static final Logger LOG = LogManager.getLogger( AccountLifecycleMailer.class ); 045 046 /** Sends a message, as {@link MailUtil#sendMessage(Properties, String, String, String)} does. */ 047 @FunctionalInterface 048 interface Mail { 049 050 void send( Properties props, String to, String subject, String content ) throws MessagingException; 051 052 } 053 054 private final Engine engine; 055 private final Locale locale; 056 private final Mail mail; 057 058 /** 059 * Creates a mailer that sends the messages with the mail settings of the wiki, in the language of the server. 060 * 061 * @param engine the wiki engine 062 */ 063 public AccountLifecycleMailer( final Engine engine ) { 064 this( engine, Locale.getDefault(), MailUtil::sendMessage ); 065 } 066 067 AccountLifecycleMailer( final Engine engine, final Locale locale, final Mail mail ) { 068 this.engine = engine; 069 this.locale = locale; 070 this.mail = mail; 071 } 072 073 /** {@inheritDoc} */ 074 @Override 075 public void passwordWillExpire( final UserProfile profile, final Date expires ) { 076 send( profile, "expiring", format( expires ) ); 077 } 078 079 /** {@inheritDoc} */ 080 @Override 081 public void accountLocked( final UserProfile profile, final Date deleted ) { 082 if( deleted == null ) { 083 send( profile, "locked" ); 084 } else { 085 send( profile, "lockedDeleted", format( deleted ) ); 086 } 087 } 088 089 /** {@inheritDoc} */ 090 @Override 091 public void accountDeleted( final UserProfile profile ) { 092 send( profile, "deleted" ); 093 } 094 095 private String format( final Date date ) { 096 return DateFormat.getDateInstance( DateFormat.LONG, locale ).format( date ); 097 } 098 099 /** 100 * Sends the message of a template. Its subject is called <code>notification.credentials.NAME.subject</code>, with the name of the 101 * wiki as argument, and its content <code>notification.credentials.NAME.content</code>, with the name of the wiki, the full name and 102 * the login name of the owner and the extra arguments. 103 */ 104 private void send( final UserProfile profile, final String name, final Object... extra ) { 105 if( StringUtils.isBlank( profile.getEmail() ) ) { 106 LOG.debug( "{} has no e-mail address, so it is not told about {}", profile.getLoginName(), name ); 107 return; 108 } 109 110 final InternationalizationManager i18n = engine.getManager( InternationalizationManager.class ); 111 final String app = engine.getApplicationName(); 112 final Object[] arguments = new Object[ 3 + extra.length ]; 113 arguments[ 0 ] = app; 114 arguments[ 1 ] = profile.getFullname() == null ? profile.getLoginName() : profile.getFullname(); 115 arguments[ 2 ] = profile.getLoginName(); 116 System.arraycopy( extra, 0, arguments, 3, extra.length ); 117 118 final String subject = i18n.get( InternationalizationManager.DEF_TEMPLATE, locale, "notification.credentials." + name + ".subject", app ); 119 final String content = i18n.get( InternationalizationManager.DEF_TEMPLATE, locale, "notification.credentials." + name + ".content", arguments ); 120 try { 121 mail.send( engine.getWikiProperties(), profile.getEmail(), subject, content ); 122 } catch( final MessagingException e ) { 123 throw new IllegalStateException( "Could not send the e-mail about " + name + " to " + profile.getLoginName() + ": " + e.getMessage(), e ); 124 } 125 } 126 127}