View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.service.security;
35  
36  import fr.paris.lutece.api.user.User;
37  import fr.paris.lutece.api.user.UserRole;
38  import fr.paris.lutece.portal.business.rbac.RBACRole;
39  import java.io.Serializable;
40  import java.security.Principal;
41  import java.util.ArrayList;
42  import java.util.Arrays;
43  import java.util.Collection;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import org.apache.commons.lang3.ObjectUtils;
49  
50  /**
51   * This Interface defines all methods required for a Lutece user implementation
52   */
53  public abstract class LuteceUser implements Principal, Serializable, Cloneable, User
54  {
55      /*
56       * These attribute names are derived from the Platform for Privacy Preferences 1.0 (P3P 1.0) Specification by the W3C (http://www.w3c.org/TR/P3P). The same
57       * attribute names are also being considered by the OASIS Web Services for Remote Portlets Technical Committee.
58       */
59      public static final String BDATE = "user.bdate";
60      public static final String GENDER = "user.gender";
61      public static final String EMPLOYER = "user.employer";
62      public static final String DEPARTMENT = "user.department";
63      public static final String JOBTITLE = "user.jobtitle";
64      public static final String PREFIX = "user.name.prefix";
65      public static final String DATE_LAST_LOGIN = "user.lastLogin";
66      public static final String NAME_GIVEN = "user.name.given";
67      public static final String NAME_FAMILY = "user.name.family";
68      public static final String NAME_MIDDLE = "user.name.middle";
69      public static final String NAME_SUFFIX = "user.name.suffix";
70      public static final String NAME_NICKNAME = "user.name.nickName";
71      public static final String NAME_CIVILITY = "user.name.civility";
72      public static final String HOME_INFO_POSTAL_NAME = "user.home-info.postal.name";
73      public static final String HOME_INFO_POSTAL_STREET = "user.home-info.postal.street";
74      public static final String HOME_INFO_POSTAL_STREET_NUMBER = "user.home-info.postal.street.number";
75      public static final String HOME_INFO_POSTAL_STREET_SUFFIX = "user.home-info.postal.street.suffix";
76      public static final String HOME_INFO_POSTAL_STREET_NAME = "user.home-info.postal.street.name";
77      public static final String HOME_INFO_POSTAL_STREET_TYPE = "user.home-info.postal.street.type";
78      public static final String HOME_INFO_POSTAL_STREET_URBAN_DISTRICT = "user.home-info.postal.street.urbandistrict";
79      public static final String HOME_INFO_POSTAL_CITY = "user.home-info.postal.city";
80      public static final String HOME_INFO_POSTAL_STATEPROV = "user.home-info.postal.stateprov";
81      public static final String HOME_INFO_POSTAL_POSTALCODE = "user.home-info.postal.postalcode";
82      public static final String HOME_INFO_POSTAL_COUNTRY = "user.home-info.postal.country";
83      public static final String HOME_INFO_POSTAL_ORGANIZATION = "user.home-info.postal.organization";
84      public static final String HOME_INFO_TELECOM_TELEPHONE_INTCODE = "user.home-info.telecom.telephone.intcode";
85      public static final String HOME_INFO_TELECOM_TELEPHONE_LOCCODE = "user.home-info.telecom.telephone.loccode";
86      public static final String HOME_INFO_TELECOM_TELEPHONE_NUMBER = "user.home-info.telecom.telephone.number";
87      public static final String HOME_INFO_TELECOM_TELEPHONE_EXT = "user.home-info.telecom.telephone.ext";
88      public static final String HOME_INFO_TELECOM_TELEPHONE_COMMENT = "user.home-info.telecom.telephone.comment";
89      public static final String HOME_INFO_TELECOM_FAX_INT = "user.home-info.telecom.fax.intcode";
90      public static final String HOME_INFO_TELECOM_FAX_LOCCODE = "user.home-info.telecom.fax.loccode";
91      public static final String HOME_INFO_TELECOM_FAX_NUMBER = "user.home-info.telecom.fax.number";
92      public static final String HOME_INFO_TELECOM_FAX_EXT = "user.home-info.telecom.fax.ext";
93      public static final String HOME_INFO_TELECOM_FAX_COMMENT = "user.home-info.telecom.fax.comment";
94      public static final String HOME_INFO_TELECOM_MOBILE_INTCODE = "user.home-info.telecom.mobile.intcode";
95      public static final String HOME_INFO_TELECOM_MOBILE_LOCCODE = "user.home-info.telecom.mobile.loccode";
96      public static final String HOME_INFO_TELECOM_MOBILE_NUMBER = "user.home-info.telecom.mobile.number";
97      public static final String HOME_INFO_TELECOM_MOBILE_EXT = "user.home-info.telecom.mobile.ext";
98      public static final String HOME_INFO_TELECOM_MOBILE_COMMENT = "user.home-info.telecom.mobile.comment";
99      public static final String HOME_INFO_TELECOM_PAGER_INTCODE = "user.home-info.telecom.pager.intcode";
100     public static final String HOME_INFO_TELECOM_PAGER_LOCCODE = "user.home-info.telecom.pager.loccode";
101     public static final String HOME_INFO_TELECOM_PAGER_NUMBER = "user.home-info.telecom.pager.number";
102     public static final String HOME_INFO_TELECOM_PAGER_EXT = "user.home-info.telecom.pager.ext";
103     public static final String HOME_INFO_TELECOM_PAGER_COMMENT = "user.home-info.telecom.pager.comment";
104     public static final String HOME_INFO_ONLINE_EMAIL = "user.home-info.online.email";
105     public static final String HOME_INFO_ONLINE_URI = "user.home-info.online.uri";
106     public static final String BUSINESS_INFO_POSTAL_NAME = "user.business-info.postal.name";
107     public static final String BUSINESS_INFO_POSTAL_STREET = "user.business-info.postal.street";
108     public static final String BUSINESS_INFO_POSTAL_CITY = "user.business-info.postal.city";
109     public static final String BUSINESS_INFO_POSTAL_STATEPROV = "user.business-info.postal.stateprov";
110     public static final String BUSINESS_INFO_POSTAL_POSTALCODE = "user.business-info.postal.postalcode";
111     public static final String BUSINESS_INFO_POSTAL_COUNTRY = "user.business-info.postal.country";
112     public static final String BUSINESS_INFO_POSTAL_ORGANIZATION = "user.business-info.postal.organization";
113     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_INTCODE = "user.business-info.telecom.telephone.intcode";
114     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_LOCCODE = "user.business-info.telecom.telephone.loccode";
115     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_NUMBER = "user.business-info.telecom.telephone.number";
116     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_EXT = "user.business-info.telecom.telephone.ext";
117     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_COMMENT = "user.business-info.telecom.telephone.comment";
118     public static final String BUSINESS_INFO_TELECOM_FAX_INTCODE = "user.business-info.telecom.fax.intcode";
119     public static final String BUSINESS_INFO_TELECOM_FAX_LOCCODE = "user.business-info.telecom.fax.loccode";
120     public static final String BUSINESS_INFO_TELECOM_FAX_NUMBER = "user.business-info.telecom.fax.number";
121     public static final String BUSINESS_INFO_TELECOM_FAX_EXT = "user.business-info.telecom.fax.ext";
122     public static final String BUSINESS_INFO_TELECOM_FAX_COMMENT = "user.business-info.telecom.fax.comment";
123     public static final String BUSINESS_INFO_TELECOM_MOBILE_INTCODE = "user.business-info.telecom.mobile.intcode";
124     public static final String BUSINESS_INFO_TELECOM_MOBILE_LOCCODE = "user.business-info.telecom.mobile.loccode";
125     public static final String BUSINESS_INFO_TELECOM_MOBILE_NUMBER = "user.business-info.telecom.mobile.number";
126     public static final String BUSINESS_INFO_TELECOM_MOBILE_EXT = "user.business-info.telecom.mobile.ext";
127     public static final String BUSINESS_INFO_TELECOM_MOBILE_COMMENT = "user.business-info.telecom.mobile.comment";
128     public static final String BUSINESS_INFO_TELECOM_PAGER_INTCODE = "user.business-info.telecom.pager.intcode";
129     public static final String BUSINESS_INFO_TELECOM_PAGER_LOCCODE = "user.business-info.telecom.pager.loccode";
130     public static final String BUSINESS_INFO_TELECOM_PAGER_NUMBER = "user.business-info.telecom.pager.number";
131     public static final String BUSINESS_INFO_TELECOM_PAGER_EXT = "user.business-info.telecom.pager.ext";
132     public static final String BUSINESS_INFO_TELECOM_PAGER_COMMENT = "user.business-info.telecom.pager.comment";
133     public static final String BUSINESS_INFO_ONLINE_EMAIL = "user.business-info.online.email";
134     public static final String BUSINESS_INFO_ONLINE_URI = "user.business-info.online.uri";
135     public static final String ANONYMOUS_USERNAME = "GUEST";
136     /** USER REALM TYPE **/
137     public static final String USER_REALM = "FRONT_OFFICE_USER";
138     private static final long serialVersionUID = -8733640540563208835L;
139 
140     /** Map containing users info */
141     private Map<String, String> _mapUserInfo = new HashMap<>( );
142 
143     /** User's name */
144     private String _strUserName;
145 
146     /** User's roles */
147     private String [ ] _roles;
148 
149     /** User's groups */
150     private String [ ] _groups;
151 
152     /** Authentication Service */
153     private String _strAuthenticationService;
154 
155     /** Authentication Service impl */
156     private LuteceAuthentication _luteceAuthenticationService;
157 
158     /** Authentication Service */
159     private String _strAuthenticationType;
160     /** User's workgroups */
161     private List<String> _workgroups = new ArrayList<String>( );
162 
163     /**
164      * Constructor
165      * 
166      * @param strUserName
167      *            The User Name
168      * @param authenticationService
169      *            The PortalAuthentication object
170      */
171     public LuteceUser( String strUserName, LuteceAuthentication authenticationService )
172     {
173         _strUserName = strUserName;
174         _strAuthenticationService = authenticationService.getAuthServiceName( );
175         _luteceAuthenticationService = authenticationService;
176     }
177 
178     /**
179      * Gets the user info map
180      * 
181      * @return The user info map
182      */
183     public final Map<String, String> getUserInfos( )
184     {
185         return _mapUserInfo;
186     }
187 
188     /**
189      * Add an user's info
190      * 
191      * @param key
192      *            The info key
193      * @param value
194      *            The info value
195      */
196     public final void setUserInfo( String key, String value )
197     {
198         _mapUserInfo.put( key, value );
199     }
200 
201     /**
202      * Gets the user info value
203      * 
204      * @param key
205      *            The info key
206      * @return the user info value
207      */
208 
209     public final String getUserInfo( String key )
210     {
211         String strInfo = _mapUserInfo.get( key );
212         return ( strInfo == null ) ? "" : strInfo;
213     }
214 
215     // /////////////////////////////////////////////////////////////////////////
216     // Principal Interface Implementation
217 
218     /**
219      * equals implementation
220      * 
221      * @param object
222      *            The object to compare
223      * @return true if equal, otherwise false
224      */
225     @Override
226     public boolean equals( Object object )
227     {
228         // FIXME : use LuteceUser property instead of object.toString()
229         return ObjectUtils.equals( this.toString( ), ObjectUtils.toString( object ) );
230     }
231 
232     /**
233      * toString implementation
234      * 
235      * @return The username
236      */
237     @Override
238     public String toString( )
239     {
240         return _strUserName;
241     }
242 
243     /**
244      * hashCode implementation
245      * 
246      * @return The hashcode
247      */
248     @Override
249     public int hashCode( )
250     {
251         return ( _strUserName == null ) ? 0 : _strUserName.hashCode( );
252     }
253 
254     /**
255      * Return the user's name
256      * 
257      * @return The username
258      */
259     @Override
260     public String getName( )
261     {
262         return _strUserName;
263     }
264 
265     // /////////////////////////////////////////////////////////////////////////
266     // Other user's info methods
267 
268     /**
269      * Sets the user's name
270      * 
271      * @param strName
272      *            The User name
273      */
274     public void setName( String strName )
275     {
276         _strUserName = strName;
277     }
278 
279     /**
280      * Returns user's roles
281      * 
282      * @return Returns user's roles
283      */
284     public String [ ] getRoles( )
285     {
286         return _roles;
287     }
288 
289     /**
290      * {@inheritDoc }
291      */
292     @Override
293     public Map<String, UserRole> getUserRoles( )
294     {
295         Map<String, UserRole> mapRoles = new HashMap<>( );
296         for ( String strRole : _roles )
297         {
298             mapRoles.put( strRole, new RBACRole( strRole, strRole ) );
299         }
300         return mapRoles;
301     }
302 
303     /**
304      * add user's roles
305      * 
306      * @param roles
307      *            The User roles
308      */
309     public void addRoles( Collection<String> roles )
310     {
311         _roles = addInArray( _roles, roles );
312     }
313 
314     /**
315      * Defines user's roles
316      * 
317      * @param roles
318      *            The User roles
319      */
320     public void setRoles( Collection<String> roles )
321     {
322         _roles = getArray( roles );
323     }
324 
325     /**
326      * Returns user's groups
327      * 
328      * @return Returns user's groups
329      */
330     public String [ ] getGroups( )
331     {
332         return _groups;
333     }
334 
335     /**
336      * add user's groups
337      * 
338      * @param groups
339      *            The User groups
340      */
341     public void addGroups( Collection<String> groups )
342     {
343         _groups = addInArray( _groups, groups );
344     }
345 
346     /**
347      * Defines user's groups
348      * 
349      * @param groups
350      *            The User groups
351      */
352     public void setGroups( Collection<String> groups )
353     {
354         _groups = getArray( groups );
355     }
356 
357     /**
358      * Add elements of a collection into an array
359      *
360      * @param array
361      *            the array to fill
362      * @param collection
363      *            the collection containing the elements to add
364      * @return The new array
365      */
366     private String [ ] addInArray( String [ ] array, Collection<String> collection )
367     {
368         String [ ] newArray;
369 
370         int j = 0;
371 
372         if ( array == null )
373         {
374             newArray = new String [ collection.size( )];
375         }
376         else
377         {
378             newArray = Arrays.copyOf( array, collection.size( ) + array.length );
379         }
380 
381         for ( String strItem : collection )
382         {
383             newArray [j++] = strItem;
384         }
385 
386         return newArray;
387     }
388 
389     /**
390      * Set elements of a collection in array
391      *
392      * @param collection
393      *            the collection containing the elements to add
394      * @return An array
395      */
396     private String [ ] getArray( Collection<String> collection )
397     {
398         String [ ] newArray = new String [ collection.size( )];
399 
400         int j = 0;
401 
402         for ( String strItem : collection )
403         {
404             newArray [j++] = strItem;
405         }
406 
407         return newArray;
408     }
409 
410     // //////////////////////////////////////////////////////////////////////////
411     // Authentication infos
412 
413     /**
414      * Defines the authentification service that had authentified the user
415      * 
416      * @param strAuthenticationService
417      *            The authentification service
418      */
419     public void setAuthenticationService( String strAuthenticationService )
420     {
421         _strAuthenticationService = strAuthenticationService;
422     }
423 
424     /**
425      * Returns the authentification service that had authentified the user
426      * 
427      * @return the authentification service that had authentified the user
428      */
429     public String getAuthenticationService( )
430     {
431         return _strAuthenticationService;
432     }
433 
434     /**
435      * Defines the authentification type that had authentified the user
436      * 
437      * @param strAuthenticationType
438      *            The authentification type
439      */
440     public void setAuthenticationType( String strAuthenticationType )
441     {
442         _strAuthenticationType = strAuthenticationType;
443     }
444 
445     /**
446      * Returns the authentification type that had authentified the user
447      * 
448      * @return the authentification type that had authentified the user
449      */
450     public String getAuthenticationType( )
451     {
452         return _strAuthenticationType;
453     }
454 
455     /**
456      * "Getter method" for {@link #_luteceAuthenticationService}
457      * 
458      * @return value of {@link #_luteceAuthenticationService}
459      */
460     public LuteceAuthentication getLuteceAuthenticationService( )
461     {
462         return _luteceAuthenticationService;
463     }
464 
465     /**
466      * "Setter method" for {@link #_luteceAuthenticationService}.
467      * 
468      * @param authenticationService
469      *            new value of {@link #_luteceAuthenticationService}
470      */
471     public void setLuteceAuthenticationService( LuteceAuthentication authenticationService )
472     {
473         _luteceAuthenticationService = authenticationService;
474     }
475 
476     /**
477      * {@inheritDoc}
478      */
479     @Override
480     public Object clone( ) throws CloneNotSupportedException
481     {
482         return super.clone( );
483     }
484 
485     /**
486      * Get the users email
487      * 
488      * @return The email
489      */
490     @Override
491     public String getEmail( )
492     {
493         return null;
494     }
495 
496     /**
497      * {@inheritDoc}
498      */
499     @Override
500     public String getAccessCode( )
501     {
502         return getName( );
503     }
504 
505     /**
506      * {@inheritDoc}
507      */
508     @Override
509     public String getLastName( )
510     {
511         return this.getUserInfo( LuteceUser.NAME_FAMILY );
512     }
513 
514     /**
515      * {@inheritDoc}
516      */
517     public @Override String getFirstName( )
518     {
519         return this.getUserInfo( LuteceUser.NAME_GIVEN );
520 
521     }
522 
523     /**
524      * {@inheritDoc}
525      */
526     @Override
527     public List<String> getUserWorkgroups( )
528     {
529         return _workgroups;
530     }
531 
532     /**
533      * {@inheritDoc}
534      */
535     @Override
536     public String getRealm( )
537     {
538         return USER_REALM;
539     }
540 }