View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.openiddatabase.authentication.business;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.openiddatabase.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.security.LuteceUser;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  
45  
46  /**
47   * This class provides Data Access methods for authentication (role retrieval).
48   *
49   */
50  public class OpenIdDatabaseDAO implements IManageUsers
51  {
52      private static final String SQL_QUERY_FIND_USER_BY_LOGIN = "SELECT mylutece_database_openid_user_id, login, name_family, name_given, email, authen_type" +
53          " FROM mylutece_database_openid_user WHERE login like ? ";
54      private static final String SQL_QUERY_FIND_ROLES_FROM_LOGIN = "SELECT b.role_key FROM mylutece_database_openid_user a, mylutece_database_openid_user_role b" +
55          " WHERE a.mylutece_database_openid_user_id = b.mylutece_database_openid_user_id AND a.login like ? ";
56      private static final String SQL_QUERY_DELETE_ROLES_FOR_USER = "DELETE FROM mylutece_database_openid_user_role WHERE mylutece_database_openid_user_id = ?";
57      private static final String SQL_QUERY_INSERT_ROLE_FOR_USER = "INSERT INTO mylutece_database_openid_user_role ( mylutece_database_openid_user_id, role_key ) VALUES ( ?, ? ) ";
58      private static final String SQL_QUERY_FIND_GROUPS_FROM_LOGIN = "SELECT b.group_key FROM mylutece_database_openid_user a, mylutece_database_openid_user_group b" +
59          " WHERE a.mylutece_database_openid_user_id = b.mylutece_database_openid_user_id AND a.login like ? ";
60      private static final String SQL_QUERY_DELETE_GROUPS_FOR_USER = "DELETE FROM mylutece_database_openid_user_group WHERE mylutece_database_openid_user_id = ?";
61      private static final String SQL_QUERY_INSERT_GROUP_FOR_USER = "INSERT INTO mylutece_database_openid_user_group ( mylutece_database_openid_user_id, group_key ) VALUES ( ?, ? ) ";
62      private static final String SQL_QUERY_SELECTALL = " SELECT mylutece_database_openid_user_id, login, name_family, name_given, email,authen_type FROM mylutece_database_openid_user ";
63  
64      /** This class implements the Singleton design pattern. */
65      private static OpenIdDatabaseDAO _dao = new OpenIdDatabaseDAO(  );
66  
67      /**
68       * Returns the unique instance of the singleton.
69       *
70       * @return the instance
71       */
72      static OpenIdDatabaseDAO getInstance(  )
73      {
74          return _dao;
75      }
76  
77      /**
78       * Find DatabaseUser by login
79       *
80       * @param strLogin the login
81       * @param plugin The Plugin using this data access service
82       * @param authenticationService the LuteceAuthentication object
83       * @return DatabaseUser the user corresponding to the login
84       */
85      public BaseUser selectLuteceUserByLogin( String strLogin, Plugin plugin, LuteceAuthentication authenticationService )
86      {
87          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_USER_BY_LOGIN, plugin );
88          daoUtil.setString( 1, strLogin );
89          daoUtil.executeQuery(  );
90  
91          if ( !daoUtil.next(  ) )
92          {
93              daoUtil.free(  );
94  
95              return null;
96          }
97  
98          String strLastName = daoUtil.getString( 3 );
99          String strFirstName = daoUtil.getString( 4 );
100         String strEmail = daoUtil.getString( 5 );
101         String strAuthentificationType = daoUtil.getString( 6 );
102 
103         BaseUser user = new BaseUser( strLogin, authenticationService );
104 
105         user.setUserInfo( LuteceUser.NAME_FAMILY, strLastName );
106         user.setUserInfo( LuteceUser.NAME_GIVEN, strFirstName );
107         user.setUserInfo( LuteceUser.BUSINESS_INFO_ONLINE_EMAIL, strEmail );
108         user.setUserInfo( BaseUser.AUTHENTICATION_TYPE, strAuthentificationType );
109         daoUtil.free(  );
110 
111         return user;
112     }
113 
114     /**
115      * Load the list of {@link BaseUser}
116      * @param plugin The Plugin using this data access service
117      * @return The Collection of the {@link BaseUser}
118      */
119     public Collection<BaseUser> selectLuteceUserList( Plugin plugin, LuteceAuthentication authenticationService )
120     {
121         Collection<BaseUser> listBaseUsers = new ArrayList<BaseUser>(  );
122         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
123         daoUtil.executeQuery(  );
124 
125         while ( daoUtil.next(  ) )
126         {
127             BaseUser user = new BaseUser( daoUtil.getString( 2 ), authenticationService );
128             user.setUserInfo( LuteceUser.NAME_FAMILY, daoUtil.getString( 3 ) );
129             user.setUserInfo( LuteceUser.NAME_GIVEN, daoUtil.getString( 4 ) );
130             user.setUserInfo( LuteceUser.BUSINESS_INFO_ONLINE_EMAIL, daoUtil.getString( 5 ) );
131             user.setUserInfo( BaseUser.AUTHENTICATION_TYPE, daoUtil.getString( 6 ) );
132             listBaseUsers.add( user );
133         }
134 
135         daoUtil.free(  );
136 
137         return listBaseUsers;
138     }
139 
140     /**
141      * Find user's roles by login
142      *
143      * @param strLogin the login
144      * @param plugin The Plugin using this data access service
145      * @return ArrayList the roles key list corresponding to the login
146      */
147     public ArrayList<String> selectUserRolesFromLogin( String strLogin, Plugin plugin )
148     {
149         ArrayList<String> arrayRoles = new ArrayList<String>(  );
150         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ROLES_FROM_LOGIN, plugin );
151         daoUtil.setString( 1, strLogin );
152         daoUtil.executeQuery(  );
153 
154         while ( daoUtil.next(  ) )
155         {
156             arrayRoles.add( daoUtil.getString( 1 ) );
157         }
158 
159         daoUtil.free(  );
160 
161         return arrayRoles;
162     }
163 
164     /**
165      * Delete roles for a user
166      * @param nIdUser The id of the user
167      * @param plugin The Plugin using this data access service
168      */
169     public void deleteRolesForUser( int nIdUser, Plugin plugin )
170     {
171         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ROLES_FOR_USER, plugin );
172         daoUtil.setInt( 1, nIdUser );
173 
174         daoUtil.executeUpdate(  );
175         daoUtil.free(  );
176     }
177 
178     /**
179      * Assign a role to user
180      * @param nIdUser The id of the user
181      * @param strRoleKey The key of the role
182      * @param plugin The Plugin using this data access service
183      */
184     public void createRoleForUser( int nIdUser, String strRoleKey, Plugin plugin )
185     {
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ROLE_FOR_USER, plugin );
187         daoUtil.setInt( 1, nIdUser );
188         daoUtil.setString( 2, strRoleKey );
189 
190         daoUtil.executeUpdate(  );
191         daoUtil.free(  );
192     }
193 
194     /**
195      * Find user's groups by login
196      *
197      * @param strLogin The login
198      * @param plugin The Plugin using this data access service
199      * @return ArrayList the group key list corresponding to the login
200      */
201     public ArrayList<String> selectUserGroupsFromLogin( String strLogin, Plugin plugin )
202     {
203         ArrayList<String> arrayGroups = new ArrayList<String>(  );
204         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_GROUPS_FROM_LOGIN, plugin );
205         daoUtil.setString( 1, strLogin );
206         daoUtil.executeQuery(  );
207 
208         while ( daoUtil.next(  ) )
209         {
210             arrayGroups.add( daoUtil.getString( 1 ) );
211         }
212 
213         daoUtil.free(  );
214 
215         return arrayGroups;
216     }
217 
218     /**
219      * Delete groups for a user
220      * @param nIdUser The id of the user
221      * @param plugin The Plugin using this data access service
222      */
223     public void deleteGroupsForUser( int nIdUser, Plugin plugin )
224     {
225         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_GROUPS_FOR_USER, plugin );
226         daoUtil.setInt( 1, nIdUser );
227 
228         daoUtil.executeUpdate(  );
229         daoUtil.free(  );
230     }
231 
232     /**
233      * Assign a group to user
234      * @param nIdUser The id of the user
235      * @param strGroupKey The key of the group
236      * @param plugin The Plugin using this data access service
237      */
238     public void createGroupForUser( int nIdUser, String strGroupKey, Plugin plugin )
239     {
240         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_GROUP_FOR_USER, plugin );
241         daoUtil.setInt( 1, nIdUser );
242         daoUtil.setString( 2, strGroupKey );
243 
244         daoUtil.executeUpdate(  );
245         daoUtil.free(  );
246     }
247 }