View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.mylutece.service;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  import java.util.Map.Entry;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.mylutece.business.LuteceUserAttributeDescription;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  
49  public class MyLuteceExternalIdentityService implements IMyLuteceExternalIdentityService
50  {
51  
52      private static final String PROPERTY_USER_MAPPING_ATTRIBUTES = "mylutece.identity.userMappingAttributes";
53      private static final String CONSTANT_LUTECE_USER_PROPERTIES_PATH = "mylutece.attribute";
54      private static Map<String, List<String>> ATTRIBUTE_USER_MAPPING;
55      private static final String BEAN_MY_LUTECE_IDENTITY_SERVICE = "mylutece.myLuteceExternalIdentityService";
56      private static volatile IMyLuteceExternalIdentityService _singleton;
57      private static final String SEPARATOR = ",";
58  
59      /**
60       * Get the instance of the MyLuteceExternalIdentityService service
61       * 
62       * @return the instance of the MyLuteceExternalIdentityService service
63       */
64      public static IMyLuteceExternalIdentityService getInstance( )
65      {
66          if ( _singleton == null )
67          {
68              synchronized( IMyLuteceExternalIdentityService.class )
69              {
70                  IMyLuteceExternalIdentityService service = SpringContextService.getBean( BEAN_MY_LUTECE_IDENTITY_SERVICE );
71                  _singleton = service;
72                  String strUserMappingAttributes = AppPropertiesService.getProperty( PROPERTY_USER_MAPPING_ATTRIBUTES );
73                  ATTRIBUTE_USER_MAPPING = new HashMap<String, List<String>>( );
74  
75                  if ( StringUtils.isNotBlank( strUserMappingAttributes ) )
76                  {
77                      String [ ] tabUserProperties = strUserMappingAttributes.split( SEPARATOR );
78                      String userProperties;
79  
80                      for ( int i = 0; i < tabUserProperties.length; i++ )
81                      {
82                          userProperties = AppPropertiesService.getProperty( CONSTANT_LUTECE_USER_PROPERTIES_PATH + "." + tabUserProperties [i] );
83  
84                          if ( StringUtils.isNotBlank( userProperties ) )
85                          {
86                              if ( !ATTRIBUTE_USER_MAPPING.containsKey( userProperties ) )
87                              {
88                                  ATTRIBUTE_USER_MAPPING.put( userProperties, new ArrayList<String>( ) );
89                              }
90                              ATTRIBUTE_USER_MAPPING.get( userProperties ).add( tabUserProperties [i] );
91  
92                          }
93                      }
94                  }
95              }
96          }
97  
98          return _singleton;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public Map<String, String> getIdentityInformations( String strUserName )
106     {
107         return getIdentityInformations( strUserName, ATTRIBUTE_USER_MAPPING );
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public Map<String, String> getIdentityInformations( String strUserName, Map<String, List<String>> attributeUserMappings )
115     {
116         Map<String, String> mapIdentityInformationsResult = new HashMap<String, String>( );
117         Map<String, String> mapIdentityInformations = null;
118         for ( IMyLuteceExternalIdentityProviderService identityProviderService : getProviders() )
119         {
120             mapIdentityInformations = identityProviderService.getIdentityInformations( strUserName );
121             break;
122         }
123         if ( mapIdentityInformations != null )
124         {
125             for ( Entry<String, String> entry : mapIdentityInformations.entrySet( ) )
126             {
127                 if ( attributeUserMappings.containsKey( entry.getKey( ) ) )
128                 {
129                     for ( String strUserInfo : attributeUserMappings.get( entry.getKey( ) ) )
130                     {
131                         mapIdentityInformationsResult.put( strUserInfo, entry.getValue( ) );
132                     }
133                 }
134             }
135 
136             mapIdentityInformationsResult.putAll( mapIdentityInformations );
137 
138         }
139         return mapIdentityInformationsResult;
140     }
141     
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public List<LuteceUserAttributeDescription> getDefaulLuteceUserAttributeDescription(Locale locale)
147     {
148     	
149     	List<LuteceUserAttributeDescription> listUserDescription=  new ArrayList<LuteceUserAttributeDescription>();
150     
151     	
152     	String strUserMappingAttributes = AppPropertiesService.getProperty( PROPERTY_USER_MAPPING_ATTRIBUTES );
153         ATTRIBUTE_USER_MAPPING = new HashMap<String, List<String>>( );
154 
155         if ( StringUtils.isNotBlank( strUserMappingAttributes ) )
156         {
157             String [ ] tabUserProperties = strUserMappingAttributes.split( SEPARATOR );
158             
159             for ( int i = 0; i < tabUserProperties.length; i++ )
160             {
161                  
162                   listUserDescription.add(new LuteceUserAttributeDescription( tabUserProperties [i],  AppPropertiesService.getProperty( CONSTANT_LUTECE_USER_PROPERTIES_PATH + "." + tabUserProperties [i] ) , ""));
163             }
164         }
165     	
166          	
167         return listUserDescription;
168     }
169     
170     /**
171 	 * Gets the providers.
172 	 *
173 	 * @return the providers
174 	 */
175 	public List<IMyLuteceExternalIdentityProviderService> getProviders()
176 	{
177 		return  SpringContextService
178                 .getBeansOfType( IMyLuteceExternalIdentityProviderService.class );
179 		
180 	}
181 
182 }