View Javadoc
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.service.security;
35  
36  import org.apache.commons.lang.ObjectUtils;
37  
38  import java.io.Serializable;
39  
40  import java.security.Principal;
41  
42  import java.sql.Timestamp;
43  
44  import java.util.Collection;
45  import java.util.HashMap;
46  import java.util.Map;
47  
48  
49  /**
50   * This Interface defines all methods required for a Lutece user implementation
51   */
52  public abstract class LuteceUser implements Principal, Serializable, Cloneable
53  {
54      /* These attribute names are derived from the Platform for Privacy
55       * Preferences 1.0 (P3P 1.0) Specification by the
56       * W3C (http://www.w3c.org/TR/P3P). The same attribute names are also being considered
57       * 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     public static final Timestamp DEFAULT_DATE_LAST_LOGIN = Timestamp.valueOf( "1980-01-01 00:00:00" );
137     private static final long serialVersionUID = -8733640540563208835L;
138 
139     /** Map containing users info */
140     private Map<String, String> _mapUserInfo = new HashMap<String, String>(  );
141 
142     /** User's name */
143     private String _strUserName;
144 
145     /** User's roles */
146     private String[] _roles;
147 
148     /** User's groups */
149     private String[] _groups;
150 
151     /** Authentication Service */
152     private String _strAuthenticationService;
153 
154     /** Authentication Service impl */
155     private LuteceAuthentication _luteceAuthenticationService;
156 
157     /** Authentication Service */
158     private String _strAuthenticationType;
159 
160     /**
161      * Constructor
162      * @param strUserName The User Name
163      * @param authenticationService The PortalAuthentication object
164      */
165     public LuteceUser( String strUserName, LuteceAuthentication authenticationService )
166     {
167         _strUserName = strUserName;
168         _strAuthenticationService = authenticationService.getAuthServiceName(  );
169         _luteceAuthenticationService = authenticationService;
170     }
171 
172     /**
173      * Gets the user info map
174      * @return The user info map
175      */
176     public final Map<String, String> getUserInfos(  )
177     {
178         return _mapUserInfo;
179     }
180 
181     /**
182      * Add an user's info
183      * @param key The info key
184      * @param value The info value
185      */
186     public final void setUserInfo( String key, String value )
187     {
188         _mapUserInfo.put( key, value );
189     }
190 
191     /**
192      * Gets a user's info
193      * @param key The info key
194      * @return The info value
195      */
196     public final String getUserInfo( String key )
197     {
198         String strInfo = _mapUserInfo.get( key );
199 
200         return ( strInfo == null ) ? "" : strInfo;
201     }
202 
203     // /////////////////////////////////////////////////////////////////////////
204     // Principal Interface Implementation
205 
206     /**
207      * equals implementation
208      * @param object The object to compare
209      * @return true if equal, otherwise false
210      */
211     @Override
212     public boolean equals( Object object )
213     {
214         // FIXME : use LuteceUser property instead of object.toString()
215         return ObjectUtils.equals( this.toString(  ), ObjectUtils.toString( object ) );
216     }
217 
218     /**
219      * toString implementation
220      * @return The username
221      */
222     @Override
223     public String toString(  )
224     {
225         return _strUserName;
226     }
227 
228     /**
229      * hashCode implementation
230      * @return The hashcode
231      */
232     @Override
233     public int hashCode(  )
234     {
235         return ( _strUserName == null ) ? 0 : _strUserName.hashCode(  );
236     }
237 
238     /**
239      * Return the user's name
240      * @return The username
241      */
242     @Override
243     public String getName(  )
244     {
245         return _strUserName;
246     }
247 
248     // /////////////////////////////////////////////////////////////////////////
249     // Other user's info methods
250 
251     /**
252      * Sets the user's name
253      * @param strName The User name
254      */
255     public void setName( String strName )
256     {
257         _strUserName = strName;
258     }
259 
260     /**
261      * Returns user's roles
262      * @return Returns user's roles
263      */
264     public String[] getRoles(  )
265     {
266         return _roles;
267     }
268 
269     /**
270      * add user's roles
271      * @param roles The User roles
272      */
273     public void addRoles( Collection<String> roles )
274     {
275         _roles = addInArray( _roles, roles );
276     }
277 
278     /**
279      * Defines user's roles
280      * @param roles The User roles
281      */
282     public void setRoles( Collection<String> roles )
283     {
284         _roles = getArray( roles );
285     }
286 
287     /**
288      * Returns user's groups
289      * @return Returns user's groups
290      */
291     public String[] getGroups(  )
292     {
293         return _groups;
294     }
295 
296     /**
297      * add user's groups
298      * @param groups The User groups
299      */
300     public void addGroups( Collection<String> groups )
301     {
302         _groups = addInArray( _groups, groups );
303     }
304 
305     /**
306      * Defines user's groups
307      * @param groups The User groups
308      */
309     public void setGroups( Collection<String> groups )
310     {
311         _groups = getArray( groups );
312     }
313 
314     /**
315      * Add elements of a collection into an array
316      *
317      * @param array the array to fill
318      * @param collection the collection containing the elements to add
319      * @return The new array
320      */
321     private String[] addInArray( String[] array, Collection<String> collection )
322     {
323         String[] newArray;
324 
325         int j = 0;
326 
327         if ( array == null )
328         {
329             newArray = new String[collection.size(  )];
330         }
331         else
332         {
333             newArray = new String[collection.size(  ) + array.length];
334 
335             for ( j = 0; j < array.length; j++ )
336             {
337                 newArray[j] = array[j];
338             }
339         }
340 
341         for ( String strItem : collection )
342         {
343             newArray[j++] = strItem;
344         }
345 
346         return newArray;
347     }
348 
349     /**
350      * Set elements of a collection in array
351      *
352      * @param collection the collection containing the elements to add
353      * @return An array
354      */
355     private String[] getArray( Collection<String> collection )
356     {
357         String[] newArray = new String[collection.size(  )];
358 
359         int j = 0;
360 
361         for ( String strItem : collection )
362         {
363             newArray[j++] = strItem;
364         }
365 
366         return newArray;
367     }
368 
369     // //////////////////////////////////////////////////////////////////////////
370     // Authentication infos
371 
372     /**
373      * Defines the authentification service that had authentified the user
374      * @param strAuthenticationService The authentification service
375      */
376     public void setAuthenticationService( String strAuthenticationService )
377     {
378         _strAuthenticationService = strAuthenticationService;
379     }
380 
381     /**
382      * Returns the authentification service that had authentified the user
383      * @return the authentification service that had authentified the user
384      */
385     public String getAuthenticationService(  )
386     {
387         return _strAuthenticationService;
388     }
389 
390     /**
391      * Defines the authentification type that had authentified the user
392      * @param strAuthenticationType The authentification type
393      */
394     public void setAuthenticationType( String strAuthenticationType )
395     {
396         _strAuthenticationType = strAuthenticationType;
397     }
398 
399     /**
400      * Returns the authentification type that had authentified the user
401      * @return the authentification type that had authentified the user
402      */
403     public String getAuthenticationType(  )
404     {
405         return _strAuthenticationType;
406     }
407 
408     /**
409      * "Getter method" for {@link #_luteceAuthenticationService}
410      * @return value of {@link #_luteceAuthenticationService}
411      */
412     public LuteceAuthentication getLuteceAuthenticationService(  )
413     {
414         return _luteceAuthenticationService;
415     }
416 
417     /**
418      * "Setter method" for {@link #_luteceAuthenticationService}.
419      * @param authenticationService new value of {@link #_luteceAuthenticationService}
420      */
421     public void setLuteceAuthenticationService( LuteceAuthentication authenticationService )
422     {
423         _luteceAuthenticationService = authenticationService;
424     }
425 
426     /**
427      * {@inheritDoc}
428      */
429     @Override
430     public Object clone(  ) throws CloneNotSupportedException
431     {
432         return super.clone(  );
433     }
434 
435     /**
436      * Get the users email
437      * @return The email
438      */
439     public String getEmail(  )
440     {
441         return null;
442     }
443 }