LuteceUser.java

  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. import fr.paris.lutece.api.user.User;
  36. import fr.paris.lutece.api.user.UserRole;
  37. import fr.paris.lutece.portal.business.rbac.RBACRole;
  38. import java.io.Serializable;
  39. import java.security.Principal;
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.Collection;
  43. import java.util.HashMap;
  44. import java.util.List;
  45. import java.util.Map;

  46. import org.apache.commons.lang3.ObjectUtils;

  47. /**
  48.  * This Interface defines all methods required for a Lutece user implementation
  49.  */
  50. public abstract class LuteceUser implements Principal, Serializable, Cloneable, User
  51. {
  52.     /*
  53.      * 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
  54.      * attribute names are also being considered by the OASIS Web Services for Remote Portlets Technical Committee.
  55.      */
  56.     public static final String BDATE = "user.bdate";
  57.     public static final String GENDER = "user.gender";
  58.     public static final String EMPLOYER = "user.employer";
  59.     public static final String DEPARTMENT = "user.department";
  60.     public static final String JOBTITLE = "user.jobtitle";
  61.     public static final String PREFIX = "user.name.prefix";
  62.     public static final String DATE_LAST_LOGIN = "user.lastLogin";
  63.     public static final String NAME_GIVEN = "user.name.given";
  64.     public static final String NAME_FAMILY = "user.name.family";
  65.     public static final String NAME_MIDDLE = "user.name.middle";
  66.     public static final String NAME_SUFFIX = "user.name.suffix";
  67.     public static final String NAME_NICKNAME = "user.name.nickName";
  68.     public static final String NAME_CIVILITY = "user.name.civility";
  69.     public static final String HOME_INFO_POSTAL_NAME = "user.home-info.postal.name";
  70.     public static final String HOME_INFO_POSTAL_STREET = "user.home-info.postal.street";
  71.     public static final String HOME_INFO_POSTAL_STREET_NUMBER = "user.home-info.postal.street.number";
  72.     public static final String HOME_INFO_POSTAL_STREET_SUFFIX = "user.home-info.postal.street.suffix";
  73.     public static final String HOME_INFO_POSTAL_STREET_NAME = "user.home-info.postal.street.name";
  74.     public static final String HOME_INFO_POSTAL_STREET_TYPE = "user.home-info.postal.street.type";
  75.     public static final String HOME_INFO_POSTAL_STREET_URBAN_DISTRICT = "user.home-info.postal.street.urbandistrict";
  76.     public static final String HOME_INFO_POSTAL_CITY = "user.home-info.postal.city";
  77.     public static final String HOME_INFO_POSTAL_STATEPROV = "user.home-info.postal.stateprov";
  78.     public static final String HOME_INFO_POSTAL_POSTALCODE = "user.home-info.postal.postalcode";
  79.     public static final String HOME_INFO_POSTAL_COUNTRY = "user.home-info.postal.country";
  80.     public static final String HOME_INFO_POSTAL_ORGANIZATION = "user.home-info.postal.organization";
  81.     public static final String HOME_INFO_TELECOM_TELEPHONE_INTCODE = "user.home-info.telecom.telephone.intcode";
  82.     public static final String HOME_INFO_TELECOM_TELEPHONE_LOCCODE = "user.home-info.telecom.telephone.loccode";
  83.     public static final String HOME_INFO_TELECOM_TELEPHONE_NUMBER = "user.home-info.telecom.telephone.number";
  84.     public static final String HOME_INFO_TELECOM_TELEPHONE_EXT = "user.home-info.telecom.telephone.ext";
  85.     public static final String HOME_INFO_TELECOM_TELEPHONE_COMMENT = "user.home-info.telecom.telephone.comment";
  86.     public static final String HOME_INFO_TELECOM_FAX_INT = "user.home-info.telecom.fax.intcode";
  87.     public static final String HOME_INFO_TELECOM_FAX_LOCCODE = "user.home-info.telecom.fax.loccode";
  88.     public static final String HOME_INFO_TELECOM_FAX_NUMBER = "user.home-info.telecom.fax.number";
  89.     public static final String HOME_INFO_TELECOM_FAX_EXT = "user.home-info.telecom.fax.ext";
  90.     public static final String HOME_INFO_TELECOM_FAX_COMMENT = "user.home-info.telecom.fax.comment";
  91.     public static final String HOME_INFO_TELECOM_MOBILE_INTCODE = "user.home-info.telecom.mobile.intcode";
  92.     public static final String HOME_INFO_TELECOM_MOBILE_LOCCODE = "user.home-info.telecom.mobile.loccode";
  93.     public static final String HOME_INFO_TELECOM_MOBILE_NUMBER = "user.home-info.telecom.mobile.number";
  94.     public static final String HOME_INFO_TELECOM_MOBILE_EXT = "user.home-info.telecom.mobile.ext";
  95.     public static final String HOME_INFO_TELECOM_MOBILE_COMMENT = "user.home-info.telecom.mobile.comment";
  96.     public static final String HOME_INFO_TELECOM_PAGER_INTCODE = "user.home-info.telecom.pager.intcode";
  97.     public static final String HOME_INFO_TELECOM_PAGER_LOCCODE = "user.home-info.telecom.pager.loccode";
  98.     public static final String HOME_INFO_TELECOM_PAGER_NUMBER = "user.home-info.telecom.pager.number";
  99.     public static final String HOME_INFO_TELECOM_PAGER_EXT = "user.home-info.telecom.pager.ext";
  100.     public static final String HOME_INFO_TELECOM_PAGER_COMMENT = "user.home-info.telecom.pager.comment";
  101.     public static final String HOME_INFO_ONLINE_EMAIL = "user.home-info.online.email";
  102.     public static final String HOME_INFO_ONLINE_URI = "user.home-info.online.uri";
  103.     public static final String BUSINESS_INFO_POSTAL_NAME = "user.business-info.postal.name";
  104.     public static final String BUSINESS_INFO_POSTAL_STREET = "user.business-info.postal.street";
  105.     public static final String BUSINESS_INFO_POSTAL_CITY = "user.business-info.postal.city";
  106.     public static final String BUSINESS_INFO_POSTAL_STATEPROV = "user.business-info.postal.stateprov";
  107.     public static final String BUSINESS_INFO_POSTAL_POSTALCODE = "user.business-info.postal.postalcode";
  108.     public static final String BUSINESS_INFO_POSTAL_COUNTRY = "user.business-info.postal.country";
  109.     public static final String BUSINESS_INFO_POSTAL_ORGANIZATION = "user.business-info.postal.organization";
  110.     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_INTCODE = "user.business-info.telecom.telephone.intcode";
  111.     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_LOCCODE = "user.business-info.telecom.telephone.loccode";
  112.     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_NUMBER = "user.business-info.telecom.telephone.number";
  113.     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_EXT = "user.business-info.telecom.telephone.ext";
  114.     public static final String BUSINESS_INFO_TELECOM_TELEPHONE_COMMENT = "user.business-info.telecom.telephone.comment";
  115.     public static final String BUSINESS_INFO_TELECOM_FAX_INTCODE = "user.business-info.telecom.fax.intcode";
  116.     public static final String BUSINESS_INFO_TELECOM_FAX_LOCCODE = "user.business-info.telecom.fax.loccode";
  117.     public static final String BUSINESS_INFO_TELECOM_FAX_NUMBER = "user.business-info.telecom.fax.number";
  118.     public static final String BUSINESS_INFO_TELECOM_FAX_EXT = "user.business-info.telecom.fax.ext";
  119.     public static final String BUSINESS_INFO_TELECOM_FAX_COMMENT = "user.business-info.telecom.fax.comment";
  120.     public static final String BUSINESS_INFO_TELECOM_MOBILE_INTCODE = "user.business-info.telecom.mobile.intcode";
  121.     public static final String BUSINESS_INFO_TELECOM_MOBILE_LOCCODE = "user.business-info.telecom.mobile.loccode";
  122.     public static final String BUSINESS_INFO_TELECOM_MOBILE_NUMBER = "user.business-info.telecom.mobile.number";
  123.     public static final String BUSINESS_INFO_TELECOM_MOBILE_EXT = "user.business-info.telecom.mobile.ext";
  124.     public static final String BUSINESS_INFO_TELECOM_MOBILE_COMMENT = "user.business-info.telecom.mobile.comment";
  125.     public static final String BUSINESS_INFO_TELECOM_PAGER_INTCODE = "user.business-info.telecom.pager.intcode";
  126.     public static final String BUSINESS_INFO_TELECOM_PAGER_LOCCODE = "user.business-info.telecom.pager.loccode";
  127.     public static final String BUSINESS_INFO_TELECOM_PAGER_NUMBER = "user.business-info.telecom.pager.number";
  128.     public static final String BUSINESS_INFO_TELECOM_PAGER_EXT = "user.business-info.telecom.pager.ext";
  129.     public static final String BUSINESS_INFO_TELECOM_PAGER_COMMENT = "user.business-info.telecom.pager.comment";
  130.     public static final String BUSINESS_INFO_ONLINE_EMAIL = "user.business-info.online.email";
  131.     public static final String BUSINESS_INFO_ONLINE_URI = "user.business-info.online.uri";
  132.     public static final String ANONYMOUS_USERNAME = "GUEST";
  133.     /** USER REALM TYPE **/
  134.     public static final String USER_REALM = "FRONT_OFFICE_USER";
  135.     private static final long serialVersionUID = -8733640540563208835L;

  136.     /** Map containing users info */
  137.     private Map<String, String> _mapUserInfo = new HashMap<>( );

  138.     /** User's name */
  139.     private String _strUserName;

  140.     /** User's roles */
  141.     private String [ ] _roles;

  142.     /** User's groups */
  143.     private String [ ] _groups;

  144.     /** Authentication Service */
  145.     private String _strAuthenticationService;

  146.     /** Authentication Service impl */
  147.     private LuteceAuthentication _luteceAuthenticationService;

  148.     /** Authentication Service */
  149.     private String _strAuthenticationType;
  150.     /** User's workgroups */
  151.     private List<String> _workgroups = new ArrayList<String>( );

  152.     /**
  153.      * Constructor
  154.      *
  155.      * @param strUserName
  156.      *            The User Name
  157.      * @param authenticationService
  158.      *            The PortalAuthentication object
  159.      */
  160.     public LuteceUser( String strUserName, LuteceAuthentication authenticationService )
  161.     {
  162.         _strUserName = strUserName;
  163.         _strAuthenticationService = authenticationService.getAuthServiceName( );
  164.         _luteceAuthenticationService = authenticationService;
  165.     }

  166.     /**
  167.      * Gets the user info map
  168.      *
  169.      * @return The user info map
  170.      */
  171.     public final Map<String, String> getUserInfos( )
  172.     {
  173.         return _mapUserInfo;
  174.     }

  175.     /**
  176.      * Add an user's info
  177.      *
  178.      * @param key
  179.      *            The info key
  180.      * @param value
  181.      *            The info value
  182.      */
  183.     public final void setUserInfo( String key, String value )
  184.     {
  185.         _mapUserInfo.put( key, value );
  186.     }

  187.     /**
  188.      * Gets the user info value
  189.      *
  190.      * @param key
  191.      *            The info key
  192.      * @return the user info value
  193.      */

  194.     public final String getUserInfo( String key )
  195.     {
  196.         String strInfo = _mapUserInfo.get( key );
  197.         return ( strInfo == null ) ? "" : strInfo;
  198.     }

  199.     // /////////////////////////////////////////////////////////////////////////
  200.     // Principal Interface Implementation

  201.     /**
  202.      * equals implementation
  203.      *
  204.      * @param object
  205.      *            The object to compare
  206.      * @return true if equal, otherwise false
  207.      */
  208.     @Override
  209.     public boolean equals( Object object )
  210.     {
  211.         // FIXME : use LuteceUser property instead of object.toString()
  212.         return ObjectUtils.equals( this.toString( ), ObjectUtils.toString( object ) );
  213.     }

  214.     /**
  215.      * toString implementation
  216.      *
  217.      * @return The username
  218.      */
  219.     @Override
  220.     public String toString( )
  221.     {
  222.         return _strUserName;
  223.     }

  224.     /**
  225.      * hashCode implementation
  226.      *
  227.      * @return The hashcode
  228.      */
  229.     @Override
  230.     public int hashCode( )
  231.     {
  232.         return ( _strUserName == null ) ? 0 : _strUserName.hashCode( );
  233.     }

  234.     /**
  235.      * Return the user's name
  236.      *
  237.      * @return The username
  238.      */
  239.     @Override
  240.     public String getName( )
  241.     {
  242.         return _strUserName;
  243.     }

  244.     // /////////////////////////////////////////////////////////////////////////
  245.     // Other user's info methods

  246.     /**
  247.      * Sets the user's name
  248.      *
  249.      * @param strName
  250.      *            The User name
  251.      */
  252.     public void setName( String strName )
  253.     {
  254.         _strUserName = strName;
  255.     }

  256.     /**
  257.      * Returns user's roles
  258.      *
  259.      * @return Returns user's roles
  260.      */
  261.     public String [ ] getRoles( )
  262.     {
  263.         return _roles;
  264.     }

  265.     /**
  266.      * {@inheritDoc }
  267.      */
  268.     @Override
  269.     public Map<String, UserRole> getUserRoles( )
  270.     {
  271.         Map<String, UserRole> mapRoles = new HashMap<>( );
  272.         for ( String strRole : _roles )
  273.         {
  274.             mapRoles.put( strRole, new RBACRole( strRole, strRole ) );
  275.         }
  276.         return mapRoles;
  277.     }

  278.     /**
  279.      * add user's roles
  280.      *
  281.      * @param roles
  282.      *            The User roles
  283.      */
  284.     public void addRoles( Collection<String> roles )
  285.     {
  286.         _roles = addInArray( _roles, roles );
  287.     }

  288.     /**
  289.      * Defines user's roles
  290.      *
  291.      * @param roles
  292.      *            The User roles
  293.      */
  294.     public void setRoles( Collection<String> roles )
  295.     {
  296.         _roles = getArray( roles );
  297.     }

  298.     /**
  299.      * Returns user's groups
  300.      *
  301.      * @return Returns user's groups
  302.      */
  303.     public String [ ] getGroups( )
  304.     {
  305.         return _groups;
  306.     }

  307.     /**
  308.      * add user's groups
  309.      *
  310.      * @param groups
  311.      *            The User groups
  312.      */
  313.     public void addGroups( Collection<String> groups )
  314.     {
  315.         _groups = addInArray( _groups, groups );
  316.     }

  317.     /**
  318.      * Defines user's groups
  319.      *
  320.      * @param groups
  321.      *            The User groups
  322.      */
  323.     public void setGroups( Collection<String> groups )
  324.     {
  325.         _groups = getArray( groups );
  326.     }

  327.     /**
  328.      * Add elements of a collection into an array
  329.      *
  330.      * @param array
  331.      *            the array to fill
  332.      * @param collection
  333.      *            the collection containing the elements to add
  334.      * @return The new array
  335.      */
  336.     private String [ ] addInArray( String [ ] array, Collection<String> collection )
  337.     {
  338.         String [ ] newArray;

  339.         int j = 0;

  340.         if ( array == null )
  341.         {
  342.             newArray = new String [ collection.size( )];
  343.         }
  344.         else
  345.         {
  346.             newArray = Arrays.copyOf( array, collection.size( ) + array.length );
  347.         j= array.length;
  348.         }

  349.         for ( String strItem : collection )
  350.         {
  351.             newArray [j++] = strItem;
  352.         }

  353.         return newArray;
  354.     }

  355.     /**
  356.      * Set elements of a collection in array
  357.      *
  358.      * @param collection
  359.      *            the collection containing the elements to add
  360.      * @return An array
  361.      */
  362.     private String [ ] getArray( Collection<String> collection )
  363.     {
  364.         String [ ] newArray = new String [ collection.size( )];

  365.         int j = 0;

  366.         for ( String strItem : collection )
  367.         {
  368.             newArray [j++] = strItem;
  369.         }

  370.         return newArray;
  371.     }

  372.     // //////////////////////////////////////////////////////////////////////////
  373.     // Authentication infos

  374.     /**
  375.      * Defines the authentification service that had authentified the user
  376.      *
  377.      * @param strAuthenticationService
  378.      *            The authentification service
  379.      */
  380.     public void setAuthenticationService( String strAuthenticationService )
  381.     {
  382.         _strAuthenticationService = strAuthenticationService;
  383.     }

  384.     /**
  385.      * Returns the authentification service that had authentified the user
  386.      *
  387.      * @return the authentification service that had authentified the user
  388.      */
  389.     public String getAuthenticationService( )
  390.     {
  391.         return _strAuthenticationService;
  392.     }

  393.     /**
  394.      * Defines the authentification type that had authentified the user
  395.      *
  396.      * @param strAuthenticationType
  397.      *            The authentification type
  398.      */
  399.     public void setAuthenticationType( String strAuthenticationType )
  400.     {
  401.         _strAuthenticationType = strAuthenticationType;
  402.     }

  403.     /**
  404.      * Returns the authentification type that had authentified the user
  405.      *
  406.      * @return the authentification type that had authentified the user
  407.      */
  408.     public String getAuthenticationType( )
  409.     {
  410.         return _strAuthenticationType;
  411.     }

  412.     /**
  413.      * "Getter method" for {@link #_luteceAuthenticationService}
  414.      *
  415.      * @return value of {@link #_luteceAuthenticationService}
  416.      */
  417.     public LuteceAuthentication getLuteceAuthenticationService( )
  418.     {
  419.         return _luteceAuthenticationService;
  420.     }

  421.     /**
  422.      * "Setter method" for {@link #_luteceAuthenticationService}.
  423.      *
  424.      * @param authenticationService
  425.      *            new value of {@link #_luteceAuthenticationService}
  426.      */
  427.     public void setLuteceAuthenticationService( LuteceAuthentication authenticationService )
  428.     {
  429.         _luteceAuthenticationService = authenticationService;
  430.     }

  431.     /**
  432.      * {@inheritDoc}
  433.      */
  434.     @Override
  435.     public Object clone( ) throws CloneNotSupportedException
  436.     {
  437.         return super.clone( );
  438.     }

  439.     /**
  440.      * Get the users email
  441.      *
  442.      * @return The email
  443.      */
  444.     @Override
  445.     public String getEmail( )
  446.     {
  447.         return null;
  448.     }

  449.     /**
  450.      * {@inheritDoc}
  451.      */
  452.     @Override
  453.     public String getAccessCode( )
  454.     {
  455.         return getName( );
  456.     }

  457.     /**
  458.      * {@inheritDoc}
  459.      */
  460.     @Override
  461.     public String getLastName( )
  462.     {
  463.         return this.getUserInfo( LuteceUser.NAME_FAMILY );
  464.     }

  465.     /**
  466.      * {@inheritDoc}
  467.      */
  468.     public @Override String getFirstName( )
  469.     {
  470.         return this.getUserInfo( LuteceUser.NAME_GIVEN );

  471.     }

  472.     /**
  473.      * {@inheritDoc}
  474.      */
  475.     @Override
  476.     public List<String> getUserWorkgroups( )
  477.     {
  478.         return _workgroups;
  479.     }

  480.     /**
  481.      * {@inheritDoc}
  482.      */
  483.     @Override
  484.     public String getRealm( )
  485.     {
  486.         return USER_REALM;
  487.     }
  488. }