1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
79 private static IdxWSSODatabaseDAO _dao = new IdxWSSODatabaseDAO( );
80
81
82
83
84
85
86 static IdxWSSODatabaseDAO getInstance( )
87 {
88 return _dao;
89 }
90
91
92
93
94
95
96
97
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
127
128
129
130
131
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
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
167
168
169
170
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
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
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
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
255
256
257
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
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
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
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 }