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.modules.database.authentication.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.password.IPassword;
38  import fr.paris.lutece.util.password.IPasswordFactory;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.sql.Timestamp;
42  
43  import java.util.ArrayList;
44  import java.util.Collection;
45  import java.util.List;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.collections.CollectionUtils;
50  
51  /**
52   * This class provides Data Access methods for databaseUser objects
53   */
54  public class DatabaseUserDAO implements IDatabaseUserDAO
55  {
56      // Constants
57      private static final String PERCENT = "%";
58      private static final String SQL_QUERY_NEW_PK = " SELECT max( mylutece_database_user_id ) FROM mylutece_database_user ";
59      private static final String SQL_QUERY_SELECT = " SELECT mylutece_database_user_id, login, name_family, name_given, email, is_active, account_max_valid_date FROM mylutece_database_user WHERE mylutece_database_user_id = ?";
60      private static final String SQL_QUERY_INSERT = " INSERT INTO mylutece_database_user ( mylutece_database_user_id, login, name_family, name_given, email, is_active, password, password_max_valid_date, account_max_valid_date ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
61      private static final String SQL_QUERY_DELETE = " DELETE FROM mylutece_database_user WHERE mylutece_database_user_id = ?  ";
62      private static final String SQL_QUERY_UPDATE = " UPDATE mylutece_database_user SET login = ?, name_family = ?, name_given = ?, email = ?, is_active = ? WHERE mylutece_database_user_id = ? ";
63      private static final String SQL_QUERY_UPDATE_PASSWORD = " UPDATE mylutece_database_user SET password = ?, password_max_valid_date = ? WHERE mylutece_database_user_id = ? ";
64      private static final String SQL_QUERY_UPDATE_RESET_PASSWORD = " UPDATE mylutece_database_user SET reset_password = ? WHERE mylutece_database_user_id = ? ";
65      private static final String SQL_QUERY_SELECTALL = " SELECT mylutece_database_user_id, login, name_family, name_given, email, is_active, account_max_valid_date, password_max_valid_date FROM mylutece_database_user ORDER BY name_family, login";
66      private static final String SQL_QUERY_SELECTALL_FOR_LOGIN = " SELECT mylutece_database_user_id, login, name_family, name_given, email, is_active FROM mylutece_database_user WHERE login = ? ";
67      private static final String SQL_QUERY_SELECTALL_FOR_EMAIL = " SELECT mylutece_database_user_id, login, name_family, name_given, email, is_active FROM mylutece_database_user WHERE email = ? ";
68      private static final String SQL_QUERY_CHECK_PASSWORD_FOR_USER_ID = " SELECT password FROM mylutece_database_user WHERE login = ?";
69      private static final String SQL_QUERY_SELECT_USER_FROM_SEARCH = " SELECT mylutece_database_user_id, login, name_family, name_given, email, is_active FROM mylutece_database_user "
70              + " WHERE login LIKE ? AND name_family LIKE ? and name_given LIKE ? AND email LIKE ? ORDER BY name_family ";
71      private static final String SQL_SELECT_USER_ID_FROM_PASSWORD = "SELECT mylutece_database_user_id FROM mylutece_database_user WHERE login = ?";
72      private static final String SQL_SELECT_USER_PASSWORD_HISTORY = "SELECT password FROM mylutece_database_user_password_history WHERE mylutece_database_user_id = ? ORDER BY date_password_change desc";
73      private static final String SQL_COUNT_USER_PASSWORD_HISTORY = "SELECT COUNT(*) FROM mylutece_database_user_password_history WHERE mylutece_database_user_id = ? AND date_password_change > ?";
74      private static final String SQL_INSERT_PASSWORD_HISTORY = "INSERT INTO mylutece_database_user_password_history (mylutece_database_user_id, password) VALUES ( ?, ? ) ";
75      private static final String SQL_DELETE_PASSWORD_HISTORY = "DELETE FROM mylutece_database_user_password_history WHERE mylutece_database_user_id = ?";
76      private static final String SQL_QUERY_SELECT_EXPIRED_USER_ID = "SELECT mylutece_database_user_id FROM mylutece_database_user WHERE is_active = ?";
77      private static final String SQL_QUERY_SELECT_EXPIRED_LIFE_TIME_USER_ID = "SELECT mylutece_database_user_id FROM mylutece_database_user WHERE account_max_valid_date < ? and is_active < ? ";
78      private static final String SQL_QUERY_SELECT_USER_ID_FIRST_ALERT = "SELECT mylutece_database_user_id FROM mylutece_database_user WHERE nb_alerts_sent = 0 and is_active < ? and account_max_valid_date < ? ";
79      private static final String SQL_QUERY_SELECT_USER_ID_OTHER_ALERT = "SELECT mylutece_database_user_id FROM mylutece_database_user "
80              + "WHERE nb_alerts_sent > 0 and nb_alerts_sent <= ? and is_active < ? and (account_max_valid_date + nb_alerts_sent * ?) < ? ";
81      private static final String SQL_QUERY_SELECT_USER_ID_PASSWORD_EXPIRED = " SELECT mylutece_database_user_id FROM mylutece_database_user WHERE password_max_valid_date < ? AND reset_password = 0 ";
82      private static final String SQL_QUERY_UPDATE_STATUS = " UPDATE mylutece_database_user SET is_active = ? WHERE mylutece_database_user_id IN ( ";
83      private static final String SQL_QUERY_UPDATE_NB_ALERT = " UPDATE mylutece_database_user SET nb_alerts_sent = nb_alerts_sent + 1 WHERE mylutece_database_user_id IN ( ";
84      private static final String SQL_QUERY_UPDATE_RESET_PASSWORD_LIST_ID = " UPDATE mylutece_database_user SET reset_password = 1 WHERE mylutece_database_user_id IN ( ";
85      private static final String SQL_QUERY_UPDATE_LAST_LOGGIN_DATE = " UPDATE mylutece_database_user SET last_login = ? WHERE login LIKE ? ";
86      private static final String SQL_QUERY_UPDATE_REACTIVATE_ACCOUNT = " UPDATE mylutece_database_user SET nb_alerts_sent = 0, account_max_valid_date = ? WHERE mylutece_database_user_id = ? ";
87      private static final String SQL_QUERY_SELECT_NB_ALERT_SENT = " SELECT nb_alerts_sent FROM mylutece_database_user WHERE mylutece_database_user_id = ? ";
88      private static final String CONSTANT_CLOSE_PARENTHESIS = " ) ";
89      private static final String CONSTANT_COMMA = ", ";
90  
91      @Inject
92      IPasswordFactory _passwordFactory;
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public int newPrimaryKey( Plugin plugin )
99      {
100         int nKey = 1;
101         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
102         {
103             daoUtil.executeQuery( );
104 
105             if ( daoUtil.next( ) )
106             {
107                 nKey = daoUtil.getInt( 1 ) + 1;
108             }
109 
110         }
111 
112         return nKey;
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public void insert( DatabaseUser databaseUser, IPassword password, Plugin plugin )
120     {
121         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
122         {
123             databaseUser.setUserId( newPrimaryKey( plugin ) );
124             daoUtil.setInt( 1, databaseUser.getUserId( ) );
125             daoUtil.setString( 2, databaseUser.getLogin( ) );
126             daoUtil.setString( 3, databaseUser.getLastName( ) );
127             daoUtil.setString( 4, databaseUser.getFirstName( ) );
128             daoUtil.setString( 5, databaseUser.getEmail( ) );
129             daoUtil.setInt( 6, databaseUser.getStatus( ) );
130             daoUtil.setString( 7, password.getStorableRepresentation( ) );
131             daoUtil.setTimestamp( 8, databaseUser.getPasswordMaxValidDate( ) );
132 
133             if ( databaseUser.getAccountMaxValidDate( ) == null )
134             {
135                 daoUtil.setLongNull( 9 );
136             }
137             else
138             {
139                 daoUtil.setLong( 9, databaseUser.getAccountMaxValidDate( ).getTime( ) );
140             }
141 
142             daoUtil.executeUpdate( );
143         }
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public DatabaseUser load( int nUserId, Plugin plugin )
151     {
152         DatabaseUser databaseUser = null;
153         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
154         {
155             daoUtil.setInt( 1, nUserId );
156             daoUtil.executeQuery( );
157 
158             if ( daoUtil.next( ) )
159             {
160                 databaseUser = DatabaseUserFactory.getFactory( ).newDatabaseUser( );
161                 databaseUser.setUserId( daoUtil.getInt( 1 ) );
162                 databaseUser.setLogin( daoUtil.getString( 2 ) );
163                 databaseUser.setLastName( daoUtil.getString( 3 ) );
164                 databaseUser.setFirstName( daoUtil.getString( 4 ) );
165                 databaseUser.setEmail( daoUtil.getString( 5 ) );
166                 databaseUser.setStatus( daoUtil.getInt( 6 ) );
167 
168                 long accountTime = daoUtil.getLong( 7 );
169 
170                 if ( accountTime > 0 )
171                 {
172                     databaseUser.setAccountMaxValidDate( new Timestamp( accountTime ) );
173                 }
174             }
175         }
176         return databaseUser;
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     public void delete( DatabaseUser databaseUser, Plugin plugin )
184     {
185         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
186         {
187             daoUtil.setInt( 1, databaseUser.getUserId( ) );
188             daoUtil.executeUpdate( );
189         }
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public void store( DatabaseUser databaseUser, Plugin plugin )
197     {
198         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
199         {
200             daoUtil.setString( 1, databaseUser.getLogin( ) );
201             daoUtil.setString( 2, databaseUser.getLastName( ) );
202             daoUtil.setString( 3, databaseUser.getFirstName( ) );
203             daoUtil.setString( 4, databaseUser.getEmail( ) );
204             daoUtil.setInt( 5, databaseUser.getStatus( ) );
205 
206             daoUtil.setInt( 6, databaseUser.getUserId( ) );
207 
208             daoUtil.executeUpdate( );
209         }
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public void updatePassword( DatabaseUser databaseUser, IPassword newPassword, Plugin plugin )
217     {
218         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_PASSWORD, plugin ) )
219         {
220             daoUtil.setString( 1, newPassword.getStorableRepresentation( ) );
221             daoUtil.setTimestamp( 2, databaseUser.getPasswordMaxValidDate( ) );
222             daoUtil.setInt( 3, databaseUser.getUserId( ) );
223 
224             daoUtil.executeUpdate( );
225         }
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public void updateResetPassword( DatabaseUser databaseUser, boolean bNewValue, Plugin plugin )
233     {
234         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_RESET_PASSWORD, plugin ) )
235         {
236             daoUtil.setBoolean( 1, bNewValue );
237             daoUtil.setInt( 2, databaseUser.getUserId( ) );
238 
239             daoUtil.executeUpdate( );
240         }
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     @Override
247     public Collection<DatabaseUser> selectDatabaseUserList( Plugin plugin )
248     {
249         Collection<DatabaseUser> listDatabaseUsers = new ArrayList<>( );
250         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
251         {
252             daoUtil.executeQuery( );
253 
254             while ( daoUtil.next( ) )
255             {
256                 DatabaseUser databaseUser = DatabaseUserFactory.getFactory( ).newDatabaseUser( );
257                 databaseUser.setUserId( daoUtil.getInt( 1 ) );
258                 databaseUser.setLogin( daoUtil.getString( 2 ) );
259                 databaseUser.setLastName( daoUtil.getString( 3 ) );
260                 databaseUser.setFirstName( daoUtil.getString( 4 ) );
261                 databaseUser.setEmail( daoUtil.getString( 5 ) );
262                 databaseUser.setStatus( daoUtil.getInt( 6 ) );
263 
264                 long accountTime = daoUtil.getLong( 7 );
265 
266                 if ( accountTime > 0 )
267                 {
268                     databaseUser.setAccountMaxValidDate( new Timestamp( accountTime ) );
269                 }
270 
271                 databaseUser.setPasswordMaxValidDate( daoUtil.getTimestamp( 8 ) );
272                 listDatabaseUsers.add( databaseUser );
273             }
274         }
275         return listDatabaseUsers;
276     }
277 
278     /**
279      * {@inheritDoc}
280      */
281     @Override
282     public Collection<DatabaseUser> selectDatabaseUserListForLogin( String strLogin, Plugin plugin )
283     {
284         Collection<DatabaseUser> listDatabaseUsers = new ArrayList<>( );
285         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_LOGIN, plugin ) )
286         {
287             daoUtil.setString( 1, strLogin );
288             daoUtil.executeQuery( );
289 
290             while ( daoUtil.next( ) )
291             {
292                 DatabaseUser databaseUser = DatabaseUserFactory.getFactory( ).newDatabaseUser( );
293                 databaseUser.setUserId( daoUtil.getInt( 1 ) );
294                 databaseUser.setLogin( daoUtil.getString( 2 ) );
295                 databaseUser.setLastName( daoUtil.getString( 3 ) );
296                 databaseUser.setFirstName( daoUtil.getString( 4 ) );
297                 databaseUser.setEmail( daoUtil.getString( 5 ) );
298                 databaseUser.setStatus( daoUtil.getInt( 6 ) );
299 
300                 listDatabaseUsers.add( databaseUser );
301             }
302         }
303         return listDatabaseUsers;
304     }
305 
306     /**
307      * {@inheritDoc}
308      */
309     @Override
310     public Collection<DatabaseUser> selectDatabaseUserListForEmail( String strEmail, Plugin plugin )
311     {
312         Collection<DatabaseUser> listDatabaseUsers = new ArrayList<>( );
313         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_EMAIL, plugin ) )
314         {
315             daoUtil.setString( 1, strEmail );
316             daoUtil.executeQuery( );
317 
318             while ( daoUtil.next( ) )
319             {
320                 DatabaseUser databaseUser = DatabaseUserFactory.getFactory( ).newDatabaseUser( );
321                 databaseUser.setUserId( daoUtil.getInt( 1 ) );
322                 databaseUser.setLogin( daoUtil.getString( 2 ) );
323                 databaseUser.setLastName( daoUtil.getString( 3 ) );
324                 databaseUser.setFirstName( daoUtil.getString( 4 ) );
325                 databaseUser.setEmail( daoUtil.getString( 5 ) );
326                 databaseUser.setStatus( daoUtil.getInt( 6 ) );
327 
328                 listDatabaseUsers.add( databaseUser );
329             }
330         }
331         return listDatabaseUsers;
332     }
333 
334     /**
335      * {@inheritDoc}
336      */
337     @Override
338     public IPassword loadPassword( String strLogin, Plugin plugin )
339     {
340         IPassword password = _passwordFactory.getDummyPassword( );
341         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PASSWORD_FOR_USER_ID, plugin ) )
342         {
343             daoUtil.setString( 1, strLogin );
344             daoUtil.executeQuery( );
345             if ( daoUtil.next( ) )
346             {
347                 password = _passwordFactory.getPassword( daoUtil.getString( 1 ) );
348             }
349         }
350         return password;
351     }
352 
353     /**
354      * {@inheritDoc}
355      */
356     @Override
357     public List<DatabaseUser> selectDatabaseUsersListByFilter( DatabaseUserFilter duFilter, Plugin plugin )
358     {
359         List<DatabaseUser> listFilteredUsers = new ArrayList<>( );
360         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_FROM_SEARCH, plugin ) )
361         {
362             daoUtil.setString( 1, PERCENT + duFilter.getLogin( ) + PERCENT );
363             daoUtil.setString( 2, PERCENT + duFilter.getLastName( ) + PERCENT );
364             daoUtil.setString( 3, PERCENT + duFilter.getFirstName( ) + PERCENT );
365             daoUtil.setString( 4, PERCENT + duFilter.getEmail( ) + PERCENT );
366 
367             daoUtil.executeQuery( );
368 
369             while ( daoUtil.next( ) )
370             {
371                 DatabaseUser filteredUser = DatabaseUserFactory.getFactory( ).newDatabaseUser( );
372                 filteredUser.setUserId( daoUtil.getInt( 1 ) );
373                 filteredUser.setLogin( daoUtil.getString( 2 ) );
374                 filteredUser.setLastName( daoUtil.getString( 3 ) );
375                 filteredUser.setFirstName( daoUtil.getString( 4 ) );
376                 filteredUser.setEmail( daoUtil.getString( 5 ) );
377                 filteredUser.setStatus( daoUtil.getInt( 6 ) );
378                 listFilteredUsers.add( filteredUser );
379             }
380         }
381         return listFilteredUsers;
382     }
383 
384     /**
385      * {@inheritDoc}
386      */
387     @Override
388     public int findDatabaseUserIdFromLogin( String strLogin, Plugin plugin )
389     {
390         int nRecordId = 0;
391 
392         try ( DAOUtil daoUtil = new DAOUtil( SQL_SELECT_USER_ID_FROM_PASSWORD, plugin ) )
393         {
394             daoUtil.setString( 1, strLogin );
395             daoUtil.executeQuery( );
396 
397             if ( daoUtil.next( ) )
398             {
399                 nRecordId = daoUtil.getInt( 1 );
400             }
401         }
402         return nRecordId;
403     }
404 
405     /**
406      * {@inheritDoc}
407      */
408     @Override
409     public List<IPassword> selectUserPasswordHistory( int nUserID, Plugin plugin )
410     {
411         List<IPassword> listPasswordHistory = new ArrayList<>( );
412 
413         try ( DAOUtil daoUtil = new DAOUtil( SQL_SELECT_USER_PASSWORD_HISTORY, plugin ) )
414         {
415             daoUtil.setInt( 1, nUserID );
416             daoUtil.executeQuery( );
417 
418             while ( daoUtil.next( ) )
419             {
420                 listPasswordHistory.add( _passwordFactory.getPassword( daoUtil.getString( 1 ) ) );
421             }
422         }
423         return listPasswordHistory;
424     }
425 
426     /**
427      * {@inheritDoc}
428      */
429     @Override
430     public int countUserPasswordHistoryFromDate( Timestamp minDate, int nUserId, Plugin plugin )
431     {
432         int nNbRes = 0;
433 
434         try ( DAOUtil daoUtil = new DAOUtil( SQL_COUNT_USER_PASSWORD_HISTORY, plugin ) )
435         {
436             daoUtil.setInt( 1, nUserId );
437             daoUtil.setTimestamp( 2, minDate );
438             daoUtil.executeQuery( );
439 
440             if ( daoUtil.next( ) )
441             {
442                 nNbRes = daoUtil.getInt( 1 );
443             }
444         }
445         return nNbRes;
446     }
447 
448     /**
449      * {@inheritDoc}
450      */
451     @Override
452     public void insertNewPasswordInHistory( IPassword password, int nUserId, Plugin plugin )
453     {
454         try ( DAOUtil daoUtil = new DAOUtil( SQL_INSERT_PASSWORD_HISTORY, plugin ) )
455         {
456             daoUtil.setInt( 1, nUserId );
457             daoUtil.setString( 2, password.getStorableRepresentation( ) );
458 
459             daoUtil.executeUpdate( );
460         }
461     }
462 
463     /**
464      * {@inheritDoc}
465      */
466     @Override
467     public void removeAllPasswordHistoryForUser( int nUserId, Plugin plugin )
468     {
469         try ( DAOUtil daoUtil = new DAOUtil( SQL_DELETE_PASSWORD_HISTORY, plugin ) )
470         {
471             daoUtil.setInt( 1, nUserId );
472             daoUtil.executeUpdate( );
473         }
474     }
475 
476     /**
477      * {@inheritDoc}
478      */
479     @Override
480     public List<Integer> findAllExpiredUserId( Plugin plugin )
481     {
482         List<Integer> listIdExpiredUser = new ArrayList<>( );
483         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EXPIRED_USER_ID, plugin ) )
484         {
485             daoUtil.setInt( 1, DatabaseUser.STATUS_EXPIRED );
486             daoUtil.executeQuery( );
487 
488             while ( daoUtil.next( ) )
489             {
490                 listIdExpiredUser.add( daoUtil.getInt( 1 ) );
491             }
492         }
493         return listIdExpiredUser;
494     }
495 
496     /**
497      * {@inheritDoc}
498      */
499     @Override
500     public List<Integer> getIdUsersWithExpiredLifeTimeList( Timestamp currentTimestamp, Plugin plugin )
501     {
502         List<Integer> listIdExpiredUser = new ArrayList<>( );
503         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EXPIRED_LIFE_TIME_USER_ID, plugin ) )
504         {
505             daoUtil.setLong( 1, currentTimestamp.getTime( ) );
506             daoUtil.setInt( 2, DatabaseUser.STATUS_EXPIRED );
507             daoUtil.executeQuery( );
508 
509             while ( daoUtil.next( ) )
510             {
511                 listIdExpiredUser.add( daoUtil.getInt( 1 ) );
512             }
513         }
514         return listIdExpiredUser;
515     }
516 
517     /**
518      * {@inheritDoc}
519      */
520     @Override
521     public List<Integer> getIdUsersToSendFirstAlert( Timestamp alertMaxDate, Plugin plugin )
522     {
523         List<Integer> listIdUserFirstAlertlist = new ArrayList<>( );
524         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_ID_FIRST_ALERT, plugin ) )
525         {
526             daoUtil.setInt( 1, DatabaseUser.STATUS_EXPIRED );
527             daoUtil.setLong( 2, alertMaxDate.getTime( ) );
528 
529             daoUtil.executeQuery( );
530             while ( daoUtil.next( ) )
531             {
532                 listIdUserFirstAlertlist.add( daoUtil.getInt( 1 ) );
533             }
534         }
535         return listIdUserFirstAlertlist;
536     }
537 
538     /**
539      * {@inheritDoc}
540      */
541     @Override
542     public List<Integer> getIdUsersToSendOtherAlert( Timestamp alertMaxDate, Timestamp timeBetweenAlerts, int maxNumberAlerts, Plugin plugin )
543     {
544         List<Integer> listIdUserFirstAlert = new ArrayList<>( );
545         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_ID_OTHER_ALERT, plugin ) )
546         {
547             daoUtil.setInt( 1, maxNumberAlerts );
548             daoUtil.setInt( 2, DatabaseUser.STATUS_EXPIRED );
549             daoUtil.setLong( 3, timeBetweenAlerts.getTime( ) );
550             daoUtil.setLong( 4, alertMaxDate.getTime( ) );
551 
552             daoUtil.executeQuery( );
553 
554             while ( daoUtil.next( ) )
555             {
556                 listIdUserFirstAlert.add( daoUtil.getInt( 1 ) );
557             }
558         }
559         return listIdUserFirstAlert;
560     }
561 
562     /**
563      * {@inheritDoc}
564      */
565     @Override
566     public List<Integer> getIdUsersWithExpiredPasswordsList( Timestamp currentTimestamp, Plugin plugin )
567     {
568         List<Integer> idUserPasswordExpiredlist = new ArrayList<>( );
569         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_ID_PASSWORD_EXPIRED, plugin ) )
570         {
571             daoUtil.setTimestamp( 1, currentTimestamp );
572             daoUtil.executeQuery( );
573 
574             while ( daoUtil.next( ) )
575             {
576                 idUserPasswordExpiredlist.add( daoUtil.getInt( 1 ) );
577             }
578         }
579         return idUserPasswordExpiredlist;
580     }
581 
582     /**
583      * {@inheritDoc}
584      */
585     @Override
586     public void updateUserStatus( List<Integer> listIdUser, int nNewStatus, Plugin plugin )
587     {
588         if ( CollectionUtils.isNotEmpty( listIdUser ) )
589         {
590             StringBuilder sbSQL = new StringBuilder( );
591             sbSQL.append( SQL_QUERY_UPDATE_STATUS );
592 
593             for ( int i = 0; i < listIdUser.size( ); i++ )
594             {
595                 if ( i > 0 )
596                 {
597                     sbSQL.append( CONSTANT_COMMA );
598                 }
599 
600                 sbSQL.append( listIdUser.get( i ) );
601             }
602 
603             sbSQL.append( CONSTANT_CLOSE_PARENTHESIS );
604 
605             try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
606             {
607                 daoUtil.setInt( 1, nNewStatus );
608                 daoUtil.executeUpdate( );
609             }
610         }
611     }
612 
613     /**
614      * {@inheritDoc}
615      */
616     @Override
617     public void updateNbAlert( List<Integer> listIdUser, Plugin plugin )
618     {
619         if ( CollectionUtils.isNotEmpty( listIdUser ) )
620         {
621             StringBuilder sbSQL = new StringBuilder( );
622             sbSQL.append( SQL_QUERY_UPDATE_NB_ALERT );
623 
624             for ( int i = 0; i < listIdUser.size( ); i++ )
625             {
626                 if ( i > 0 )
627                 {
628                     sbSQL.append( CONSTANT_COMMA );
629                 }
630 
631                 sbSQL.append( listIdUser.get( i ) );
632             }
633 
634             sbSQL.append( CONSTANT_CLOSE_PARENTHESIS );
635 
636             try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
637             {
638                 daoUtil.executeUpdate( );
639             }
640         }
641     }
642 
643     /**
644      * {@inheritDoc}
645      */
646     @Override
647     public void updateChangePassword( List<Integer> listIdUser, Plugin plugin )
648     {
649         if ( CollectionUtils.isNotEmpty( listIdUser ) )
650         {
651             StringBuilder sbSQL = new StringBuilder( );
652             sbSQL.append( SQL_QUERY_UPDATE_RESET_PASSWORD_LIST_ID );
653 
654             for ( int i = 0; i < listIdUser.size( ); i++ )
655             {
656                 if ( i > 0 )
657                 {
658                     sbSQL.append( CONSTANT_COMMA );
659                 }
660 
661                 sbSQL.append( listIdUser.get( i ) );
662             }
663 
664             sbSQL.append( CONSTANT_CLOSE_PARENTHESIS );
665 
666             try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
667             {
668                 daoUtil.executeUpdate( );
669             }
670         }
671     }
672 
673     /**
674      * {@inheritDoc}
675      */
676     @Override
677     public void updateUserExpirationDate( int nIdUser, Timestamp newExpirationDate, Plugin plugin )
678     {
679         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_REACTIVATE_ACCOUNT, plugin ) )
680         {
681             if ( newExpirationDate == null )
682             {
683                 daoUtil.setLongNull( 1 );
684             }
685             else
686             {
687                 daoUtil.setLong( 1, newExpirationDate.getTime( ) );
688             }
689 
690             daoUtil.setInt( 2, nIdUser );
691 
692             daoUtil.executeUpdate( );
693         }
694     }
695 
696     /**
697      * {@inheritDoc}
698      */
699     @Override
700     public int getNbAccountLifeTimeNotification( int nIdUser, Plugin plugin )
701     {
702         int nRes = 0;
703         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NB_ALERT_SENT, plugin ) )
704         {
705             daoUtil.setInt( 1, nIdUser );
706             daoUtil.executeQuery( );
707 
708             if ( daoUtil.next( ) )
709             {
710                 nRes = daoUtil.getInt( 1 );
711             }
712         }
713         return nRes;
714     }
715 
716     /**
717      * {@inheritDoc}
718      */
719     @Override
720     public void updateUserLastLoginDate( String strLogin, Timestamp dateLastLogin, Plugin plugin )
721     {
722         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_LAST_LOGGIN_DATE, plugin ) )
723         {
724             daoUtil.setTimestamp( 1, dateLastLogin );
725             daoUtil.setString( 2, strLogin );
726             daoUtil.executeUpdate( );
727         }
728     }
729 }