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.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   * MyLutecePortlet
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       * Constructor
76       */
77      public MyLutecePortlet( )
78      {
79      }
80  
81      /**
82       * Returns the Xml code of the MyLutece portlet without XML heading
83       *
84       * @param request
85       *            The HTTP Servlet request
86       * @return the Xml code of the MyLutece portlet content
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      * Returns the Xml code of the MyLutece portlet with XML heading
196      *
197      * @param request
198      *            The HTTP servlet request
199      * @return the Xml code of the MyLutece portlet
200      */
201     public String getXmlDocument( HttpServletRequest request )
202     {
203         return XmlUtil.getXmlHeader( ) + getXml( request );
204     }
205 
206     /**
207      * Update portlet's data
208      */
209     public void update( )
210     {
211         MyLutecePortletHome.getInstance( ).update( this );
212     }
213 
214     /**
215      * Remove of this portlet
216      */
217     public void remove( )
218     {
219         MyLutecePortletHome.getInstance( ).remove( this );
220     }
221 }