1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
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
103
104 @Override
105 public Map<String, String> getIdentityInformations( String strUserName )
106 {
107 return getIdentityInformations( strUserName, ATTRIBUTE_USER_MAPPING );
108 }
109
110
111
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
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
172
173
174
175 public List<IMyLuteceExternalIdentityProviderService> getProviders()
176 {
177 return SpringContextService
178 .getBeansOfType( IMyLuteceExternalIdentityProviderService.class );
179
180 }
181
182 }