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.plugins.mylutece.modules.database.authentication.BaseUser;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.security.LuteceAuthentication;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  
41  import java.sql.Timestamp;
42  
43  import java.util.Collection;
44  import java.util.List;
45  
46  /**
47   * This class provides instances management methods (create, find, ...) for databaseUser objects
48   */
49  public final class DatabaseHome
50  {
51      // Static variable pointed at the DAO instance
52      private static IDatabaseDAO _dao = SpringContextService.getBean( "mylutece-database.databaseDAO" );
53  
54      /**
55       * Private constructor - this class need not be instantiated
56       */
57      private DatabaseHome( )
58      {
59      }
60  
61      /**
62       * Find users by login
63       *
64       * @param strLogin
65       *            the login
66       * @param plugin
67       *            The Plugin using this data access service
68       * @param authenticationService
69       *            the LuteceAuthentication object
70       * @return DatabaseUser the user corresponding to the login
71       */
72      public static BaseUser findLuteceUserByLogin( String strLogin, Plugin plugin, LuteceAuthentication authenticationService )
73      {
74          return _dao.selectLuteceUserByLogin( strLogin, plugin, authenticationService );
75      }
76  
77      /**
78       * Gets the reset password attribute of the user from his login
79       *
80       * @param strLogin
81       *            the login
82       * @param plugin
83       *            The Plugin using this data access service
84       * @return True if the password has to be changed, false otherwise
85       */
86      public static boolean findResetPasswordFromLogin( String strLogin, Plugin plugin )
87      {
88          return _dao.selectResetPasswordFromLogin( strLogin, plugin );
89      }
90  
91      /**
92       * Gets the expiration date of the user's password
93       * 
94       * @param strLogin
95       *            The login of the user
96       * @param plugin
97       *            The plugin
98       * @return The expiration date of the user's password
99       */
100     public static Timestamp findPasswordMaxValideDateFromLogin( String strLogin, Plugin plugin )
101     {
102         return _dao.selectPasswordMaxValideDateFromLogin( strLogin, plugin );
103     }
104 
105     /**
106      * Load the list of {@link BaseUser}
107      * 
108      * @param plugin
109      *            The Plugin using this data access service
110      * @param authenticationService
111      *            the authentication service
112      * @return The Collection of the {@link BaseUser}
113      */
114     public static Collection<BaseUser> findDatabaseUsersList( Plugin plugin, LuteceAuthentication authenticationService )
115     {
116         return _dao.selectLuteceUserList( plugin, authenticationService );
117     }
118 
119     /**
120      * Find user's roles by login
121      *
122      * @param strLogin
123      *            the login
124      * @param plugin
125      *            The Plugin using this data access service
126      * @return ArrayList the role key list corresponding to the login
127      */
128     public static List<String> findUserRolesFromLogin( String strLogin, Plugin plugin )
129     {
130         return _dao.selectUserRolesFromLogin( strLogin, plugin );
131     }
132 
133     /**
134      * Delete roles for a user
135      * 
136      * @param nIdUser
137      *            The id of the user
138      * @param plugin
139      *            The Plugin using this data access service
140      */
141     public static void removeRolesForUser( int nIdUser, Plugin plugin )
142     {
143         _dao.deleteRolesForUser( nIdUser, plugin );
144     }
145 
146     /**
147      * Assign a role to user
148      * 
149      * @param nIdUser
150      *            The id of the user
151      * @param strRoleKey
152      *            The key of the role
153      * @param plugin
154      *            The Plugin using this data access service
155      */
156     public static void addRoleForUser( int nIdUser, String strRoleKey, Plugin plugin )
157     {
158         _dao.createRoleForUser( nIdUser, strRoleKey, plugin );
159     }
160 
161     /**
162      * Find user's groups by login
163      *
164      * @param strLogin
165      *            the login
166      * @param plugin
167      *            The Plugin using this data access service
168      * @return ArrayList the group key list corresponding to the login
169      */
170     public static List<String> findUserGroupsFromLogin( String strLogin, Plugin plugin )
171     {
172         return _dao.selectUserGroupsFromLogin( strLogin, plugin );
173     }
174 
175     /**
176      * Delete groups for a user
177      * 
178      * @param nIdUser
179      *            The id of the user
180      * @param plugin
181      *            The Plugin using this data access service
182      */
183     public static void removeGroupsForUser( int nIdUser, Plugin plugin )
184     {
185         _dao.deleteGroupsForUser( nIdUser, plugin );
186     }
187 
188     /**
189      * Assign a group to user
190      * 
191      * @param nIdUser
192      *            The id of the user
193      * @param strGroupKey
194      *            The key of the group
195      * @param plugin
196      *            The Plugin using this data access service
197      */
198     public static void addGroupForUser( int nIdUser, String strGroupKey, Plugin plugin )
199     {
200         _dao.createGroupForUser( nIdUser, strGroupKey, plugin );
201     }
202 
203     /**
204      * Returns a collection of DatabaseUser objects for a Lutece role
205      *
206      * @param strRoleKey
207      *            The role of the databseUser
208      * @param plugin
209      *            The current plugin using this method
210      * @return A collection of logins
211      */
212     public static Collection<String> findDatabaseUsersListForRoleKey( String strRoleKey, Plugin plugin )
213     {
214         return _dao.selectLoginListForRoleKey( strRoleKey, plugin );
215     }
216 
217     /**
218      * Find assigned users to the given group
219      * 
220      * @param strGroupKey
221      *            The group key
222      * @param plugin
223      *            Plugin
224      * @return a list of DatabaseUser
225      */
226     public static List<DatabaseUser> findGroupUsersFromGroupKey( String strGroupKey, Plugin plugin )
227     {
228         return _dao.selectGroupUsersFromGroupKey( strGroupKey, plugin );
229     }
230 
231     /**
232      * Update the reset password attribut of a user from his login
233      * 
234      * @param strUserName
235      *            Login of the user to update
236      * @param bNewValue
237      *            New value
238      * @param plugin
239      *            the plugin
240      */
241     public static void updateResetPasswordFromLogin( String strUserName, boolean bNewValue, Plugin plugin )
242     {
243         _dao.updateResetPasswordFromLogin( strUserName, bNewValue, plugin );
244     }
245 
246     /**
247      * Get the id of a user from his login
248      * 
249      * @param strLogin
250      *            Login of the user
251      * @param plugin
252      *            The plugin
253      * @return The id of the user
254      */
255     public static int findUserIdFromLogin( String strLogin, Plugin plugin )
256     {
257         return _dao.findUserIdFromLogin( strLogin, plugin );
258     }
259 }