-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUserService.java
More file actions
384 lines (350 loc) · 13 KB
/
UserService.java
File metadata and controls
384 lines (350 loc) · 13 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (c) 2002-2019, Mairie de Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.plugins.appcenter.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import fr.paris.lutece.api.user.UserRole;
import fr.paris.lutece.plugins.appcenter.business.Role;
import fr.paris.lutece.plugins.appcenter.business.RoleHome;
import fr.paris.lutece.plugins.appcenter.business.User;
import fr.paris.lutece.plugins.appcenter.business.UserApplicationRole;
import fr.paris.lutece.plugins.appcenter.business.UserApplicationRoleHome;
import fr.paris.lutece.plugins.appcenter.business.UserHome;
import fr.paris.lutece.plugins.appcenter.business.UserInfos;
import fr.paris.lutece.portal.business.rbac.RBACHome;
import fr.paris.lutece.portal.business.rbac.RBACRole;
import fr.paris.lutece.portal.service.security.LuteceUser;
import fr.paris.lutece.portal.service.security.SecurityService;
import fr.paris.lutece.portal.service.security.UserNotSignedException;
import fr.paris.lutece.util.ReferenceList;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
/**
* UserService
*/
public class UserService
{
private static final String MOCK_USER = "john.doe@nowhere.com";
private static final String CONSTANTE_EMPTY_JSON = "{}";
private static ObjectMapper _mapper = new ObjectMapper( );
/**
* Get the list of available users
*
* @return The list
*/
public static ReferenceList getUserList( )
{
if ( SecurityService.isAuthenticationEnable( ) )
{
ReferenceList list = new ReferenceList( );
for ( LuteceUser user : SecurityService.getInstance( ).getUsers( ) )
{
list.addItem( getEmailUser( user ), getEmailUser( user ) );
}
return list;
}
else
{
return getMockUserList( );
}
}
/**
* Gets the Id of the current connected user
*
* @param request
* The HTTP request
* @return The user Id
*/
public static String getCurrentUserId( HttpServletRequest request )
{
if ( SecurityService.isAuthenticationEnable( ) )
{
LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
return getEmailUser( user );
}
else
{
return MOCK_USER;
}
}
/**
* Gets the current connected user
*
* @param request
* The HTTP request
* @param nApplicationId
* The applcation Id
* @return The user Id
*/
public static User getCurrentUser( HttpServletRequest request ) throws UserNotSignedException
{
User user = new User( );
if ( SecurityService.isAuthenticationEnable( ) )
{
LuteceUser luteceUser = SecurityService.getInstance( ).getRegisteredUser( request );
if ( luteceUser != null )
{
user = UserHome.findByPrimaryKey( getEmailUser( luteceUser ) );
if ( user == null )
{
user = new User( );
user.setId( getEmailUser( luteceUser ) );
user.setUserInfos( CONSTANTE_EMPTY_JSON );
UserHome.create( user );
user.setListUserApplicationRoles( UserApplicationRoleHome.getUserApplicationRolesListByIdUser( user.getId( ) ) );
}
}
else
{
throw new UserNotSignedException( );
}
}
else
{
user = UserHome.findByPrimaryKey( MOCK_USER );
if ( user != null )
{
user.setListUserApplicationRoles( UserApplicationRoleHome.getUserApplicationRolesListByIdUser( user.getId( ) ) );
}
}
return user;
}
/**
* Gets the user's email
*
* @param user
* The user
* @return The mail
*/
public static String getEmailUser( LuteceUser user )
{
String strUserEmail = null;
if ( user != null )
{
strUserEmail = ( user.getEmail( ) == null || user.getEmail( ).isEmpty( ) ) ? user.getUserInfo( LuteceUser.BUSINESS_INFO_ONLINE_EMAIL ) : user
.getEmail( );
}
return strUserEmail;
}
/**
* Mock list for dev and tests without MyLutece
*
* @return a mock list
*/
private static ReferenceList getMockUserList( )
{
ReferenceList list = new ReferenceList( );
list.addItem( MOCK_USER, MOCK_USER );
return list;
}
/**
* Get the user in the application context (with his specific role for the application)
*
* @param request
* @param nIdApplication
* @return
*/
public static User getCurrentUserInAppContext( HttpServletRequest request, int nIdApplication ) throws UserNotSignedException
{
User user = getCurrentUser( request );
if ( user.getListUserApplicationRoles( ) != null )
{
// Do a Home method instead
user.setListUserApplicationRoles( user.getListUserApplicationRoles( ).stream( )
.filter( userApplicationRole -> userApplicationRole.getIdApplication( ) == nIdApplication ).collect( Collectors.toList( ) ) );
}
return user;
}
/**
* Save a data subset into the global JSON data of an application
*
* @param user
* @param dataSubset
* The data subset
*/
public static void saveUserInfos( User user, DataSubset dataSubset )
{
try
{
String strUpdatedJson = getUserInfo( user, dataSubset );
UserHome.updateUserInfos( user.getId( ), strUpdatedJson );
user.setUserInfos( strUpdatedJson );
}
catch( IOException ex )
{
Logger.getLogger( UserService.class.getName( ) ).log( Level.SEVERE, null, ex );
}
}
/**
* Load a datasubset from the global JSON
*
* @param <UI>
* @param <T>
* The datasubset type
* @param user
* @param userInfosClass
*
* @return The data subset as an object
*/
public static <T extends UserInfos> T loadUserInfosDataSubset( User user, Class<T> userInfosClass )
{
try
{
Method mGetDataSetName = userInfosClass.getMethod( "getName" );
String strDataSetName = (String) mGetDataSetName.invoke( userInfosClass.newInstance( ), null );
String strUserInfosJson = user.getUserInfos( );
return getDataSubset( strUserInfosJson, strDataSetName, userInfosClass );
}
catch( IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| InstantiationException ex )
{
Logger.getLogger( ApplicationService.class.getName( ) ).log( Level.SEVERE, null, ex );
}
return null;
}
/**
* Load a datasubset from the global JSON
*
* @param <T>
* The datasubset type
* @param user
* @param strDataSubsetName
* The subset name
* @param valueType
* The class of the data subset
* @return The data subset as an object
*/
public static <T> T loadUserInfoSubset( User user, String strDataSubsetName, Class<T> valueType )
{
try
{
String strUserInfosJson = user.getUserInfos( );
return getDataSubset( strUserInfosJson, strDataSubsetName, valueType );
}
catch( IOException ex )
{
Logger.getLogger( UserService.class.getName( ) ).log( Level.SEVERE, null, ex );
}
return null;
}
/**
* Build a global JSON data of an application by adding or replacing a data subset
*
* @param application
* The application
* @param dataSubset
* The data subset
* @return The JSON
* @throws IOException
* if an error occurs
*/
static String getUserInfo( User user, DataSubset dataSubset ) throws IOException
{
String strUserInfos = user.getUserInfos( );
JsonNode nodeUserInfos;
if ( strUserInfos.isEmpty( ) )
{
nodeUserInfos = _mapper.createObjectNode( );
}
else
{
nodeUserInfos = _mapper.readTree( strUserInfos );
}
JsonNode nodeData = nodeUserInfos.get( dataSubset.getName( ) );
if ( nodeData != null )
{
( (ObjectNode) nodeUserInfos ).replace( dataSubset.getName( ), _mapper.valueToTree( dataSubset ) );
}
else
{
( (ObjectNode) nodeUserInfos ).set( dataSubset.getName( ), _mapper.valueToTree( dataSubset ) );
}
return nodeUserInfos.toString( );
}
/**
* Load a datasubset from the global JSON
*
* @param <T>
* The datasubset type
* @param nodeUserInfos
* The global JSON of the application
* @param strDataSubsetName
* The subset name
* @param valueType
* The class of the data subset
* @return The data subset as an object
*/
static <T> T getDataSubset( String strUserInfosJson, String strDataSubsetName, Class<T> valueType ) throws IOException
{
JsonNode nodeUserInfos = _mapper.readTree( strUserInfosJson );
JsonNode nodeData = nodeUserInfos.get( strDataSubsetName );
if ( nodeData != null )
{
return _mapper.treeToValue( nodeData, valueType );
}
return null;
}
/**
* Add RBACRole front to user
* @param user
* @param nIdApplication
*/
public static void addRBACRole ( User user, int nIdApplication )
{
Map<String, UserRole> mapRoles = new HashMap<>();
user.setListUserApplicationRoles( UserApplicationRoleHome.getUserApplicationRolesListByIdUser( user.getId( ) ) );
if ( user.getListUserApplicationRoles( ) != null )
{
user.setListUserApplicationRoles( user.getListUserApplicationRoles( ).stream( )
.filter( userApplicationRole -> userApplicationRole.getIdApplication( ) == nIdApplication ).collect( Collectors.toList( ) ) );
}
for ( UserApplicationRole userApplicationRole : user.getListUserApplicationRoles( ) )
{
Role role = RoleHome.findByPrimaryKey( userApplicationRole.getIdRole( ) );
RBACRole rbacRole = new RBACRole( "appcenter_" + role.getCode( ), role.getLabel( ) );
mapRoles.put( "appcenter_" + role.getCode( ), rbacRole );
}
user.setUserRoles( mapRoles );
}
}