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.web.includes;
35  
36  import fr.paris.lutece.plugins.mylutece.authentication.MultiLuteceAuthentication;
37  import fr.paris.lutece.plugins.mylutece.web.MyLuteceApp;
38  import fr.paris.lutece.portal.service.content.PageData;
39  import fr.paris.lutece.portal.service.content.XPageAppService;
40  import fr.paris.lutece.portal.service.includes.PageInclude;
41  import fr.paris.lutece.portal.service.security.LuteceAuthentication;
42  import fr.paris.lutece.portal.service.security.LuteceUser;
43  import fr.paris.lutece.portal.service.security.SecurityService;
44  import fr.paris.lutece.portal.service.security.SecurityTokenService;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * This class provide user login form in a UserLoginInclude
55   *
56   */
57  public class UserLoginInclude implements PageInclude
58  {
59      private static final String TOKEN_ACTION_LOGIN = "dologin";
60      private static final String MARK_USERLOGIN = "pageinclude_userlogin";
61      private static final String MARK_USERLOGIN_TITLE = "pageinclude_userlogin_title";
62      private static final String MARK_LIST_AUTHENTICATIONS = "list_authentications";
63      private static final String MARK_URL_NEWACCOUNT = "url_new_account";
64      private static final String TEMPLATE_USER_LOGIN_TITLE = "/skin/plugins/mylutece/includes/user_login_title.html";
65      private static final String TEMPLATE_USER_LOGIN_TITLE_LOGGED = "/skin/plugins/mylutece/includes/user_login_title_logged.html";
66      private static final String TEMPLATE_USER_LOGIN_FORM = "/skin/plugins/mylutece/includes/user_login_include.html";
67      private static final String TEMPLATE_USER_LOGIN_MULTI_FORM = "/skin/plugins/mylutece/includes/user_login_multi_include.html";
68      private static final String MARK_USER = "user";
69      private static final String MARK_DO_LOGIN = "url_dologin";
70      private static final String MARK_DO_LOGOUT = "url_dologout";
71      private static final String MARK_URL_ACCOUNT = "url_account";
72      private static final String PARAMETER_XPAGE_MYLUTECE = "mylutece";
73  
74      /**
75       * Substitue specific Freemarker markers in the page template.
76       * 
77       * @param rootModel
78       *            the HashMap containing markers to substitute
79       * @param data
80       *            A PageData object containing applications data
81       * @param nMode
82       *            The current mode
83       * @param request
84       *            The HTTP request
85       */
86      public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
87      {
88          String strUserLoginTitle = "<!-- no authenticated user -->";
89          String strUserLoginForm = "<!-- no authenticated user -->";
90  
91          Map<String, Object> model = new HashMap<String, Object>( );
92  
93          if ( SecurityService.isAuthenticationEnable( ) )
94          {
95              if ( request != null )
96              {
97                  LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
98                  model.put( MARK_DO_LOGIN, SecurityService.getInstance( ).getDoLoginUrl( ) );
99                  model.put( MARK_DO_LOGOUT, SecurityService.getInstance( ).getDoLogoutUrl( ) );
100                 model.put( MARK_URL_ACCOUNT, SecurityService.getInstance( ).getViewAccountPageUrl( ) );
101                 // Add Token
102                 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TOKEN_ACTION_LOGIN ) );
103 
104                 if ( user != null )
105                 {
106                     model.put( MARK_USER, user );
107 
108                     HtmlTemplate tTitle = AppTemplateService.getTemplate( TEMPLATE_USER_LOGIN_TITLE_LOGGED, request.getLocale( ), model );
109                     strUserLoginTitle = tTitle.getHtml( );
110                 }
111                 else
112                 {
113                     String strPage = request.getParameter( XPageAppService.PARAM_XPAGE_APP );
114 
115                     if ( ( strPage == null ) || !strPage.equals( PARAMETER_XPAGE_MYLUTECE ) )
116                     {
117                         MyLuteceApp.setCurrentUrl( request );
118                     }
119 
120                     HtmlTemplate tTitle = AppTemplateService.getTemplate( TEMPLATE_USER_LOGIN_TITLE, request.getLocale( ), model );
121                     strUserLoginTitle = tTitle.getHtml( );
122                 }
123 
124                 HtmlTemplate t;
125                 model.put( MARK_URL_NEWACCOUNT, SecurityService.getInstance( ).getAuthenticationService( ).getNewAccountPageUrl( ) );
126 
127                 if ( SecurityService.getInstance( ).isMultiAuthenticationSupported( ) )
128                 {
129                     LuteceAuthentication luteceAuthentication = SecurityService.getInstance( ).getAuthenticationService( );
130 
131                     if ( luteceAuthentication instanceof MultiLuteceAuthentication )
132                     {
133                         model.put( MARK_LIST_AUTHENTICATIONS, ( (MultiLuteceAuthentication) luteceAuthentication ).getListLuteceAuthentication( ) );
134                     }
135 
136                     t = AppTemplateService.getTemplate( TEMPLATE_USER_LOGIN_MULTI_FORM, request.getLocale( ), model );
137                 }
138                 else
139                 {
140                     t = AppTemplateService.getTemplate( TEMPLATE_USER_LOGIN_FORM, request.getLocale( ), model );
141                 }
142 
143                 strUserLoginForm = t.getHtml( );
144                 rootModel.put( "LUTECE_USER", user );
145             }
146         }
147 
148         rootModel.put( MARK_USERLOGIN_TITLE, strUserLoginTitle );
149         rootModel.put( MARK_USERLOGIN, strUserLoginForm );
150 
151     }
152 }