View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.participatorybudget.web.includes;
35  
36  import java.util.Map;
37  import java.util.Random;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.plugins.participatorybudget.service.BudgetIncludeService;
42  import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
43  import fr.paris.lutece.plugins.participatorybudget.service.vote.MyVoteService;
44  import fr.paris.lutece.plugins.participatorybudget.util.BudgetUtils;
45  import fr.paris.lutece.plugins.participatorybudget.web.MyInfosXPage;
46  import fr.paris.lutece.portal.service.content.PageData;
47  import fr.paris.lutece.portal.service.includes.PageInclude;
48  import fr.paris.lutece.portal.service.security.LuteceUser;
49  import fr.paris.lutece.portal.service.security.SecurityService;
50  import fr.paris.lutece.portal.service.security.SecurityTokenService;
51  import fr.paris.lutece.portal.service.security.UserNotSignedException;
52  import fr.paris.lutece.portal.service.spring.SpringContextService;
53  import fr.paris.lutece.portal.service.template.AppTemplateService;
54  import fr.paris.lutece.portal.service.util.AppLogService;
55  import fr.paris.lutece.portal.service.util.AppPropertiesService;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  
58  /**
59   * Page include to add the
60   */
61  public class HeaderInclude implements PageInclude
62  {
63      // Marks
64      public static final String MARK_HEADER_INCLUDE = "header_include";
65      public static final String MARK_HEADER_CONNEXION = "header_connexion";
66      public static final String MARK_USER = "user";
67      public static final String MARK_RANDOM = "RANDOM";
68      private static final String MARK_URL_DO_LOGIN = "url_dologin";
69      private static final String MARK_URL_DO_LOGOUT = "url_dologout";
70      private static final String MARK_URL_MONCOMPTE = "url_moncompte";
71      private static final String MARK_CAMPAIGN_SERVICE = "campaignService";
72  
73      // TODO : delete following mark
74      public static final String MARK_USER_ARRONDISSEMENT_VOTE = "user_arr_vote";
75  
76      // Properties
77      private static final String PROPERTY_URL_MONCOMPTE = "participatorybudget.include.url.moncompte";
78  
79      // Services
80      private MyVoteService _myVoteService = SpringContextService.getBean( MyVoteService.BEAN_NAME );
81  
82      // Templates
83      private static final String TEMPLATE_CONNEXION = "skin/plugins/mylutece/includes/user_login_title.html";
84  
85      private static final Random _random = new Random( );
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
92      {
93          if ( request != null )
94          {
95              rootModel.put( MARK_HEADER_INCLUDE, BudgetIncludeService.getHeaderTemplate( request, _myVoteService ) );
96              rootModel.put( MARK_URL_DO_LOGIN, SecurityService.getInstance( ).getDoLoginUrl( ) );
97              rootModel.put( MARK_URL_DO_LOGOUT, SecurityService.getInstance( ).getDoLogoutUrl( ) );
98  
99              LuteceUser user = null;
100             try
101             {
102                 user = SecurityService.getInstance( ).getRemoteUser( request );
103                 if ( user != null )
104                 {
105                     rootModel.put( MARK_USER_ARRONDISSEMENT_VOTE, BudgetUtils.getArrondissement( user.getName( ) ) );
106                     rootModel.put( BudgetUtils.MARK_VOTE_VALIDATED, _myVoteService.isUserVoteValidated( user.getName( ) ) );
107                 }
108             }
109             catch( UserNotSignedException e )
110             {
111                 AppLogService.error( e.getMessage( ) );
112             }
113 
114             rootModel.put( MARK_USER, user );
115 
116             rootModel.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, MyInfosXPage.TOKEN_DO_CREATE_MY_INFOS ) );
117 
118             rootModel.put( BudgetUtils.MARK_CAMPAIGN_SERVICE, CampaignService.getInstance( ) );
119 
120             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CONNEXION, request.getLocale( ), rootModel );
121 
122             rootModel.put( MARK_USER, user );
123             rootModel.put( MARK_HEADER_CONNEXION, template.getHtml( ) );
124             rootModel.put( MARK_RANDOM, Math.abs( _random.nextLong( ) ) );
125             rootModel.put( MARK_URL_MONCOMPTE, AppPropertiesService.getProperty( PROPERTY_URL_MONCOMPTE ) );
126             rootModel.put( MARK_CAMPAIGN_SERVICE, CampaignService.getInstance( ) );
127         }
128     }
129 }