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.portal.business.user;
35
36 import fr.paris.lutece.portal.business.rbac.AdminRole;
37 import fr.paris.lutece.portal.business.right.Right;
38 import fr.paris.lutece.portal.business.user.authentication.AdminAuthentication;
39 import fr.paris.lutece.portal.business.user.parameter.EmailPatternRegularExpressionRemovalListener;
40 import fr.paris.lutece.portal.service.regularexpression.RegularExpressionRemovalListenerService;
41 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupResource;
42 import fr.paris.lutece.portal.web.l10n.LocaleService;
43
44 import org.apache.commons.lang.StringUtils;
45
46 import java.io.Serializable;
47
48 import java.sql.Timestamp;
49
50 import java.util.HashMap;
51 import java.util.Locale;
52 import java.util.Map;
53
54
55 /**
56 * This Interface defines all methods required for an admin user implementation
57 */
58 public class AdminUser implements Serializable, AdminWorkgroupResource
59 {
60 public static final String RESOURCE_TYPE = "ADMIN_USER";
61 public static final int ACTIVE_CODE = 0;
62 public static final int NOT_ACTIVE_CODE = 1;
63 public static final int EXPIRED_CODE = 5;
64 public static final int ANONYMIZED_CODE = 10;
65 public static final Timestamp DEFAULT_DATE_LAST_LOGIN = Timestamp.valueOf( "1980-01-01 00:00:00" );
66 private static final long serialVersionUID = 7533831976351347197L;
67 private static EmailPatternRegularExpressionRemovalListener _listenerRegularExpression;
68 private int _nUserId;
69 private String _strAccessCode;
70 private String _strLastName;
71 private String _strFirstName;
72 private String _strEmail;
73 private int _nStatus;
74 private int _nUserLevel;
75 private boolean _bIsPasswordReset;
76 private boolean _bAccessibilityMode;
77 private Timestamp _passwordMaxValidDate;
78 private Timestamp _accountMaxValidDate;
79 private Timestamp _dateLastLogin;
80 private String _strWorkgroupKey;
81
82 /**
83 * User's rights. We use a HashMap instead of a Map so that the field is
84 * forced to be serializable.
85 */
86 private HashMap<String, Right> _rights = new HashMap<String, Right>( );
87
88 /**
89 * User's roles. We use a HashMap instead of a Map so that the field is
90 * forced to be serializable.
91 */
92 private HashMap<String, AdminRole> _roles = new HashMap<String, AdminRole>( );
93
94 /** Authentication Service */
95 private String _strAuthenticationService;
96
97 /** Authentication Service */
98 private String _strAuthenticationType;
99
100 /** the user's locale */
101 private Locale _locale;
102
103 /**
104 * Constructor
105 */
106 public AdminUser( )
107 {
108 }
109
110 /**
111 * Constructor
112 * @param stAccessCode The User Name
113 * @param authenticationService The PortalAuthentication object
114 */
115 public AdminUser( String stAccessCode, AdminAuthentication authenticationService )
116 {
117 _strAccessCode = stAccessCode;
118 _strAuthenticationService = authenticationService.getAuthServiceName( );
119 }
120
121 /**
122 * Init
123 */
124 public static synchronized void init( )
125 {
126 if ( _listenerRegularExpression == null )
127 {
128 _listenerRegularExpression = new EmailPatternRegularExpressionRemovalListener( );
129 RegularExpressionRemovalListenerService.getService( ).registerListener( _listenerRegularExpression );
130 }
131 }
132
133 /**
134 * Get the user's Locale
135 * @return The user's locale
136 */
137 public Locale getLocale( )
138 {
139 return ( _locale == null ) ? LocaleService.getDefault( ) : _locale;
140 }
141
142 /**
143 * Set the user Locale
144 * @param locale The locale
145 */
146 public void setLocale( Locale locale )
147 {
148 _locale = locale;
149 }
150
151 /**
152 * Return the user's id
153 * @return The user id
154 */
155 public int getUserId( )
156 {
157 return _nUserId;
158 }
159
160 /**
161 * Sets the user's id
162 * @param nUserId The User id
163 */
164 public void setUserId( int nUserId )
165 {
166 _nUserId = nUserId;
167 }
168
169 /**
170 * @return Returns the status. Only ACTIVE_CODE, NOT_ACTIVE_CODE or
171 * ANONYMIZED_CODE are returned.
172 * If the status in an other status, then its equivalent is returned
173 */
174 public int getStatus( )
175 {
176 switch ( _nStatus )
177 {
178 case ACTIVE_CODE:
179 case ANONYMIZED_CODE:
180 case NOT_ACTIVE_CODE:
181 return _nStatus;
182
183 case EXPIRED_CODE:
184 return ANONYMIZED_CODE;
185
186 default:
187 return ACTIVE_CODE;
188 }
189 }
190
191 /**
192 * @return Returns the real status of the user.
193 */
194 public int getRealStatus( )
195 {
196 return _nStatus;
197 }
198
199 /**
200 * @param nStatus The _nStatus to set.
201 */
202 public void setStatus( int nStatus )
203 {
204 _nStatus = nStatus;
205 }
206
207 /**
208 * Tells whether the current user is active or not
209 * @return true if active, false otherwise
210 */
211 public boolean isStatusActive( )
212 {
213 return ( _nStatus == ACTIVE_CODE );
214 }
215
216 /**
217 * Tells whether the current user is anonymized
218 * @return true if anonymized, false otherwise
219 */
220 public boolean isStatusAnonymized( )
221 {
222 return ( _nStatus == ANONYMIZED_CODE );
223 }
224
225 /**
226 * Returns the last name of this user.
227 *
228 * @return the user last name
229 */
230 public String getLastName( )
231 {
232 return _strLastName;
233 }
234
235 /**
236 * Sets the last name of the user to the specified string.
237 *
238 * @param strLastName the new last name
239 */
240 public void setLastName( String strLastName )
241 {
242 _strLastName = ( strLastName == null ) ? StringUtils.EMPTY : strLastName;
243 }
244
245 /**
246 * Returns the first name of this user.
247 *
248 * @return the user first name
249 */
250 public String getFirstName( )
251 {
252 return _strFirstName;
253 }
254
255 /**
256 * Sets the first name of the user to the specified string.
257 *
258 * @param strFirstName the new first name
259 */
260 public void setFirstName( String strFirstName )
261 {
262 _strFirstName = ( strFirstName == null ) ? StringUtils.EMPTY : strFirstName;
263 }
264
265 /**
266 * Returns the email of this user.
267 *
268 * @return the user email
269 */
270 public String getEmail( )
271 {
272 return _strEmail;
273 }
274
275 /**
276 * Sets the email of the user to the specified string.
277 *
278 * @param strEmail the new email
279 */
280 public void setEmail( String strEmail )
281 {
282 _strEmail = ( strEmail == null ) ? StringUtils.EMPTY : strEmail;
283 }
284
285 /**
286 * @return Returns the _strAccessCode.
287 */
288 public String getAccessCode( )
289 {
290 return _strAccessCode;
291 }
292
293 /**
294 * @param strAccessCode The _strAccessCode to set.
295 */
296 public void setAccessCode( String strAccessCode )
297 {
298 _strAccessCode = strAccessCode;
299 }
300
301 /**
302 * Get the maximum valid date of the password of the user
303 * @return The maximum valid date of the password of the user
304 */
305 public Timestamp getPasswordMaxValidDate( )
306 {
307 return _passwordMaxValidDate;
308 }
309
310 /**
311 * Set the maximum valid date of the password of the user
312 * @param passwordMaxValidDate The new maximum valid date of the password of
313 * the user, or null if it doesn't have any.
314 */
315 public void setPasswordMaxValidDate( Timestamp passwordMaxValidDate )
316 {
317 _passwordMaxValidDate = passwordMaxValidDate;
318 }
319
320 /**
321 * Get the expiration date of the user account.
322 * @return The expiration date of the user account, or null if it doesn't
323 * have any.
324 */
325 public Timestamp getAccountMaxValidDate( )
326 {
327 return _accountMaxValidDate;
328 }
329
330 /**
331 * Set the expiration date of the user account.
332 * @param accountMaxValidDate The new expiration date of the user account.
333 */
334 public void setAccountMaxValidDate( Timestamp accountMaxValidDate )
335 {
336 _accountMaxValidDate = accountMaxValidDate;
337 }
338
339 /**
340 * Returns user's roles
341 * @return Returns user's roles
342 */
343 public Map<String, AdminRole> getRoles( )
344 {
345 return _roles;
346 }
347
348 /**
349 * add user's roles
350 * @param roles The User roles
351 */
352 public void addRoles( Map<String, AdminRole> roles )
353 {
354 _roles.putAll( roles );
355 }
356
357 /**
358 * Defines user's roles
359 * @param roles The User roles
360 */
361 public void setRoles( Map<String, AdminRole> roles )
362 {
363 _roles.clear( );
364 _roles.putAll( roles );
365 }
366
367 /**
368 * Returns user's rights
369 * @return Returns user's rights
370 */
371 public Map<String, Right> getRights( )
372 {
373 return _rights;
374 }
375
376 /**
377 * Verify user rights on a given functionality
378 *
379 * @param strRightCode right code which corresponding to the functionality
380 * @return true if user have this authorisation and false otherwise
381 */
382 public boolean checkRight( String strRightCode )
383 {
384 return _rights.containsKey( strRightCode );
385 }
386
387 /**
388 * Defines user's rights
389 * @param rights The User rights
390 */
391 public void setRights( Map<String, Right> rights )
392 {
393 _rights.clear( );
394 _rights.putAll( rights );
395 }
396
397 // //////////////////////////////////////////////////////////////////////////
398 // Authentication infos
399
400 /**
401 * Defines the authentification service that had authentified the user
402 * @param strAuthenticationService The authentification service
403 */
404 public void setAuthenticationService( String strAuthenticationService )
405 {
406 _strAuthenticationService = strAuthenticationService;
407 }
408
409 /**
410 * Returns the authentification service that had authentified the user
411 * @return the authentification service that had authentified the user
412 */
413 public String getAuthenticationService( )
414 {
415 return _strAuthenticationService;
416 }
417
418 /**
419 * Defines the authentification type that had authentified the user
420 * @param strAuthenticationType The authentification type
421 */
422 public void setAuthenticationType( String strAuthenticationType )
423 {
424 _strAuthenticationType = strAuthenticationType;
425 }
426
427 /**
428 * Returns the authentification type that had authentified the user
429 * @return the authentification type that had authentified the user
430 */
431 public String getAuthenticationType( )
432 {
433 return _strAuthenticationType;
434 }
435
436 /**
437 * Defines the user level
438 * @param nUserLevel the user level
439 */
440 public void setUserLevel( int nUserLevel )
441 {
442 _nUserLevel = nUserLevel;
443 }
444
445 /**
446 * Returns the user level
447 * @return the user level
448 */
449 public int getUserLevel( )
450 {
451 return _nUserLevel;
452 }
453
454 /**
455 * Check if current user has rights over user
456 * @param user the user to check
457 * @return true if current user has higher level than user
458 */
459 public boolean isParent( AdminUser user )
460 {
461 return _nUserLevel < user.getUserLevel( );
462 }
463
464 /**
465 * Check if current user has rights depending on level
466 * @param level a level id
467 * @return true if current user has higher level than level
468 */
469 public boolean hasRights( int level )
470 {
471 return _nUserLevel < level;
472 }
473
474 /**
475 * Check if this user has admin rights
476 * @return true if user has admin rights
477 */
478 public boolean isAdmin( )
479 {
480 return _nUserLevel == 0;
481 }
482
483 /**
484 * Check if this user has a given role
485 * @param strRole The role key
486 * @return true if user has the role
487 */
488 public boolean isInRole( String strRole )
489 {
490 // Reload roles because roles are only load by the bind and should not be accessible
491 // through users list for security reasons
492 Map<String, AdminRole> roles = AdminUserHome.getRolesListForUser( getUserId( ) );
493
494 return roles.containsKey( strRole );
495 }
496
497 /**
498 * Check if the password has been reinitialized
499 * @return true if it has been reinitialized, false otherwise
500 */
501 public boolean isPasswordReset( )
502 {
503 return _bIsPasswordReset;
504 }
505
506 /**
507 * Set pwd reseted
508 * @param bIsPasswordReset true if it has been reinitialized, false
509 * otherwise
510 */
511 public void setPasswordReset( boolean bIsPasswordReset )
512 {
513 _bIsPasswordReset = bIsPasswordReset;
514 }
515
516 /**
517 * Set the accessibility mode
518 * @param bAccessibilityMode true if the mode is accessible, false otherwise
519 */
520 public void setAccessibilityMode( boolean bAccessibilityMode )
521 {
522 _bAccessibilityMode = bAccessibilityMode;
523 }
524
525 /**
526 * Return the accessibility mode
527 * @return true if the mode is accessible, false otherwise
528 */
529 public boolean getAccessibilityMode( )
530 {
531 return _bAccessibilityMode;
532 }
533
534 /**
535 * Get the last login date of the user
536 * @return The last login date of the user
537 */
538 public Timestamp getDateLastLogin( )
539 {
540 return _dateLastLogin;
541 }
542
543 /**
544 * Set the last login date of the user
545 * @param dateLastLogin The last login date of the user
546 */
547 public void setDateLastLogin( Timestamp dateLastLogin )
548 {
549 _dateLastLogin = dateLastLogin;
550 }
551
552 /**
553 * @return the _strWorkgroupKey
554 */
555 public String getWorkgroupKey( )
556 {
557 return _strWorkgroupKey;
558 }
559
560 /**
561 * @param strWorkgroupKey the _strWorkgroupKey to set
562 */
563 public void setWorkgroupKey( String strWorkgroupKey )
564 {
565 this._strWorkgroupKey = strWorkgroupKey;
566 }
567
568 @Override
569 public String getWorkgroup( )
570 {
571 return _strWorkgroupKey;
572 }
573 }