AdminUserHome.java

  1. /*
  2.  * Copyright (c) 2002-2022, 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.portal.business.user;

  35. import fr.paris.lutece.portal.business.rbac.RBACRole;
  36. import fr.paris.lutece.portal.business.right.Right;
  37. import fr.paris.lutece.portal.business.user.authentication.LuteceDefaultAdminUser;
  38. import fr.paris.lutece.portal.service.spring.SpringContextService;
  39. import fr.paris.lutece.portal.service.util.CryptoService;
  40. import fr.paris.lutece.util.password.IPassword;

  41. import java.sql.Timestamp;

  42. import java.util.Collection;
  43. import java.util.Date;
  44. import java.util.List;
  45. import java.util.Map;

  46. /**
  47.  * This class provides instances management methods (create, find, ...) for AdminUser objects
  48.  */
  49. public final class AdminUserHome
  50. {
  51.     // Static variable pointed at the DAO instance
  52.     private static IAdminUserDAO _dao = SpringContextService.getBean( "adminUserDAO" );

  53.     /**
  54.      * Private constructor
  55.      */
  56.     private AdminUserHome( )
  57.     {
  58.     }

  59.     /**
  60.      * Get the user infos from the access code.
  61.      *
  62.      * @param strUserLogin
  63.      *            the login
  64.      * @return user info
  65.      */
  66.     public static AdminUser findUserByLogin( String strUserLogin )
  67.     {
  68.         return _dao.selectUserByAccessCode( strUserLogin );
  69.     }
  70.    
  71.     /**
  72.      * Get the user infos from the access code.
  73.      *
  74.      * @param strUserLogin
  75.      *            the login
  76.      * @param   user the user to load          
  77.      * @return user info
  78.      */
  79.     public static <T extends AdminUser> T findUserByLogin( String strUserLogin,T user )
  80.     {
  81.         return _dao.selectUserByAccessCode( strUserLogin ,user);
  82.     }
  83.    

  84.     /**
  85.      * Get the user access code from its email.
  86.      *
  87.      * @param strEmail
  88.      *            The email
  89.      * @return The access code of the user with the given email, or null if no user has been found
  90.      */
  91.     public static String findUserByEmail( String strEmail )
  92.     {
  93.         return _dao.selectUserByEmail( strEmail );
  94.     }

  95.     /**
  96.      * Get the user infos from user id
  97.      *
  98.      * @param nUserId
  99.      *            the user identifier
  100.      * @return The user
  101.      */
  102.     public static AdminUser findByPrimaryKey( int nUserId )
  103.     {
  104.         return _dao.load( nUserId );
  105.     }

  106.     /**
  107.      * @return the user list
  108.      */
  109.     public static Collection<AdminUser> findUserList( )
  110.     {
  111.         return _dao.selectUserList( );
  112.     }

  113.     /**
  114.      * @param user
  115.      *            The AdminUser
  116.      */
  117.     public static void create( AdminUser user )
  118.     {
  119.         _dao.insert( user );
  120.     }

  121.     /**
  122.      * @param user
  123.      *            The AdminUser
  124.      */
  125.     public static void update( AdminUser user )
  126.     {
  127.         _dao.store( user );
  128.     }

  129.     /**
  130.      * @param nUserId
  131.      *            the user identifier
  132.      */
  133.     public static void remove( int nUserId )
  134.     {
  135.         _dao.delete( nUserId );
  136.     }

  137.     /**
  138.      * Get the right list associated to a given user id
  139.      *
  140.      * @param nUserId
  141.      *            the id of the user to retrieve rights
  142.      * @return the right list
  143.      */
  144.     public static Map<String, Right> getRightsListForUser( int nUserId )
  145.     {
  146.         return _dao.selectRightsListForUser( nUserId );
  147.     }

  148.     /**
  149.      * @param nUserId
  150.      *            The user identifier
  151.      * @param strRightId
  152.      *            The right identifier
  153.      */
  154.     public static void createRightForUser( int nUserId, String strRightId )
  155.     {
  156.         _dao.insertRightsListForUser( nUserId, strRightId );
  157.     }

  158.     /**
  159.      * @param nUserId
  160.      *            The user identifier
  161.      */
  162.     public static void removeAllRightsForUser( int nUserId )
  163.     {
  164.         _dao.deleteAllRightsForUser( nUserId );
  165.     }

  166.     /**
  167.      * @param user
  168.      *            The Admin User object
  169.      */
  170.     public static void removeAllDelegatedRightsForUser( AdminUser user )
  171.     {
  172.         _dao.deleteAllDelegatedRightsForUser( user.getUserId( ), user.getUserLevel( ) );
  173.     }

  174.     /**
  175.      * @param user
  176.      *            The Admin User object
  177.      */
  178.     public static void removeAllOwnRightsForUser( AdminUser user )
  179.     {
  180.         _dao.deleteAllOwnRightsForUser( user.getUserId( ), user.getUserLevel( ) );
  181.     }

  182.     /**
  183.      * Get the role list associated to a given user id
  184.      *
  185.      * @param nUserId
  186.      *            the id of the user to retrieve rights
  187.      * @return the role list
  188.      */
  189.     public static Map<String, RBACRole> getRolesListForUser( int nUserId )
  190.     {
  191.         return _dao.selectRolesListForUser( nUserId );
  192.     }

  193.     /**
  194.      * @param nUserId
  195.      *            the id of the user
  196.      * @param strRightId
  197.      *            the right identifier
  198.      */
  199.     public static void createRoleForUser( int nUserId, String strRightId )
  200.     {
  201.         _dao.insertRolesListForUser( nUserId, strRightId );
  202.     }

  203.     /**
  204.      * @param nUserId
  205.      *            the user identifier
  206.      */
  207.     public static void removeAllRolesForUser( int nUserId )
  208.     {
  209.         _dao.deleteAllRolesForUser( nUserId );
  210.     }

  211.     /**
  212.      * Checks wether the role is in use or not
  213.      *
  214.      * @param strRoleKey
  215.      *            the role key to check
  216.      * @return true if the role is attributed, false otherwise
  217.      */
  218.     public static boolean checkRoleAttributed( String strRoleKey )
  219.     {
  220.         return _dao.checkRoleAttributed( strRoleKey );
  221.     }

  222.     /**
  223.      * Checks if a given login is already in use
  224.      *
  225.      * @param strAccessCode
  226.      *            The login
  227.      * @return user ID if the access code is already used by another user, -1 otherwise
  228.      */
  229.     public static int checkAccessCodeAlreadyInUse( String strAccessCode )
  230.     {
  231.         return _dao.checkAccessCodeAlreadyInUse( strAccessCode );
  232.     }

  233.     /**
  234.      * Checks if a given email is already in use
  235.      *
  236.      * @param strEmail
  237.      *            The email
  238.      * @return user ID if the email is already used by another user, -1 otherwise
  239.      */
  240.     public static int checkEmailAlreadyInUse( String strEmail )
  241.     {
  242.         return _dao.checkEmailAlreadyInUse( strEmail );
  243.     }

  244.     /**
  245.      * Check if the user has the role
  246.      *
  247.      * @param user
  248.      *            The AdminUser
  249.      * @param strRoleKey
  250.      *            The role Key
  251.      * @return true if the user has the role
  252.      */
  253.     public static boolean hasRole( AdminUser user, String strRoleKey )
  254.     {
  255.         return _dao.hasRole( user.getUserId( ), strRoleKey );
  256.     }

  257.     /**
  258.      * Remove role for an user
  259.      *
  260.      * @param nUserId
  261.      *            The ID of the user
  262.      * @param strRoleKey
  263.      *            The role key
  264.      */
  265.     public static void removeRoleForUser( int nUserId, String strRoleKey )
  266.     {
  267.         _dao.deleteRoleForUser( nUserId, strRoleKey );
  268.     }

  269.     // ////////////////////////////////////////////////////////////////
  270.     // / for no-module mode

  271.     /**
  272.      * @param user
  273.      *            the LuteceDefaultAdminUSer
  274.      */
  275.     public static void create( LuteceDefaultAdminUser user )
  276.     {
  277.         _dao.insert( user );
  278.     }

  279.     /**
  280.      * @param user
  281.      *            the LuteceDefaultAdminUSer
  282.      */
  283.     public static void update( LuteceDefaultAdminUser user )
  284.     {
  285.         update( user, PasswordUpdateMode.UPDATE );
  286.     }

  287.     /**
  288.      * @param user
  289.      *            the LuteceDefaultAdminUSer
  290.      * @param passwordMode
  291.      *            should the password be updated or not
  292.      */
  293.     public static void update( LuteceDefaultAdminUser user, PasswordUpdateMode passwordMode )
  294.     {
  295.         _dao.store( user, passwordMode );
  296.     }

  297.     /**
  298.      * Get the user infos from user id
  299.      *
  300.      * @param nUserId
  301.      *            the user identifier
  302.      * @return the delfault admin user
  303.      */
  304.     public static LuteceDefaultAdminUser findLuteceDefaultAdminUserByPrimaryKey( int nUserId )
  305.     {
  306.         return _dao.loadDefaultAdminUser( nUserId );
  307.     }

  308.     /**
  309.      * Get all users having a given role
  310.      *
  311.      * @param strRoleKey
  312.      *            The role key
  313.      * @return A collection of AdminUser
  314.      */
  315.     public static Collection<AdminUser> findByRole( String strRoleKey )
  316.     {
  317.         return _dao.selectUsersByRole( strRoleKey );
  318.     }

  319.     /**
  320.      * Get all users having a given level
  321.      *
  322.      * @param nIdLevel
  323.      *            The level
  324.      * @return A collection of AdminUser
  325.      */
  326.     public static Collection<AdminUser> findByLevel( int nIdLevel )
  327.     {
  328.         return _dao.selectUsersByLevel( nIdLevel );
  329.     }

  330.     /**
  331.      * Update role key if role key name has change
  332.      *
  333.      * @param strOldRoleKey
  334.      *            The old role key name
  335.      * @param role
  336.      *            The new role
  337.      */
  338.     public static void updateUsersRole( String strOldRoleKey, RBACRole role )
  339.     {
  340.         _dao.storeUsersRole( strOldRoleKey, role );
  341.     }

  342.     /**
  343.      * Get all users by using a filter.
  344.      *
  345.      * @param auFilter
  346.      *            The filter
  347.      * @return A collection of AdminUser
  348.      */
  349.     public static Collection<AdminUser> findUserByFilter( AdminUserFilter auFilter )
  350.     {
  351.         return _dao.selectUsersByFilter( auFilter );
  352.     }

  353.     /**
  354.      * Get all users having a given right
  355.      *
  356.      * @param strIdRight
  357.      *            The ID right
  358.      * @return A collection of AdminUser
  359.      */
  360.     public static Collection<AdminUser> findByRight( String strIdRight )
  361.     {
  362.         return _dao.selectUsersByRight( strIdRight );
  363.     }

  364.     /**
  365.      * Check if the user has the given right
  366.      *
  367.      * @param user
  368.      *            The Admin User
  369.      * @param strIdRight
  370.      *            The ID right
  371.      * @return true if the user has the right
  372.      */
  373.     public static boolean hasRight( AdminUser user, String strIdRight )
  374.     {
  375.         return _dao.hasRight( user.getUserId( ), strIdRight );
  376.     }

  377.     /**
  378.      * Remove a right for an user
  379.      *
  380.      * @param nUserId
  381.      *            The user ID
  382.      * @param strIdRight
  383.      *            The right ID
  384.      */
  385.     public static void removeRightForUser( int nUserId, String strIdRight )
  386.     {
  387.         _dao.deleteRightForUser( nUserId, strIdRight );
  388.     }

  389.     /**
  390.      * Gets the history of password of the given user
  391.      *
  392.      * @param nUserID
  393.      *            Id of the user
  394.      * @return The collection of recent passwords used by the user.
  395.      */
  396.     public static List<IPassword> selectUserPasswordHistory( int nUserID )
  397.     {
  398.         return _dao.selectUserPasswordHistory( nUserID );
  399.     }

  400.     /**
  401.      * Get the number of password change done by a user since the given date.
  402.      *
  403.      * @param minDate
  404.      *            Minimum date to consider.
  405.      * @param nUserId
  406.      *            Id of the user
  407.      * @return The number of password change done by the user since the given date.
  408.      */
  409.     public static int countUserPasswordHistoryFromDate( Timestamp minDate, int nUserId )
  410.     {
  411.         return _dao.countUserPasswordHistoryFromDate( minDate, nUserId );
  412.     }

  413.     /**
  414.      * Log a password change in the password history
  415.      *
  416.      * @param password
  417.      *            New password of the user
  418.      * @param nUserId
  419.      *            Id of the user
  420.      */
  421.     public static void insertNewPasswordInHistory( IPassword password, int nUserId )
  422.     {
  423.         _dao.insertNewPasswordInHistory( password, nUserId );
  424.     }

  425.     /**
  426.      * Remove every password saved in the password history for a user.
  427.      *
  428.      * @param nUserId
  429.      *            Id of the user
  430.      */
  431.     public static void removeAllPasswordHistoryForUser( int nUserId )
  432.     {
  433.         _dao.removeAllPasswordHistoryForUser( nUserId );
  434.     }

  435.     /**
  436.      * Get a map of anonymization status of a user field.
  437.      *
  438.      * @return A map containing the associations of user field name and a boolean describing whether the field should be anonymized.
  439.      */
  440.     public static Map<String, Boolean> getAnonymizationStatusUserStaticField( )
  441.     {
  442.         return _dao.selectAnonymizationStatusUserStaticField( );
  443.     }

  444.     /**
  445.      * Update the anonymization status of a user field.
  446.      *
  447.      * @param strFieldName
  448.      *            Name of the field to update
  449.      * @param bAnonymizeFiled
  450.      *            True if the field should be anonymize, false otherwise
  451.      */
  452.     public static void updateAnonymizationStatusUserStaticField( String strFieldName, boolean bAnonymizeFiled )
  453.     {
  454.         _dao.updateAnonymizationStatusUserStaticField( strFieldName, bAnonymizeFiled );
  455.     }

  456.     /**
  457.      * Get the list of id of user with the expired status.
  458.      *
  459.      * @return The list of if of user with the expired status.
  460.      */
  461.     public static List<Integer> findAllExpiredUserId( )
  462.     {
  463.         return _dao.findAllExpiredUserId( );
  464.     }

  465.     /**
  466.      * Get the list of id of users that have an expired time life but not the expired status
  467.      *
  468.      * @param currentTimestamp
  469.      *            Timestamp describing the current time.
  470.      * @return the list of id of users with expired time life
  471.      */
  472.     public static List<Integer> getIdUsersWithExpiredLifeTimeList( Timestamp currentTimestamp )
  473.     {
  474.         return _dao.getIdUsersWithExpiredLifeTimeList( currentTimestamp );
  475.     }

  476.     /**
  477.      * Get the list of id of users that need to receive their first alert
  478.      *
  479.      * @param firstAlertMaxDate
  480.      *            The maximum expiration date to send first alert.
  481.      * @return the list of id of users that need to receive their first alert
  482.      */
  483.     public static List<Integer> getIdUsersToSendFirstAlert( Timestamp firstAlertMaxDate )
  484.     {
  485.         return _dao.getIdUsersToSendFirstAlert( firstAlertMaxDate );
  486.     }

  487.     /**
  488.      * Get the list of id of users that need to receive their first alert
  489.      *
  490.      * @param alertMaxDate
  491.      *            The maximum date to send alerts.
  492.      * @param timeBetweenAlerts
  493.      *            Timestamp describing the time between two alerts.
  494.      * @param maxNumberAlerts
  495.      *            Maximum number of alerts to send to a user
  496.      * @return the list of id of users that need to receive their first alert
  497.      */
  498.     public static List<Integer> getIdUsersToSendOtherAlert( Timestamp alertMaxDate, Timestamp timeBetweenAlerts, int maxNumberAlerts )
  499.     {
  500.         return _dao.getIdUsersToSendOtherAlert( alertMaxDate, timeBetweenAlerts, maxNumberAlerts );
  501.     }

  502.     /**
  503.      * Get the list of id of users that have an expired password but not the change password flag
  504.      *
  505.      * @param currentTimestamp
  506.      *            Timestamp describing the current time.
  507.      * @return the list of id of users with expired passwords
  508.      */
  509.     public static List<Integer> getIdUsersWithExpiredPasswordsList( Timestamp currentTimestamp )
  510.     {
  511.         return _dao.getIdUsersWithExpiredPasswordsList( currentTimestamp );
  512.     }

  513.     /**
  514.      * Update status of a list of user accounts
  515.      *
  516.      * @param listIdUser
  517.      *            List of user accounts to update
  518.      * @param nNewStatus
  519.      *            New status of the user
  520.      */
  521.     public static void updateUserStatus( List<Integer> listIdUser, int nNewStatus )
  522.     {
  523.         _dao.updateUserStatus( listIdUser, nNewStatus );
  524.     }

  525.     /**
  526.      * Increment the number of alert send to users by 1
  527.      *
  528.      * @param listIdUser
  529.      *            The list of users to update
  530.      */
  531.     public static void updateNbAlert( List<Integer> listIdUser )
  532.     {
  533.         _dao.updateNbAlert( listIdUser );
  534.     }

  535.     /**
  536.      * Set the "change password" flag of users to true
  537.      *
  538.      * @param listIdUser
  539.      *            The list of users to update
  540.      */
  541.     public static void updateChangePassword( List<Integer> listIdUser )
  542.     {
  543.         _dao.updateChangePassword( listIdUser );
  544.     }

  545.     /**
  546.      * Update the admin user expiration date with the new values. Also update his alert account to 0
  547.      *
  548.      * @param nIdUser
  549.      *            Id of the admin user to update
  550.      * @param newExpirationDate
  551.      *            Id of the user to update
  552.      */
  553.     public static void updateUserExpirationDate( int nIdUser, Timestamp newExpirationDate )
  554.     {
  555.         _dao.updateUserExpirationDate( nIdUser, newExpirationDate );
  556.     }

  557.     /**
  558.      * Update the admin user last login date.
  559.      *
  560.      * @param nIdUser
  561.      *            Id of the admin user to update
  562.      * @param dateLastLogin
  563.      *            New last login date of the user
  564.      */
  565.     public static void updateDateLastLogin( int nIdUser, Timestamp dateLastLogin )
  566.     {
  567.         _dao.updateDateLastLogin( nIdUser, dateLastLogin );
  568.     }

  569.     /**
  570.      * Construct a password reset token. Use the numerical userId as it does not change. Use the stored password, so that the token is invalidated if the
  571.      * password is changed. Use a timestamp to allow limiting the validity of the token in time. Optionally bind the token to the user session. Finally return
  572.      * an HMAC of this info using the application crypto key.
  573.      *
  574.      * @param nIdUser
  575.      *            the user ID
  576.      * @param timestamp
  577.      *            the timestamp
  578.      * @param strSessionId
  579.      *            the session ID
  580.      * @return the password reset token
  581.      */
  582.     public static String getUserPasswordResetToken( int nIdUser, Date timestamp, String strSessionId )
  583.     {
  584.         LuteceDefaultAdminUser user = _dao.loadDefaultAdminUser( nIdUser );
  585.         StringBuilder builder = new StringBuilder( );
  586.         builder.append( "userId:" ).append( nIdUser );
  587.         IPassword password = user.getPassword( );
  588.         if ( password != null )
  589.         {
  590.             builder.append( ":password:" );
  591.             if ( password.isLegacy( ) )
  592.             {
  593.                 builder.append( "legacy" );
  594.             }
  595.             else
  596.             {
  597.                 builder.append( password.getStorableRepresentation( ) );
  598.             }
  599.         }
  600.         builder.append( ":timestamp:" ).append( timestamp.getTime( ) );
  601.         if ( strSessionId != null )
  602.         {
  603.             builder.append( ":sessionId:" ).append( strSessionId );
  604.         }
  605.         return CryptoService.hmacSHA256( builder.toString( ) );
  606.     }
  607. }