View Javadoc
1   /*
2    * Copyright (c) 2002-2016, Mairie de 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.mydashboard.modules.identity.web;
35  
36  import fr.paris.lutece.plugins.mydashboard.modules.identity.util.Constants;
37  import fr.paris.lutece.plugins.mydashboard.modules.identity.util.DashboardIdentityUtils;
38  import java.util.HashMap;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import fr.paris.lutece.plugins.avatar.service.AvatarService;
45  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityNotFoundException;
46  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityDto;
47  import fr.paris.lutece.plugins.identitystore.web.service.IdentityService;
48  import fr.paris.lutece.plugins.mydashboard.modules.identity.business.DashboardIdentity;
49  import fr.paris.lutece.plugins.mydashboard.service.MyDashboardComponent;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.security.LuteceUser;
52  import fr.paris.lutece.portal.service.security.SecurityService;
53  import fr.paris.lutece.portal.service.security.UserNotSignedException;
54  import fr.paris.lutece.portal.service.spring.SpringContextService;
55  import fr.paris.lutece.portal.service.template.AppTemplateService;
56  import fr.paris.lutece.portal.service.util.AppLogService;
57  import fr.paris.lutece.portal.service.util.AppPropertiesService;
58  import fr.paris.lutece.portal.web.l10n.LocaleService;
59  import fr.paris.lutece.util.html.HtmlTemplate;
60  
61  /**
62   * MyDashboardAlertsComponent
63   */
64  public class MyDashboardIdentityComponent extends MyDashboardComponent
65  {
66      private static final String DASHBOARD_APP_CODE = AppPropertiesService.getProperty( Constants.PROPERTY_APPLICATION_CODE );
67      private static final String BEAN_IDENTITYSTORE_SERVICE = "mydashboard-identity.identitystore.service";
68      private static final String DASHBOARD_COMPONENT_ID = "mydashboard-identity.identityComponent";
69      private static final String MESSAGE_DASHBOARD_COMPONENT_DESCRIPTION = "module.mydashboard.identity.component.identity.description";
70      private static final String TEMPLATE_DASHBOARD_COMPONENT = "skin/plugins/mydashboard/modules/identity/identity_component.html";
71      private static final String MARK_AVATAR_URL = "avatar_url";
72      private static final String MARK_IDENTITY = "identity";
73  
74      private IdentityService _identityService;
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public String getDashboardData( HttpServletRequest request )
81      {
82  
83          if ( _identityService == null )
84          {
85              _identityService = SpringContextService.getBean( BEAN_IDENTITYSTORE_SERVICE );
86          }
87  
88          Map<String, Object> model = new HashMap<String, Object>( );
89  
90          LuteceUser luteceUser = SecurityService.isAuthenticationEnable( ) ? SecurityService.getInstance( ).getRegisteredUser( request ) : null;
91  
92          if ( luteceUser != null )
93          {
94  
95              model.put( MARK_AVATAR_URL, getAvatarUrl( request ) );
96              DashboardIdentity dashboardIdentity = DashboardIdentityUtils.getInstance( ).convertToDashboardIdentity( getIdentityDto( luteceUser.getName( ) ) );
97              model.put( MARK_IDENTITY, dashboardIdentity );
98              HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD_COMPONENT, LocaleService.getDefault( ), model );
99  
100             return template.getHtml( );
101         }
102         return "";
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String getComponentId( )
110     {
111         return DASHBOARD_COMPONENT_ID;
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String getComponentDescription( Locale locale )
119     {
120         return I18nService.getLocalizedString( MESSAGE_DASHBOARD_COMPONENT_DESCRIPTION, locale );
121     }
122 
123     /**
124      * Return the avatar URL
125      * 
126      * @param request
127      *            The HTTP request
128      * @return The URL
129      */
130     private String getAvatarUrl( HttpServletRequest request )
131     {
132         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
133         return AvatarService.getAvatarUrl( user.getEmail( ) );
134     }
135 
136     /**
137      * return IdentityDto from strConnectionId
138      * 
139      * @param strConnectionId
140      *            user connection id
141      * @return IdentityDto
142      * @throws UserNotSignedException
143      */
144     private IdentityDto getIdentityDto( String strConnectionId )
145     {
146         IdentityDto identityDto = null;
147 
148         try
149         {
150             identityDto = _identityService.getIdentityByConnectionId( strConnectionId, DASHBOARD_APP_CODE );
151         }
152         catch( IdentityNotFoundException infe )
153         {
154             identityDto = new IdentityDto( );
155             identityDto.setConnectionId( strConnectionId );
156             AppLogService.error( "Identity Not Found for guig:" + strConnectionId, infe );
157         }
158 
159         return identityDto;
160     }
161 
162 }