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.portal.service.security.LuteceUserService;
38 import fr.paris.lutece.portal.service.spring.SpringContextService;
39 import fr.paris.lutece.util.password.IPassword;
40 import fr.paris.lutece.util.password.IPasswordFactory;
41
42 import java.sql.Timestamp;
43
44 import java.util.Collection;
45 import java.util.Date;
46 import java.util.List;
47
48 /**
49 * This class provides instances management methods (create, find, ...) for DatabaseUser objects
50 */
51 public final class DatabaseUserHome
52 {
53 // Static variable pointed at the DAO instance
54 private static IDatabaseUserDAO _dao = SpringContextService.getBean( "mylutece-database.databaseUserDAO" );
55 private static IPasswordFactory _passwordFactory = SpringContextService.getBean( IPasswordFactory.BEAN_NAME );
56
57 /**
58 * Private constructor - this class need not be instantiated
59 */
60 private DatabaseUserHome( )
61 {
62 }
63
64 /**
65 * Creation of an instance of databaseUser
66 *
67 * @param databaseUser
68 * The instance of the DatabaseUser which contains the informations to store
69 * @param password
70 * The user's password
71 * @param plugin
72 * The current plugin using this method
73 * @return The instance of DatabaseUser which has been created with its primary key.
74 */
75 public static DatabaseUser./../../../../../../../fr/paris/lutece/plugins/mylutece/modules/database/authentication/business/DatabaseUser.html#DatabaseUser">DatabaseUser create( DatabaseUser databaseUser, IPassword password, Plugin plugin )
76 {
77 _dao.insert( databaseUser, password, plugin );
78
79 return databaseUser;
80 }
81
82 /**
83 * Update of the databaseUser which is specified in parameter
84 *
85 * @param databaseUser
86 * The instance of the DatabaseUser which contains the data to store
87 * @param plugin
88 * The current plugin using this method
89 * @return The instance of the DatabaseUser which has been updated
90 */
91 public static DatabaseUser./../../../../../../../fr/paris/lutece/plugins/mylutece/modules/database/authentication/business/DatabaseUser.html#DatabaseUser">DatabaseUser update( DatabaseUser databaseUser, Plugin plugin )
92 {
93 _dao.store( databaseUser, plugin );
94 LuteceUserService.userAttributesChanged( databaseUser.getLogin( ) );
95
96 return databaseUser;
97 }
98
99 /**
100 * Update of the databaseUser which is specified in parameter
101 *
102 * @param databaseUser
103 * The instance of the DatabaseUser which contains the data to store
104 * @param newPassword
105 * The new password to store
106 * @param plugin
107 * The current plugin using this method
108 * @return The instance of the DatabaseUser which has been updated
109 */
110 public static DatabaseUser../../../../../fr/paris/lutece/plugins/mylutece/modules/database/authentication/business/DatabaseUser.html#DatabaseUser">DatabaseUser updatePassword( DatabaseUser databaseUser, IPassword newPassword, Plugin plugin )
111 {
112 _dao.updatePassword( databaseUser, newPassword, plugin );
113
114 return databaseUser;
115 }
116
117 /**
118 * Update of the databaseUser which is specified in parameter
119 *
120 * @param user
121 * The instance of the DatabaseUser which contains the data to store
122 * @param bNewValue
123 * The new value of the reset password attribute
124 * @param plugin
125 * The current plugin using this method
126 * @return The instance of the DatabaseUser which has been updated
127 */
128 public static DatabaseUser/../../../fr/paris/lutece/plugins/mylutece/modules/database/authentication/business/DatabaseUser.html#DatabaseUser">DatabaseUser updateResetPassword( DatabaseUser user, boolean bNewValue, Plugin plugin )
129 {
130 _dao.updateResetPassword( user, bNewValue, plugin );
131
132 return user;
133 }
134
135 /**
136 * Remove the databaseUser whose identifier is specified in parameter
137 *
138 * @param databaseUser
139 * The DatabaseUser object to remove
140 * @param plugin
141 * The current plugin using this method
142 */
143 public static void remove( DatabaseUser databaseUser, Plugin plugin )
144 {
145 _dao.delete( databaseUser, plugin );
146 _dao.removeAllPasswordHistoryForUser( databaseUser.getUserId( ), plugin );
147 LuteceUserService.userAttributesChanged( databaseUser.getLogin( ) );
148 }
149
150 // /////////////////////////////////////////////////////////////////////////
151 // Finders
152
153 /**
154 * Returns an instance of a DatabaseUser whose identifier is specified in parameter
155 *
156 * @param nKey
157 * The Primary key of the databaseUser
158 * @param plugin
159 * The current plugin using this method
160 * @return An instance of DatabaseUser
161 */
162 public static DatabaseUser findByPrimaryKey( int nKey, Plugin plugin )
163 {
164 return _dao.load( nKey, plugin );
165 }
166
167 /**
168 * Returns a collection of DatabaseUser objects
169 *
170 * @param plugin
171 * The current plugin using this method
172 * @return A collection of DatabaseUser
173 */
174 public static Collection<DatabaseUser> findDatabaseUsersList( Plugin plugin )
175 {
176 return _dao.selectDatabaseUserList( plugin );
177 }
178
179 /**
180 * Returns a collection of DatabaseUser objects for a login
181 *
182 * @param strLogin
183 * The login of the databseUser
184 * @param plugin
185 * The current plugin using this method
186 * @return A collection of DatabaseUser
187 */
188 public static Collection<DatabaseUser> findDatabaseUsersListForLogin( String strLogin, Plugin plugin )
189 {
190 return _dao.selectDatabaseUserListForLogin( strLogin, plugin );
191 }
192
193 /**
194 * Returns a collection of DatabaseUser objects for a email
195 *
196 * @param strEmail
197 * The email of the databseUser
198 * @param plugin
199 * The current plugin using this method
200 * @return A collection of DatabaseUser
201 */
202 public static Collection<DatabaseUser> findDatabaseUsersListForEmail( String strEmail, Plugin plugin )
203 {
204 return _dao.selectDatabaseUserListForEmail( strEmail, plugin );
205 }
206
207 /**
208 * Check the password for a DatabaseUser
209 *
210 * @param strLogin
211 * The user login of DatabaseUser
212 * @param strPassword
213 * The password of DatabaseUser
214 * @param plugin
215 * The Plugin using this data access service
216 * @return true if password is ok
217 */
218 public static boolean checkPassword( String strLogin, String strPassword, Plugin plugin )
219 {
220 IPassword storedPassword = _dao.loadPassword( strLogin, plugin );
221 boolean check = storedPassword.check( strPassword );
222 if ( check && storedPassword.isLegacy( ) )
223 {
224 // upgrade password storage
225 int nUserId = findDatabaseUserIdFromLogin( strLogin, plugin );
226 DatabaseUser databaseUser = findByPrimaryKey( nUserId, plugin );
227 updatePassword( databaseUser, _passwordFactory.getPasswordFromCleartext( strPassword ), plugin );
228 }
229 return check;
230 }
231
232 /**
233 * Find DatabaseUsers by filter
234 *
235 * @param duFilter
236 * filter
237 * @param plugin
238 * The plugin
239 * @return a list of DatabaseUsers
240 */
241 public static List<DatabaseUser> findDatabaseUsersListByFilter( DatabaseUserFilter duFilter, Plugin plugin )
242 {
243 return _dao.selectDatabaseUsersListByFilter( duFilter, plugin );
244 }
245
246 /**
247 * Get a user id from his login
248 *
249 * @param strLogin
250 * The login of the user
251 * @param plugin
252 * The plugin
253 * @return The user id, or 0 if no user has this login.
254 */
255 public static int findDatabaseUserIdFromLogin( String strLogin, Plugin plugin )
256 {
257 return _dao.findDatabaseUserIdFromLogin( strLogin, plugin );
258 }
259
260 /**
261 * Gets the history of password of the given user
262 *
263 * @param nUserID
264 * Id of the user
265 * @param plugin
266 * The plugin
267 * @return The collection of recent passwords used by the user.
268 */
269 public static List<IPassword> selectUserPasswordHistory( int nUserID, Plugin plugin )
270 {
271 return _dao.selectUserPasswordHistory( nUserID, plugin );
272 }
273
274 /**
275 * Get the number of password change done by a user since the given date.
276 *
277 * @param minDate
278 * Minimum date to consider.
279 * @param nUserId
280 * Id of the user
281 * @param plugin
282 * The plugin
283 * @return The number of password change done by the user since the given date.
284 */
285 public static int countUserPasswordHistoryFromDate( Timestamp minDate, int nUserId, Plugin plugin )
286 {
287 return _dao.countUserPasswordHistoryFromDate( minDate, nUserId, plugin );
288 }
289
290 /**
291 * Log a password change in the password history
292 *
293 * @param strPassword
294 * New password of the user
295 * @param nUserId
296 * Id of the user
297 * @param plugin
298 * The plugin
299 */
300 public static void insertNewPasswordInHistory( IPassword password, int nUserId, Plugin plugin )
301 {
302 _dao.insertNewPasswordInHistory( password, nUserId, plugin );
303 }
304
305 /**
306 * Remove every password saved in the password history for a user.
307 *
308 * @param nUserId
309 * Id of the user
310 * @param plugin
311 * The plugin
312 */
313 public static void removeAllPasswordHistoryForUser( int nUserId, Plugin plugin )
314 {
315 _dao.removeAllPasswordHistoryForUser( nUserId, plugin );
316 }
317
318 /**
319 * Get the list of id of user with the expired status.
320 *
321 * @param plugin
322 * The plugin
323 * @return The list of id of user with the expired status.
324 */
325 public static List<Integer> findAllExpiredUserId( Plugin plugin )
326 {
327 return _dao.findAllExpiredUserId( plugin );
328 }
329
330 /**
331 * Get the list of id of users that have an expired time life but not the expired status
332 *
333 * @param currentTimestamp
334 * Timestamp describing the current time.
335 * @param plugin
336 * The plugin
337 * @return the list of id of users with expired time life
338 */
339 public static List<Integer> getIdUsersWithExpiredLifeTimeList( Timestamp currentTimestamp, Plugin plugin )
340 {
341 return _dao.getIdUsersWithExpiredLifeTimeList( currentTimestamp, plugin );
342 }
343
344 /**
345 * Get the list of id of users that need to receive their first alert
346 *
347 * @param firstAlertMaxDate
348 * The maximum expiration date to send first alert.
349 * @param plugin
350 * The plugin
351 * @return the list of id of users that need to receive their first alert
352 */
353 public static List<Integer> getIdUsersToSendFirstAlert( Timestamp firstAlertMaxDate, Plugin plugin )
354 {
355 return _dao.getIdUsersToSendFirstAlert( firstAlertMaxDate, plugin );
356 }
357
358 /**
359 * Get the list of id of users that need to receive their first alert
360 *
361 * @param alertMaxDate
362 * The maximum date to send alerts.
363 * @param timeBetweenAlerts
364 * Timestamp describing the time between two alerts.
365 * @param maxNumberAlerts
366 * Maximum number of alerts to send to a user
367 * @param plugin
368 * The plugin
369 * @return the list of id of users that need to receive their first alert
370 */
371 public static List<Integer> getIdUsersToSendOtherAlert( Timestamp alertMaxDate, Timestamp timeBetweenAlerts, int maxNumberAlerts, Plugin plugin )
372 {
373 return _dao.getIdUsersToSendOtherAlert( alertMaxDate, timeBetweenAlerts, maxNumberAlerts, plugin );
374 }
375
376 /**
377 * Get the list of id of users that have an expired password but not the change password flag
378 *
379 * @param currentTimestamp
380 * Timestamp describing the current time.
381 * @param plugin
382 * The plugin
383 * @return the list of id of users with expired passwords
384 */
385 public static List<Integer> getIdUsersWithExpiredPasswordsList( Timestamp currentTimestamp, Plugin plugin )
386 {
387 return _dao.getIdUsersWithExpiredPasswordsList( currentTimestamp, plugin );
388 }
389
390 /**
391 * Update status of a list of user accounts
392 *
393 * @param listIdUser
394 * List of user accounts to update
395 * @param nNewStatus
396 * New status of the user
397 * @param plugin
398 * The plugin
399 */
400 public static void updateUserStatus( List<Integer> listIdUser, int nNewStatus, Plugin plugin )
401 {
402 _dao.updateUserStatus( listIdUser, nNewStatus, plugin );
403 }
404
405 /**
406 * Increment the number of alert send to users by 1
407 *
408 * @param listIdUser
409 * The list of users to update
410 * @param plugin
411 * The plugin
412 */
413 public static void updateNbAlert( List<Integer> listIdUser, Plugin plugin )
414 {
415 _dao.updateNbAlert( listIdUser, plugin );
416 }
417
418 /**
419 * Set the "change password" flag of users to true
420 *
421 * @param listIdUser
422 * The list of users to update
423 * @param plugin
424 * The plugin
425 */
426 public static void updateChangePassword( List<Integer> listIdUser, Plugin plugin )
427 {
428 _dao.updateChangePassword( listIdUser, plugin );
429 }
430
431 /**
432 * Update the user expiration date with the new values. Also update his alert account to 0
433 *
434 * @param nIdUser
435 * Id of the user to update
436 * @param newExpirationDate
437 * Id of the user to update
438 * @param plugin
439 * The plugin
440 */
441 public static void updateUserExpirationDate( int nIdUser, Timestamp newExpirationDate, Plugin plugin )
442 {
443 _dao.updateUserExpirationDate( nIdUser, newExpirationDate, plugin );
444 }
445
446 /**
447 * Get the number of notification send to a user to warn him about the expiration of his account
448 *
449 * @param nIdUser
450 * Id of the user
451 * @param plugin
452 * The plugin
453 * @return The number of notification send to the user
454 */
455 public static int getNbAccountLifeTimeNotification( int nIdUser, Plugin plugin )
456 {
457 return _dao.getNbAccountLifeTimeNotification( nIdUser, plugin );
458 }
459
460 /**
461 * Update a user last login date
462 *
463 * @param strLogin
464 * Login of the user to update
465 * @param dateLastLogin
466 * date of the last login of the user
467 * @param plugin
468 * The plugin
469 */
470 public static void updateUserLastLoginDate( String strLogin, Date dateLastLogin, Plugin plugin )
471 {
472 _dao.updateUserLastLoginDate( strLogin, new java.sql.Timestamp( dateLastLogin.getTime( ) ), plugin );
473 }
474 }