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.business.portlet;
35
36 import fr.paris.lutece.plugins.mylutece.authentication.MultiLuteceAuthentication;
37 import fr.paris.lutece.portal.business.portlet.Portlet;
38 import fr.paris.lutece.portal.service.security.LuteceAuthentication;
39 import fr.paris.lutece.portal.service.security.LuteceUser;
40 import fr.paris.lutece.portal.service.security.SecurityService;
41 import fr.paris.lutece.util.xml.XmlUtil;
42
43 import org.apache.commons.lang3.ObjectUtils;
44
45 import java.util.HashMap;
46 import java.util.Map;
47
48 import javax.servlet.http.HttpServletRequest;
49
50
51
52
53 public class MyLutecePortlet extends Portlet
54 {
55 private static final String TAG_MY_LUTECE_PORTLET = "mylutece-portlet";
56 private static final String TAG_USER_NOT_SIGNED = "user-not-signed";
57 private static final String TAG_LUTECE_USER = "lutece-user";
58 private static final String TAG_LUTECE_USER_NAME = "lutece-user-name";
59 private static final String TAG_LUTECE_USER_NAME_GIVEN = "lutece-user-name-given";
60 private static final String TAG_LUTECE_USER_NAME_FAMILY = "lutece-user-name-family";
61 private static final String TAG_LUTECE_USER_NEW_ACCOUNT_URL = "lutece-user-new-account-url";
62 private static final String TAG_LUTECE_USER_VIEW_ACCOUNT_URL = "lutece-user-view-account-url";
63 private static final String TAG_LUTECE_USER_LOST_PASSWORD_URL = "lutece-user-lost-password-url";
64 private static final String TAG_LUTECE_USER_LOGOUT_URL = "lutece-user-logout-url";
65 private static final String TAG_LUTECE_USER_AUTHENTICATION_SERVICE = "lutece-user-authentication-service";
66 private static final String TAG_AUTHENTICATION_NAME = "name";
67 private static final String TAG_AUTHENTICATION_ICON_URL = "icon-url";
68 private static final String TAG_AUTHENTICATION_URL = "url";
69 private static final String TAG_AUTHENTICATION_DISPLAY_NAME = "display-name";
70 private static final String ATTRIBUTE_AUTHENTICATION_EXTERNAL = "external";
71 private static final String ATTRIBUTE_AUTHENTICATION_DELEGATED = "delegated";
72 private static final String ATTRIBUTE_AUTHENTICATION_LOGINPASSWORD_REQUIRED = "loginpassword-required";
73
74
75
76
77 public MyLutecePortlet( )
78 {
79 }
80
81
82
83
84
85
86
87
88 public String getXml( HttpServletRequest request )
89 {
90 StringBuffer sbXml = new StringBuffer( );
91
92 if ( !SecurityService.isAuthenticationEnable( ) )
93 {
94 XmlUtil.beginElement( sbXml, TAG_MY_LUTECE_PORTLET );
95 XmlUtil.endElement( sbXml, TAG_MY_LUTECE_PORTLET );
96
97 return sbXml.toString( );
98 }
99
100 LuteceUser user = ( request == null ) ? null : SecurityService.getInstance( ).getRegisteredUser( request );
101
102 XmlUtil.beginElement( sbXml, TAG_MY_LUTECE_PORTLET );
103
104 if ( user != null )
105 {
106 XmlUtil.beginElement( sbXml, TAG_LUTECE_USER );
107 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_NAME, user.getName( ) );
108 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_NAME_GIVEN, user.getUserInfo( LuteceUser.NAME_GIVEN ) );
109 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_NAME_FAMILY, user.getUserInfo( LuteceUser.NAME_FAMILY ) );
110
111 if ( user.getLuteceAuthenticationService( ) != null )
112 {
113 Map<String, Object> mapAttributes = new HashMap<String, Object>( );
114 mapAttributes.put( ATTRIBUTE_AUTHENTICATION_EXTERNAL, user.getLuteceAuthenticationService( ).isExternalAuthentication( ) );
115 mapAttributes.put( ATTRIBUTE_AUTHENTICATION_DELEGATED, user.getLuteceAuthenticationService( ).isDelegatedAuthentication( ) );
116 mapAttributes.put( ATTRIBUTE_AUTHENTICATION_LOGINPASSWORD_REQUIRED, user.getLuteceAuthenticationService( ).isExternalAuthentication( )
117 && user.getLuteceAuthenticationService( ).isDelegatedAuthentication( ) );
118 XmlUtil.beginElement( sbXml, TAG_LUTECE_USER_AUTHENTICATION_SERVICE, mapAttributes );
119 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_NAME, user.getLuteceAuthenticationService( ).getName( ) );
120 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_DISPLAY_NAME, user.getLuteceAuthenticationService( ).getAuthServiceName( ) );
121 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_ICON_URL, ObjectUtils.toString( user.getLuteceAuthenticationService( ).getIconUrl( ) ) );
122 XmlUtil.addElement( sbXml, ATTRIBUTE_AUTHENTICATION_DELEGATED,
123 Boolean.toString( user.getLuteceAuthenticationService( ).isDelegatedAuthentication( ) ) );
124 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_URL, user.getLuteceAuthenticationService( ).getDoLoginUrl( ) );
125 XmlUtil.endElement( sbXml, TAG_LUTECE_USER_AUTHENTICATION_SERVICE );
126 }
127
128 String strLogoutUrl = SecurityService.getInstance( ).getDoLogoutUrl( );
129
130 if ( strLogoutUrl != null )
131 {
132 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_LOGOUT_URL, strLogoutUrl );
133 }
134
135 String strViewAccountUrl = ( user.getLuteceAuthenticationService( ) != null ) ? user.getLuteceAuthenticationService( ).getViewAccountPageUrl( )
136 : null;
137
138 if ( strViewAccountUrl != null )
139 {
140 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_VIEW_ACCOUNT_URL, user.getLuteceAuthenticationService( ).getViewAccountPageUrl( ) );
141 }
142
143 XmlUtil.endElement( sbXml, TAG_LUTECE_USER );
144 }
145 else
146 {
147 XmlUtil.beginElement( sbXml, TAG_USER_NOT_SIGNED );
148
149 if ( SecurityService.getInstance( ).getAuthenticationService( ).isMultiAuthenticationSupported( ) )
150 {
151 LuteceAuthentication multiAuthentication = SecurityService.getInstance( ).getAuthenticationService( );
152
153 if ( multiAuthentication instanceof MultiLuteceAuthentication )
154 {
155 for ( LuteceAuthentication luteceAuthentication : ( (MultiLuteceAuthentication) multiAuthentication ).getListLuteceAuthentication( ) )
156 {
157 Map<String, Object> mapAttributes = new HashMap<String, Object>( );
158 mapAttributes.put( ATTRIBUTE_AUTHENTICATION_EXTERNAL, luteceAuthentication.isExternalAuthentication( ) );
159 mapAttributes.put( ATTRIBUTE_AUTHENTICATION_DELEGATED, luteceAuthentication.isDelegatedAuthentication( ) );
160 mapAttributes.put( ATTRIBUTE_AUTHENTICATION_LOGINPASSWORD_REQUIRED,
161 !luteceAuthentication.isExternalAuthentication( ) && !luteceAuthentication.isDelegatedAuthentication( ) );
162 XmlUtil.beginElement( sbXml, TAG_LUTECE_USER_AUTHENTICATION_SERVICE, mapAttributes );
163 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_NAME, luteceAuthentication.getName( ) );
164 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_DISPLAY_NAME, luteceAuthentication.getAuthServiceName( ) );
165 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_ICON_URL, ObjectUtils.toString( luteceAuthentication.getIconUrl( ) ) );
166 XmlUtil.addElement( sbXml, TAG_AUTHENTICATION_URL, luteceAuthentication.getDoLoginUrl( ) );
167 XmlUtil.endElement( sbXml, TAG_LUTECE_USER_AUTHENTICATION_SERVICE );
168 }
169 }
170 }
171
172 String strNewAccountUrl = SecurityService.getInstance( ).getNewAccountPageUrl( );
173
174 if ( strNewAccountUrl != null )
175 {
176 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_NEW_ACCOUNT_URL, strNewAccountUrl );
177 }
178
179 String strLostPasswordUrl = SecurityService.getInstance( ).getLostPasswordPageUrl( );
180
181 if ( strLostPasswordUrl != null )
182 {
183 XmlUtil.addElementHtml( sbXml, TAG_LUTECE_USER_LOST_PASSWORD_URL, strLostPasswordUrl );
184 }
185
186 XmlUtil.endElement( sbXml, TAG_USER_NOT_SIGNED );
187 }
188
189 XmlUtil.endElement( sbXml, TAG_MY_LUTECE_PORTLET );
190
191 return addPortletTags( sbXml );
192 }
193
194
195
196
197
198
199
200
201 public String getXmlDocument( HttpServletRequest request )
202 {
203 return XmlUtil.getXmlHeader( ) + getXml( request );
204 }
205
206
207
208
209 public void update( )
210 {
211 MyLutecePortletHome.getInstance( ).update( this );
212 }
213
214
215
216
217 public void remove( )
218 {
219 MyLutecePortletHome.getInstance( ).remove( this );
220 }
221 }