View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.portal.business.user;
35  
36  import fr.paris.lutece.portal.business.rbac.AdminRole;
37  import fr.paris.lutece.portal.business.right.Right;
38  import fr.paris.lutece.portal.business.user.authentication.LuteceDefaultAdminUser;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  import fr.paris.lutece.util.password.IPassword;
41  
42  import java.sql.Timestamp;
43  
44  import java.util.Collection;
45  import java.util.List;
46  import java.util.Map;
47  
48  
49  /**
50   * This class provides instances management methods (create, find, ...) for AdminUser objects
51   */
52  public final class AdminUserHome
53  {
54      // Static variable pointed at the DAO instance
55      private static IAdminUserDAO _dao = (IAdminUserDAO) SpringContextService.getBean( "adminUserDAO" );
56  
57      /**
58       * Private constructor
59       */
60      private AdminUserHome(  )
61      {
62      }
63  
64      /**
65       * Get the user infos from the access code.
66       * @param strUserLogin the login
67       * @return user info
68       */
69      public static AdminUser findUserByLogin( String strUserLogin )
70      {
71          return _dao.selectUserByAccessCode( strUserLogin );
72      }
73  
74      /**
75       * Get the user access code from its email.
76       * @param strEmail The email
77       * @return The access code of the user with the given email, or null if no user has been found
78       */
79      public static String findUserByEmail( String strEmail )
80      {
81          return _dao.selectUserByEmail( strEmail );
82      }
83  
84      /**
85       * Get the user infos from user id
86       * @param nUserId the user identifier
87       * @return The user
88       */
89      public static AdminUser findByPrimaryKey( int nUserId )
90      {
91          return _dao.load( nUserId );
92      }
93  
94      /**
95       * @return the user list
96       */
97      public static Collection<AdminUser> findUserList(  )
98      {
99          return _dao.selectUserList(  );
100     }
101 
102     /**
103      * @param user The AdminUser
104      */
105     public static void create( AdminUser user )
106     {
107         _dao.insert( user );
108     }
109 
110     /**
111      * @param user The AdminUser
112      */
113     public static void update( AdminUser user )
114     {
115         _dao.store( user );
116     }
117 
118     /**
119      * @param nUserId the user identifier
120      */
121     public static void remove( int nUserId )
122     {
123         _dao.delete( nUserId );
124     }
125 
126     /**
127      * Get the right list associated to a given user id
128      * @param nUserId the id of the user to retrieve rights
129      * @return the right list
130      */
131     public static Map<String, Right> getRightsListForUser( int nUserId )
132     {
133         return _dao.selectRightsListForUser( nUserId );
134     }
135 
136     /**
137      * @param nUserId The user identifier
138      * @param strRightId The right identifier
139      */
140     public static void createRightForUser( int nUserId, String strRightId )
141     {
142         _dao.insertRightsListForUser( nUserId, strRightId );
143     }
144 
145     /**
146      * @param nUserId The user identifier
147      */
148     public static void removeAllRightsForUser( int nUserId )
149     {
150         _dao.deleteAllRightsForUser( nUserId );
151     }
152 
153     /**
154      * @param user The Admin User object
155      */
156     public static void removeAllDelegatedRightsForUser( AdminUser user )
157     {
158         _dao.deleteAllDelegatedRightsForUser( user.getUserId(  ), user.getUserLevel(  ) );
159     }
160 
161     /**
162      * @param user The Admin User object
163      */
164     public static void removeAllOwnRightsForUser( AdminUser user )
165     {
166         _dao.deleteAllOwnRightsForUser( user.getUserId(  ), user.getUserLevel(  ) );
167     }
168 
169     /**
170      * Get the role list associated to a given user id
171      * @param nUserId the id of the user to retrieve rights
172      * @return the role list
173      */
174     public static Map<String, AdminRole> getRolesListForUser( int nUserId )
175     {
176         return _dao.selectRolesListForUser( nUserId );
177     }
178 
179     /**
180      * @param nUserId the id of the user
181      * @param strRightId the right identifier
182      */
183     public static void createRoleForUser( int nUserId, String strRightId )
184     {
185         _dao.insertRolesListForUser( nUserId, strRightId );
186     }
187 
188     /**
189      * @param nUserId the user identifier
190      */
191     public static void removeAllRolesForUser( int nUserId )
192     {
193         _dao.deleteAllRolesForUser( nUserId );
194     }
195 
196     /**
197      * Checks wether the role is in use or not
198      * @param strRoleKey the role key to check
199      * @return true if the role is attributed, false otherwise
200      */
201     public static boolean checkRoleAttributed( String strRoleKey )
202     {
203         return _dao.checkRoleAttributed( strRoleKey );
204     }
205 
206     /**
207      * Checks if a given login is already in use
208      * @param strAccessCode The login
209      * @return user ID if the access code is already used by another user, -1 otherwise
210      */
211     public static int checkAccessCodeAlreadyInUse( String strAccessCode )
212     {
213         return _dao.checkAccessCodeAlreadyInUse( strAccessCode );
214     }
215 
216     /**
217      * Checks if a given email is already in use
218      * @param strEmail The email
219      * @return user ID if the email is already used by another user, -1 otherwise
220      */
221     public static int checkEmailAlreadyInUse( String strEmail )
222     {
223         return _dao.checkEmailAlreadyInUse( strEmail );
224     }
225 
226     /**
227      * Check if the user has the role
228      * @param user The AdminUser
229      * @param strRoleKey The role Key
230      * @return true if the user has the role
231      */
232     public static boolean hasRole( AdminUser user, String strRoleKey )
233     {
234         return _dao.hasRole( user.getUserId(  ), strRoleKey );
235     }
236 
237     /**
238      * Remove role for an user
239      * @param nUserId The ID of the user
240      * @param strRoleKey The role key
241      */
242     public static void removeRoleForUser( int nUserId, String strRoleKey )
243     {
244         _dao.deleteRoleForUser( nUserId, strRoleKey );
245     }
246 
247     // ////////////////////////////////////////////////////////////////
248     // / for no-module mode
249 
250     /**
251      * @param user the LuteceDefaultAdminUSer
252      */
253     public static void create( LuteceDefaultAdminUser user )
254     {
255         _dao.insert( user );
256     }
257 
258     /**
259      * @param user the LuteceDefaultAdminUSer
260      */
261     public static void update( LuteceDefaultAdminUser user )
262     {
263         _dao.store( user );
264     }
265 
266     /**
267      * Get the user infos from user id
268      * @param nUserId the user identifier
269      * @return the delfault admin user
270      */
271     public static LuteceDefaultAdminUser findLuteceDefaultAdminUserByPrimaryKey( int nUserId )
272     {
273         return _dao.loadDefaultAdminUser( nUserId );
274     }
275 
276     /**
277      * Get all users having a given role
278      * @param strRoleKey The role key
279      * @return A collection of AdminUser
280      */
281     public static Collection<AdminUser> findByRole( String strRoleKey )
282     {
283         return _dao.selectUsersByRole( strRoleKey );
284     }
285 
286     /**
287      * Get all users having a given level
288      * @param nIdLevel The level
289      * @return A collection of AdminUser
290      */
291     public static Collection<AdminUser> findByLevel( int nIdLevel )
292     {
293         return _dao.selectUsersByLevel( nIdLevel );
294     }
295 
296     /**
297      * Update role key if role key name has change
298      * @param strOldRoleKey The old role key name
299      * @param role The new role
300      */
301     public static void updateUsersRole( String strOldRoleKey, AdminRole role )
302     {
303         _dao.storeUsersRole( strOldRoleKey, role );
304     }
305 
306     /**
307      * Get all users by using a filter.
308      * @param auFilter The filter
309      * @return A collection of AdminUser
310      */
311     public static Collection<AdminUser> findUserByFilter( AdminUserFilter auFilter )
312     {
313         return _dao.selectUsersByFilter( auFilter );
314     }
315 
316     /**
317      * Get all users having a given right
318      * @param strIdRight The ID right
319      * @return A collection of AdminUser
320      */
321     public static Collection<AdminUser> findByRight( String strIdRight )
322     {
323         return _dao.selectUsersByRight( strIdRight );
324     }
325 
326     /**
327      * Check if the user has the given right
328      * @param user The Admin User
329      * @param strIdRight The ID right
330      * @return true if the user has the right
331      */
332     public static boolean hasRight( AdminUser user, String strIdRight )
333     {
334         return _dao.hasRight( user.getUserId(  ), strIdRight );
335     }
336 
337     /**
338      * Remove a right for an user
339      * @param nUserId The user ID
340      * @param strIdRight The right ID
341      */
342     public static void removeRightForUser( int nUserId, String strIdRight )
343     {
344         _dao.deleteRightForUser( nUserId, strIdRight );
345     }
346 
347     /**
348      * Gets the history of password of the given user
349      * @param nUserID Id of the user
350      * @return The collection of recent passwords used by the user.
351      */
352     public static List<IPassword> selectUserPasswordHistory( int nUserID )
353     {
354         return _dao.selectUserPasswordHistory( nUserID );
355     }
356 
357     /**
358      * Get the number of password change done by a user since the given date.
359      * @param minDate Minimum date to consider.
360      * @param nUserId Id of the user
361      * @return The number of password change done by the user since the given date.
362      */
363     public static int countUserPasswordHistoryFromDate( Timestamp minDate, int nUserId )
364     {
365         return _dao.countUserPasswordHistoryFromDate( minDate, nUserId );
366     }
367 
368     /**
369      * Log a password change in the password history
370      * @param password New password of the user
371      * @param nUserId Id of the user
372      */
373     public static void insertNewPasswordInHistory( IPassword password, int nUserId )
374     {
375         _dao.insertNewPasswordInHistory( password, nUserId );
376     }
377 
378     /**
379      * Remove every password saved in the password history for a user.
380      * @param nUserId Id of the user
381      */
382     public static void removeAllPasswordHistoryForUser( int nUserId )
383     {
384         _dao.removeAllPasswordHistoryForUser( nUserId );
385     }
386 
387     /**
388      * Get a map of anonymization status of a user field.
389      * @return A map containing the associations of user field name and a boolean describing whether the field should be anonymized.
390      */
391     public static Map<String, Boolean> getAnonymizationStatusUserStaticField(  )
392     {
393         return _dao.selectAnonymizationStatusUserStaticField(  );
394     }
395 
396     /**
397      * Update the anonymization status of a user field.
398      * @param strFieldName Name of the field to update
399      * @param bAnonymizeFiled True if the field should be anonymize, false otherwise
400      */
401     public static void updateAnonymizationStatusUserStaticField( String strFieldName, boolean bAnonymizeFiled )
402     {
403         _dao.updateAnonymizationStatusUserStaticField( strFieldName, bAnonymizeFiled );
404     }
405 
406     /**
407      * Get the list of id of user with the expired status.
408      * @return The list of if of user with the expired status.
409      */
410     public static List<Integer> findAllExpiredUserId(  )
411     {
412         return _dao.findAllExpiredUserId(  );
413     }
414 
415     /**
416      * Get the list of id of users that have an expired time life but not the expired status
417      * @param currentTimestamp Timestamp describing the current time.
418      * @return the list of id of users with expired time life
419      */
420     public static List<Integer> getIdUsersWithExpiredLifeTimeList( Timestamp currentTimestamp )
421     {
422         return _dao.getIdUsersWithExpiredLifeTimeList( currentTimestamp );
423     }
424 
425     /**
426      * Get the list of id of users that need to receive their first alert
427      * @param firstAlertMaxDate The maximum expiration date to send first alert.
428      * @return the list of id of users that need to receive their first alert
429      */
430     public static List<Integer> getIdUsersToSendFirstAlert( Timestamp firstAlertMaxDate )
431     {
432         return _dao.getIdUsersToSendFirstAlert( firstAlertMaxDate );
433     }
434 
435     /**
436      * Get the list of id of users that need to receive their first alert
437      * @param alertMaxDate The maximum date to send alerts.
438      * @param timeBetweenAlerts Timestamp describing the time between two alerts.
439      * @param maxNumberAlerts Maximum number of alerts to send to a user
440      * @return the list of id of users that need to receive their first alert
441      */
442     public static List<Integer> getIdUsersToSendOtherAlert( Timestamp alertMaxDate, Timestamp timeBetweenAlerts,
443         int maxNumberAlerts )
444     {
445         return _dao.getIdUsersToSendOtherAlert( alertMaxDate, timeBetweenAlerts, maxNumberAlerts );
446     }
447 
448     /**
449      * Get the list of id of users that have an expired password but not the change password flag
450      * @param currentTimestamp Timestamp describing the current time.
451      * @return the list of id of users with expired passwords
452      */
453     public static List<Integer> getIdUsersWithExpiredPasswordsList( Timestamp currentTimestamp )
454     {
455         return _dao.getIdUsersWithExpiredPasswordsList( currentTimestamp );
456     }
457 
458     /**
459      * Update status of a list of user accounts
460      * @param listIdUser List of user accounts to update
461      * @param nNewStatus New status of the user
462      */
463     public static void updateUserStatus( List<Integer> listIdUser, int nNewStatus )
464     {
465         _dao.updateUserStatus( listIdUser, nNewStatus );
466     }
467 
468     /**
469      * Increment the number of alert send to users by 1
470      * @param listIdUser The list of users to update
471      */
472     public static void updateNbAlert( List<Integer> listIdUser )
473     {
474         _dao.updateNbAlert( listIdUser );
475     }
476 
477     /**
478      * Set the "change password" flag of users to true
479      * @param listIdUser The list of users to update
480      */
481     public static void updateChangePassword( List<Integer> listIdUser )
482     {
483         _dao.updateChangePassword( listIdUser );
484     }
485 
486     /**
487      * Update the admin user expiration date with the new values. Also update his alert account to 0
488      * @param nIdUser Id of the admin user to update
489      * @param newExpirationDate Id of the user to update
490      */
491     public static void updateUserExpirationDate( int nIdUser, Timestamp newExpirationDate )
492     {
493         _dao.updateUserExpirationDate( nIdUser, newExpirationDate );
494     }
495 
496     /**
497      * Update the admin user last login date.
498      * @param nIdUser Id of the admin user to update
499      * @param dateLastLogin New last login date of the user
500      */
501     public static void updateDateLastLogin( int nIdUser, Timestamp dateLastLogin )
502     {
503         _dao.updateDateLastLogin( nIdUser, dateLastLogin );
504     }
505 }