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.plugins.mylutece.modules.wssodatabase.authentication.business;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.List;
39  
40  import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.IdxWSSODatabaseUser;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.security.LuteceAuthentication;
43  import fr.paris.lutece.portal.service.security.LuteceUser;
44  import fr.paris.lutece.util.sql.DAOUtil;
45  
46  
47  /**
48   * This class provides Data Access methods for authentication (role retrieval).
49   * 
50   */
51  public class IdxWSSODatabaseDAO implements IIdxWSSODatabaseDAO
52  {
53      public static final String SQL_QUERY_FIND_USER_BY_GUID = "SELECT mylutece_wsso_user_id, last_name, first_name, email FROM mylutece_wsso_user WHERE guid like ? ";
54  
55      public static final String SQL_QUERY_FIND_ALL_USERS = "SELECT guid, last_name, first_name, email FROM mylutece_wsso_user";
56  
57      public static final String SQL_QUERY_FIND_ROLES_FROM_GUID = "SELECT a.role FROM mylutece_wsso_user_role a, mylutece_wsso_user b"
58              + " WHERE b.mylutece_wsso_user_id = a.mylutece_wsso_user_id AND b.guid like ? ";
59  
60      public static final String SQL_QUERY_FIND_ROLES_BY_PROFIL = "SELECT b.role_key FROM mylutece_wsso_profil a, mylutece_wsso_profil_role b"
61              + " WHERE a.code = b.mylutece_wsso_profil_code AND a.code like ? ";
62  
63      private static final String SQL_QUERY_UPDATE_DATE_LAST_LOGIN = " UPDATE mylutece_wsso_user SET date_last_login = ? WHERE guid like ? ";
64  
65      private static final String SQL_QUERY_DELETE_ROLES_FOR_PROFIL = "DELETE FROM mylutece_wsso_profil_role WHERE mylutece_wsso_profil_code = ? ";
66  
67      private static final String SQL_QUERY_ADD_ROLE_FOR_PROFIL = "INSERT INTO mylutece_wsso_profil_role ( mylutece_wsso_profil_code, role_key ) VALUES ( ?, ? ) ";
68  
69      private static final String SQL_QUERY_FIND_USERS_FOR_PROFIL = "SELECT a.mylutece_wsso_user_id, a.guid, a.last_name, a.first_name, a.email, a.date_last_login FROM mylutece_wsso_user a "
70              + "INNER JOIN mylutece_wsso_profil_user b ON a.mylutece_wsso_user_id = b.mylutece_wsso_user_id WHERE b.mylutece_wsso_profil_code = ?";
71  
72      private static final String SQL_QUERY_ADD_USER_FOR_PROFIL = "INSERT INTO mylutece_wsso_profil_user ( mylutece_wsso_user_id, mylutece_wsso_profil_code ) VALUES ( ?, ? ) ";
73  
74      private static final String SQL_QUERY_DELETE_USER_FOR_PROFIL = "DELETE FROM mylutece_wsso_profil_user WHERE mylutece_wsso_user_id = ? AND mylutece_wsso_profil_code = ?";
75  
76      private static final String SQL_QUERY_DELETE_PROFILS_FOR_USER = "DELETE FROM mylutece_wsso_profil_user WHERE mylutece_wsso_user_id = ? ";
77  
78      /** This class implements the Singleton design pattern. */
79      private static IdxWSSODatabaseDAO _dao = new IdxWSSODatabaseDAO( );
80  
81      /**
82       * Returns the unique instance of the singleton.
83       * 
84       * @return the instance
85       */
86      static IdxWSSODatabaseDAO getInstance( )
87      {
88          return _dao;
89      }
90  
91      /**
92       * Find users by guid
93       * 
94       * @param strGuid the WSSO guid
95       * @param plugin The Plugin using this data access service
96       * @param authenticationService the LuteceAuthentication object
97       * @return IdxWSSODatabaseUser the user corresponding to the guid
98       */
99      public IdxWSSODatabaseUser findUserByGuid( String strGuid, Plugin plugin, LuteceAuthentication authenticationService )
100     {
101         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_USER_BY_GUID, plugin );
102         daoUtil.setString( 1, strGuid );
103         daoUtil.executeQuery( );
104 
105         if ( !daoUtil.next( ) )
106         {
107             daoUtil.free( );
108 
109             return null;
110         }
111 
112         String strLastName = daoUtil.getString( 2 );
113         String strFirstName = daoUtil.getString( 3 );
114         String strEmail = daoUtil.getString( 4 );
115 
116         IdxWSSODatabaseUser user = new IdxWSSODatabaseUser( strGuid, authenticationService );
117         user.setUserInfo( LuteceUser.NAME_FAMILY, strLastName );
118         user.setUserInfo( LuteceUser.NAME_GIVEN, strFirstName );
119         user.setUserInfo( LuteceUser.BUSINESS_INFO_ONLINE_EMAIL, strEmail );
120         daoUtil.free( );
121 
122         return user;
123     }
124 
125     /**
126      * Find user's roles by guid
127      * 
128      * @param strGuid the WSSO guid
129      * @param plugin The Plugin using this data access service
130      * @param authenticationService the LuteceAuthentication object
131      * @return ArrayList the roles list corresponding to the guid
132      */
133     public List<String> findUserRolesFromGuid( String strGuid, Plugin plugin, LuteceAuthentication authenticationService )
134     {
135         List<String> arrayRoles = new ArrayList<String>( );
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ROLES_FROM_GUID, plugin );
137         daoUtil.setString( 1, strGuid );
138         daoUtil.executeQuery( );
139 
140         while ( daoUtil.next( ) )
141         {
142             arrayRoles.add( daoUtil.getString( 1 ) );
143         }
144 
145         daoUtil.free( );
146 
147         return arrayRoles;
148     }
149 
150     /**
151      * 
152      * {@inheritDoc}
153      */
154     @Override
155     public void updateDateLastLogin( String strGuid, java.util.Date dateLastLogin, Plugin plugin )
156     {
157         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_DATE_LAST_LOGIN, plugin );
158         java.sql.Date dateSql = new java.sql.Date( dateLastLogin.getTime( ) );
159         daoUtil.setDate( 1, dateSql );
160         daoUtil.setString( 2, strGuid );
161         daoUtil.executeUpdate( );
162         daoUtil.free( );
163     }
164 
165     /**
166      * Find users list
167      * 
168      * @param plugin The Plugin using this data access service
169      * @param authenticationService the LuteceAuthentication object
170      * @return A Collection of users
171      */
172     public Collection<IdxWSSODatabaseUser> findUsersList( Plugin plugin, LuteceAuthentication authenticationService )
173     {
174         Collection<IdxWSSODatabaseUser> usersList = new ArrayList<IdxWSSODatabaseUser>( );
175         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ALL_USERS, plugin );
176         daoUtil.executeQuery( );
177 
178         while ( daoUtil.next( ) )
179         {
180             String strGuid = daoUtil.getString( 1 );
181             String strLastName = daoUtil.getString( 2 );
182             String strFirstName = daoUtil.getString( 3 );
183             String strEmail = daoUtil.getString( 4 );
184 
185             IdxWSSODatabaseUser user = new IdxWSSODatabaseUser( strGuid, authenticationService );
186 
187             user.setUserInfo( LuteceUser.NAME_FAMILY, strLastName );
188             user.setUserInfo( LuteceUser.NAME_GIVEN, strFirstName );
189             user.setUserInfo( LuteceUser.BUSINESS_INFO_ONLINE_EMAIL, strEmail );
190 
191             user.setRoles( findUserRolesFromGuid( strGuid, plugin, authenticationService ) );
192 
193             usersList.add( user );
194         }
195 
196         daoUtil.free( );
197 
198         return usersList;
199     }
200 
201     /**
202      * 
203      * {@inheritDoc}
204      */
205     @Override
206     public List<String> findRolesFromProfil( String codeProfil, Plugin plugin )
207     {
208         List<String> listRoleKeys = new ArrayList<String>( );
209         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ROLES_BY_PROFIL, plugin );
210         daoUtil.setString( 1, codeProfil );
211         daoUtil.executeQuery( );
212 
213         while ( daoUtil.next( ) )
214         {
215             listRoleKeys.add( daoUtil.getString( 1 ) );
216         }
217 
218         daoUtil.free( );
219 
220         return listRoleKeys;
221     }
222 
223     /**
224      * 
225      * {@inheritDoc}
226      */
227     @Override
228     public void removeRolesForProfil( String codeProfil, Plugin plugin )
229     {
230         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ROLES_FOR_PROFIL, plugin );
231         daoUtil.setString( 1, codeProfil );
232 
233         daoUtil.executeUpdate( );
234         daoUtil.free( );
235     }
236 
237     /**
238      * 
239      * {@inheritDoc}
240      */
241     @Override
242     public void addRoleForProfil( String codeProfil, String codeRole, Plugin plugin )
243     {
244 
245         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_ADD_ROLE_FOR_PROFIL, plugin );
246         daoUtil.setString( 1, codeProfil );
247         daoUtil.setString( 2, codeRole );
248 
249         daoUtil.executeUpdate( );
250         daoUtil.free( );
251     }
252 
253     /**
254      * Find assigned users to the given profil
255      * @param codeProfil The profil code
256      * @param plugin Plugin
257      * @return a list of WssoUser
258      */
259     public List<WssoUser> findWssoUsersForProfil( String codeProfil, Plugin plugin )
260     {
261 
262         List<WssoUser> listUsers = new ArrayList<WssoUser>( );
263         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_USERS_FOR_PROFIL, plugin );
264         daoUtil.setString( 1, codeProfil );
265         daoUtil.executeQuery( );
266 
267         while ( daoUtil.next( ) )
268         {
269             WssoUser wssoUser = new WssoUser( );
270             wssoUser.setMyluteceWssoUserId( daoUtil.getInt( 1 ) );
271             wssoUser.setGuid( daoUtil.getString( 2 ) );
272             wssoUser.setLastName( daoUtil.getString( 3 ) );
273             wssoUser.setFirstName( daoUtil.getString( 4 ) );
274             wssoUser.setEmail( daoUtil.getString( 5 ) );
275             wssoUser.setDateLastLogin( daoUtil.getDate( 6 ) );
276             listUsers.add( wssoUser );
277         }
278 
279         daoUtil.free( );
280 
281         return listUsers;
282     }
283 
284     /**
285      * 
286      * {@inheritDoc}
287      */
288     @Override
289     public void addUserForProfil( int wssoUserId, String codeProfil, Plugin plugin )
290     {
291 
292         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_ADD_USER_FOR_PROFIL, plugin );
293         daoUtil.setInt( 1, wssoUserId );
294         daoUtil.setString( 2, codeProfil );
295 
296         daoUtil.executeUpdate( );
297         daoUtil.free( );
298     }
299 
300     /**
301      * 
302      * {@inheritDoc}
303      */
304     @Override
305     public void removeUserForProfil( int wssoUserId, String codeProfil, Plugin plugin )
306     {
307 
308         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_USER_FOR_PROFIL, plugin );
309         daoUtil.setInt( 1, wssoUserId );
310         daoUtil.setString( 2, codeProfil );
311 
312         daoUtil.executeUpdate( );
313         daoUtil.free( );
314     }
315 
316     /**
317      * 
318      * {@inheritDoc}
319      */
320     @Override
321     public void removeProfilsForUser( int wssoUserId, Plugin plugin )
322     {
323         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_PROFILS_FOR_USER, plugin );
324         daoUtil.setInt( 1, wssoUserId );
325 
326         daoUtil.executeUpdate( );
327         daoUtil.free( );
328     }
329 
330 }