View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.mylutece.service;
35  
36  import fr.paris.lutece.plugins.mylutece.util.SecurityUtils;
37  import fr.paris.lutece.portal.service.daemon.Daemon;
38  import fr.paris.lutece.portal.service.mail.MailService;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.util.ReferenceItem;
43  import fr.paris.lutece.util.date.DateUtil;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import org.apache.commons.lang3.StringUtils;
47  
48  import java.sql.Timestamp;
49  
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Locale;
53  import java.util.Map;
54  
55  /**
56   * Daemon to anonymize users
57   */
58  public abstract class AbstractAccountLifeTimeDaemon extends Daemon
59  {
60      private static final String PARAMETER_TIME_BEFORE_ALERT_ACCOUNT = "time_before_alert_account";
61      private static final String PARAMETER_NB_ALERT_ACCOUNT = "nb_alert_account";
62      private static final String PARAMETER_TIME_BETWEEN_ALERTS_ACCOUNT = "time_between_alerts_account";
63      private static final String PARAMETER_NOTIFY_USER_PASSWORD_EXPIRED = "notify_user_password_expired";
64      private static final String PARAMETER_EXPIRED_ALERT_MAIL_SENDER = "expired_alert_mail_sender";
65      private static final String PARAMETER_EXPIRED_ALERT_MAIL_SUBJECT = "expired_alert_mail_subject";
66      private static final String PARAMETER_FIRST_ALERT_MAIL_SENDER = "first_alert_mail_sender";
67      private static final String PARAMETER_FIRST_ALERT_MAIL_SUBJECT = "first_alert_mail_subject";
68      private static final String PARAMETER_OTHER_ALERT_MAIL_SENDER = "other_alert_mail_sender";
69      private static final String PARAMETER_OTHER_ALERT_MAIL_SUBJECT = "other_alert_mail_subject";
70      private static final String PARAMETER_PASSWORD_EXPIRED_MAIL_SENDER = "password_expired_mail_sender";
71      private static final String PARAMETER_PASSWORD_EXPIRED_MAIL_SUBJECT = "password_expired_mail_subject";
72  
73      /**
74       * Get the account life time service implementation to use
75       * 
76       * @return The account life time service
77       */
78      public abstract IAccountLifeTimeService getAccountLifeTimeService( );
79  
80      /**
81       * Get the Parameter service to use
82       * 
83       * @return The parameter service to use
84       */
85      public abstract IUserParameterService getParameterService( );
86  
87      /**
88       * Get the name of the daemon
89       * 
90       * @return The name of the daemon
91       */
92      public abstract String getDaemonName( );
93  
94      /**
95       * {@inheritDoc}
96       */
97      @SuppressWarnings( "deprecation" )
98      @Override
99      public void run( )
100     {
101         StringBuilder sbLogs = null;
102 
103         IAccountLifeTimeService accountLifeTimeService = getAccountLifeTimeService( );
104         IUserParameterService parameterService = getParameterService( );
105         Plugin plugin = accountLifeTimeService.getPlugin( );
106 
107         Timestamp currentTimestamp = new Timestamp( new java.util.Date( ).getTime( ) );
108         List<Integer> accountsToSetAsExpired = accountLifeTimeService.getIdUsersWithExpiredLifeTimeList( currentTimestamp );
109 
110         StringBuilder sbResult = new StringBuilder( );
111 
112         // We first set as expirated user that have reached their life time limit
113         if ( ( accountsToSetAsExpired != null ) && ( accountsToSetAsExpired.size( ) > 0 ) )
114         {
115             int nbAccountToExpire = accountsToSetAsExpired.size( );
116             String strBody = accountLifeTimeService.getExpirationtMailBody( );
117 
118             ReferenceItem referenceItem = parameterService.findByKey( PARAMETER_EXPIRED_ALERT_MAIL_SENDER, plugin );
119             String strSenderName = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
120             String strSenderEmail = MailService.getNoReplyEmail( );
121             referenceItem = parameterService.findByKey( PARAMETER_EXPIRED_ALERT_MAIL_SUBJECT, plugin );
122 
123             String strSubject = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
124 
125             for ( Integer nIdUser : accountsToSetAsExpired )
126             {
127                 try
128                 {
129                     String strUserMail = accountLifeTimeService.getUserMainEmail( nIdUser );
130 
131                     if ( ( strUserMail != null ) && StringUtils.isNotBlank( strUserMail ) )
132                     {
133                         Map<String, String> model = new HashMap<String, String>( );
134                         accountLifeTimeService.addParametersToModel( model, nIdUser );
135 
136                         HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strBody, Locale.getDefault( ), model );
137                         MailService.sendMailHtml( strUserMail, strSenderName, strSenderEmail, strSubject, template.getHtml( ) );
138                     }
139                 }
140                 catch( Exception e )
141                 {
142                     AppLogService.error( getDaemonName( ) + " - Error sending expiration alert to user : " + e.getMessage( ), e );
143                 }
144             }
145 
146             accountLifeTimeService.setUserStatusExpired( accountsToSetAsExpired );
147             accountsToSetAsExpired = null;
148             sbLogs = new StringBuilder( );
149             sbLogs.append( getDaemonName( ) );
150             sbLogs.append( " - " );
151             sbLogs.append( Integer.toString( nbAccountToExpire ) );
152             sbLogs.append( " account(s) have expired" );
153             AppLogService.info( sbLogs.toString( ) );
154             sbResult.append( sbLogs.toString( ) );
155             sbResult.append( "\n" );
156         }
157         else
158         {
159             AppLogService.info( getDaemonName( ) + " - No expired user found" );
160             sbResult.append( getDaemonName( ) + " - No expired user found\n" );
161         }
162 
163         // We send first alert to users
164         long nbDaysBeforeFirstAlert = SecurityUtils.getIntegerSecurityParameter( parameterService, plugin, PARAMETER_TIME_BEFORE_ALERT_ACCOUNT );
165 
166         Timestamp firstAlertMaxDate = new Timestamp( currentTimestamp.getTime( ) + DateUtil.convertDaysInMiliseconds( nbDaysBeforeFirstAlert ) );
167 
168         if ( nbDaysBeforeFirstAlert <= 0 )
169         {
170             AppLogService.info( getDaemonName( ) + " - First alert deactivated, skipping" );
171             sbResult.append( getDaemonName( ) + " - First alert deactivated, skipping\n" );
172         }
173         else
174         {
175             List<Integer> listIdUserToSendFirstAlert = accountLifeTimeService.getIdUsersToSendFirstAlert( firstAlertMaxDate );
176 
177             if ( ( listIdUserToSendFirstAlert != null ) && ( listIdUserToSendFirstAlert.size( ) > 0 ) )
178             {
179                 int nbFirstAlertSent = listIdUserToSendFirstAlert.size( );
180                 String strBody = accountLifeTimeService.getFirstAlertMailBody( );
181 
182                 ReferenceItem referenceItem = parameterService.findByKey( PARAMETER_FIRST_ALERT_MAIL_SENDER, plugin );
183                 String strSenderName = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
184                 String strSenderEmail = MailService.getNoReplyEmail( );
185                 referenceItem = parameterService.findByKey( PARAMETER_FIRST_ALERT_MAIL_SUBJECT, plugin );
186 
187                 String strSubject = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
188 
189                 for ( Integer nIdUser : listIdUserToSendFirstAlert )
190                 {
191                     try
192                     {
193                         String strUserMail = accountLifeTimeService.getUserMainEmail( nIdUser );
194 
195                         if ( ( strUserMail != null ) && StringUtils.isNotBlank( strUserMail ) )
196                         {
197                             Map<String, String> model = new HashMap<String, String>( );
198                             accountLifeTimeService.addParametersToModel( model, nIdUser );
199 
200                             HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strBody, Locale.getDefault( ), model );
201                             MailService.sendMailHtml( strUserMail, strSenderName, strSenderEmail, strSubject, template.getHtml( ) );
202                         }
203                     }
204                     catch( Exception e )
205                     {
206                         AppLogService.error( getDaemonName( ) + " - Error sending first alert to user : " + e.getMessage( ), e );
207                     }
208                 }
209 
210                 accountLifeTimeService.updateNbAlert( listIdUserToSendFirstAlert );
211 
212                 sbLogs = new StringBuilder( );
213                 sbLogs.append( "MyluteceAccountLifeTimeDaemon - " );
214                 sbLogs.append( Integer.toString( nbFirstAlertSent ) );
215                 sbLogs.append( " first alert(s) have been sent" );
216                 AppLogService.info( sbLogs.toString( ) );
217                 sbResult.append( sbLogs.toString( ) );
218                 sbResult.append( "\n" );
219             }
220             else
221             {
222                 AppLogService.info( getDaemonName( ) + " - No first alert to send" );
223                 sbResult.append( getDaemonName( ) + " - No first alert to send\n" );
224             }
225         }
226 
227         // We send other alert to users
228         int maxNumberOfAlerts = SecurityUtils.getIntegerSecurityParameter( parameterService, plugin, PARAMETER_NB_ALERT_ACCOUNT );
229         int nbDaysBetweenAlerts = SecurityUtils.getIntegerSecurityParameter( parameterService, plugin, PARAMETER_TIME_BETWEEN_ALERTS_ACCOUNT );
230         Timestamp timeBetweenAlerts = new Timestamp( DateUtil.convertDaysInMiliseconds( nbDaysBetweenAlerts ) );
231 
232         if ( ( maxNumberOfAlerts <= 0 ) || ( nbDaysBetweenAlerts <= 0 ) )
233         {
234             AppLogService.info( getDaemonName( ) + " - Other alerts deactivated, skipping" );
235             sbResult.append( getDaemonName( ) + " - Other alerts deactivated, skipping" );
236         }
237         else
238         {
239             List<Integer> listIdUserToSendNextAlert = accountLifeTimeService.getIdUsersToSendOtherAlert( firstAlertMaxDate, timeBetweenAlerts,
240                     maxNumberOfAlerts );
241 
242             if ( ( listIdUserToSendNextAlert != null ) && ( listIdUserToSendNextAlert.size( ) > 0 ) )
243             {
244                 int nbOtherAlertSent = listIdUserToSendNextAlert.size( );
245                 String strBody = accountLifeTimeService.getOtherAlertMailBody( );
246 
247                 ReferenceItem referenceItem = parameterService.findByKey( PARAMETER_OTHER_ALERT_MAIL_SENDER, plugin );
248                 String strSenderName = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
249 
250                 referenceItem = parameterService.findByKey( PARAMETER_OTHER_ALERT_MAIL_SUBJECT, plugin );
251 
252                 String strSubject = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
253                 String strSenderEmail = MailService.getNoReplyEmail( );
254 
255                 for ( Integer nIdUser : listIdUserToSendNextAlert )
256                 {
257                     try
258                     {
259                         String strUserMail = accountLifeTimeService.getUserMainEmail( nIdUser );
260 
261                         if ( ( strUserMail != null ) && StringUtils.isNotBlank( strUserMail ) )
262                         {
263                             Map<String, String> model = new HashMap<String, String>( );
264                             accountLifeTimeService.addParametersToModel( model, nIdUser );
265 
266                             HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strBody, Locale.getDefault( ), model );
267                             MailService.sendMailHtml( strUserMail, strSenderName, strSenderEmail, strSubject, template.getHtml( ) );
268                         }
269                     }
270                     catch( Exception e )
271                     {
272                         AppLogService.error( getDaemonName( ) + " - Error sending next alert to user : " + e.getMessage( ), e );
273                     }
274                 }
275 
276                 accountLifeTimeService.updateNbAlert( listIdUserToSendNextAlert );
277 
278                 sbLogs = new StringBuilder( );
279                 sbLogs.append( getDaemonName( ) );
280                 sbLogs.append( " - " );
281                 sbLogs.append( Integer.toString( nbOtherAlertSent ) );
282                 sbLogs.append( " next alert(s) have been sent" );
283                 AppLogService.info( sbLogs.toString( ) );
284                 sbResult.append( sbLogs.toString( ) );
285                 sbResult.append( "\n" );
286             }
287             else
288             {
289                 AppLogService.info( getDaemonName( ) + " - No next alert to send" );
290                 sbResult.append( getDaemonName( ) + " - No next alert to send\n" );
291             }
292         }
293 
294         ReferenceItem referenceItem = parameterService.findByKey( PARAMETER_NOTIFY_USER_PASSWORD_EXPIRED, plugin );
295 
296         if ( ( referenceItem != null ) && StringUtils.isNotEmpty( referenceItem.getName( ) ) && referenceItem.isChecked( ) )
297         {
298             // We notify users with expired passwords
299             List<Integer> accountsWithPasswordsExpired = accountLifeTimeService.getIdUsersWithExpiredPasswordsList( currentTimestamp );
300 
301             if ( ( accountsWithPasswordsExpired != null ) && ( accountsWithPasswordsExpired.size( ) > 0 ) )
302             {
303                 referenceItem = parameterService.findByKey( PARAMETER_PASSWORD_EXPIRED_MAIL_SENDER, plugin );
304 
305                 String strSenderName = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
306 
307                 referenceItem = parameterService.findByKey( PARAMETER_PASSWORD_EXPIRED_MAIL_SUBJECT, plugin );
308 
309                 String strSubject = ( referenceItem == null ) ? StringUtils.EMPTY : referenceItem.getName( );
310                 String strSenderEmail = MailService.getNoReplyEmail( );
311 
312                 String strTemplate = accountLifeTimeService.getPasswordExpiredMailBody( );
313 
314                 if ( StringUtils.isNotBlank( strTemplate ) )
315                 {
316                     for ( Integer nIdUser : accountsWithPasswordsExpired )
317                     {
318                         String strUserMail = accountLifeTimeService.getUserMainEmail( nIdUser );
319 
320                         if ( StringUtils.isNotBlank( strUserMail ) )
321                         {
322                             Map<String, String> model = new HashMap<String, String>( );
323                             accountLifeTimeService.addParametersToModel( model, nIdUser );
324 
325                             HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strTemplate, Locale.getDefault( ), model );
326 
327                             MailService.sendMailHtml( strUserMail, strSenderName, strSenderEmail, strSubject, template.getHtml( ) );
328                         }
329                     }
330                 }
331 
332                 accountLifeTimeService.updateChangePassword( accountsWithPasswordsExpired );
333                 sbLogs = new StringBuilder( );
334                 sbLogs.append( getDaemonName( ) );
335                 sbLogs.append( " - " );
336                 sbLogs.append( Integer.toString( accountsWithPasswordsExpired.size( ) ) );
337                 sbLogs.append( " user(s) have been notified their password has expired" );
338                 AppLogService.info( sbLogs.toString( ) );
339                 sbResult.append( sbLogs.toString( ) );
340                 sbResult.append( "\n" );
341             }
342             else
343             {
344                 AppLogService.info( getDaemonName( ) + " - No expired passwords" );
345                 sbResult.append( getDaemonName( ) + " - No expired passwords" );
346             }
347         }
348         else
349         {
350             AppLogService.info( getDaemonName( ) + " - Expired passwords notification deactivated, skipping" );
351             sbResult.append( getDaemonName( ) + " - Expired passwords notification deactivated, skipping" );
352         }
353 
354         this.setLastRunLogs( sbResult.toString( ) );
355     }
356 }