View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.crm.modules.mylutece.web;
35  
36  import fr.paris.lutece.plugins.crm.business.user.CRMUser;
37  import fr.paris.lutece.plugins.crm.modules.mylutece.service.MyLuteceUserManager;
38  import fr.paris.lutece.plugins.crm.service.security.CRMUserAnonymizationService;
39  import fr.paris.lutece.plugins.crm.service.security.IAnonymizationService;
40  import fr.paris.lutece.plugins.crm.service.user.CRMUserAttributesService;
41  import fr.paris.lutece.plugins.crm.service.user.CRMUserService;
42  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.service.template.AppTemplateService;
47  import fr.paris.lutece.portal.service.util.AppPathService;
48  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
49  import fr.paris.lutece.portal.web.constants.Messages;
50  import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
51  import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import org.apache.commons.lang.StringUtils;
56  
57  import java.util.HashMap;
58  import java.util.Map;
59  
60  import javax.servlet.http.HttpServletRequest;
61  import javax.servlet.http.HttpServletResponse;
62  
63  
64  /**
65   *
66   * CRMMyluteceJspBean
67   *
68   */
69  public class CRMMyluteceJspBean extends PluginAdminPageJspBean
70  {
71      /** The Constant RIGHT_MANAGE_MYLUTECE_USERS. */
72      public static final String RIGHT_MANAGE_MYLUTECE_USERS = "CRM_MYLUTECE_MANAGEMENT";
73  
74      // PARAMETERS
75      private static final String PARAMETER_ID_CRM_USER = "idCRMUser";
76      private static final String PARAMETER_SESSION = "session";
77  
78      // MARKS
79      private static final String MARK_LIST_ATTRIBUTE_KEYS = "listAttributeKeys";
80  
81      // PROPERTIES
82      private static final String PROPERTY_MANAGE_CRM_USERS_PAGE_TITLE = "module.crm.mylutece.manage_crm_users.pageTitle";
83  
84      // MESSAGES
85      private static final String MESSAGE_CONFIRM_REMOVE_CRM_USER = "module.crm.mylutece.message.confirm.removeCRMUser";
86      private static final String MESSAGE_CONFIRM_ANONYMIZE = "module.crm.mylutece.message.confirm.anonymize";
87      private static final String MESSAGE_CONFIRM_REINIT_PASSWORD = "module.crm.mylutece.message.confirm.reinitPassword";
88  
89      // TEMPLATES
90      private static final String TEMPLATE_MANAGE_CRM_USERS = "admin/plugins/crm/modules/mylutece/manage_crm_users.html";
91  
92      // JSP
93      private static final String JSP_URL_MANAGE_USERS = "jsp/admin/plugins/crm/modules/mylutece/ManageCRMUsers.jsp";
94      private static final String JSP_URL_REMOVE_CRM_USER = "jsp/admin/plugins/crm/modules/mylutece/DoRemoveCRMUser.jsp";
95      private static final String JSP_URL_ANONYMIZE_CRM_USER = "jsp/admin/plugins/crm/modules/mylutece/DoAnonymizeCRMUser.jsp";
96      private static final String JSP_URL_REINIT_PASSWORD = "jsp/admin/plugins/crm/modules/mylutece/DoReinitPassword.jsp";
97  
98      // VARIABLES
99      private CRMMyLuteceSearchFields _userSearchFields = new CRMMyLuteceSearchFields(  );
100     private CRMUserService _crmUserService = CRMUserService.getService(  );
101     private CRMUserAttributesService _crmUserAttributesService = CRMUserAttributesService.getService(  );
102     private IAnonymizationService _anonymizationService = SpringContextService.getBean( CRMUserAnonymizationService.BEAN_SERVICE );
103 
104     /**
105      * Gets the manage my lutece users.
106      *
107      * @param request the request
108      * @param response the response
109      * @return the manage my lutece users
110      * @throws AccessDeniedException the access denied exception
111      */
112     public IPluginActionResult getManageMyLuteceUsers( HttpServletRequest request, HttpServletResponse response )
113         throws AccessDeniedException
114     {
115         setPageTitleProperty( PROPERTY_MANAGE_CRM_USERS_PAGE_TITLE );
116 
117         Map<String, Object> model = new HashMap<String, Object>(  );
118         _userSearchFields.fillModel( getUrlManageUsers( request, true ).getUrl(  ), request, model );
119         model.put( MARK_LIST_ATTRIBUTE_KEYS, _crmUserAttributesService.getUserAttributeKeys(  ) );
120 
121         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CRM_USERS, request.getLocale(  ), model );
122 
123         IPluginActionResult result = new DefaultPluginActionResult(  );
124 
125         result.setHtmlContent( getAdminPage( template.getHtml(  ) ) );
126 
127         return result;
128     }
129 
130     /**
131      * Gets the confirm remove crm user.
132      *
133      * @param request the request
134      * @return the confirm remove crm user
135      */
136     public String getConfirmRemoveCRMUser( HttpServletRequest request )
137     {
138         String strIdCRMUser = request.getParameter( PARAMETER_ID_CRM_USER );
139 
140         if ( StringUtils.isBlank( strIdCRMUser ) || !StringUtils.isNumeric( strIdCRMUser ) )
141         {
142             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
143         }
144 
145         UrlItem url = new UrlItem( JSP_URL_REMOVE_CRM_USER );
146         url.addParameter( PARAMETER_ID_CRM_USER, strIdCRMUser );
147 
148         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CRM_USER, url.getUrl(  ),
149             AdminMessage.TYPE_CONFIRMATION );
150     }
151 
152     /**
153      * Gets the confirm anonymize crm user.
154      *
155      * @param request the request
156      * @return the confirm anonymize crm user
157      */
158     public String getConfirmAnonymizeCRMUser( HttpServletRequest request )
159     {
160         String strIdCRMUser = request.getParameter( PARAMETER_ID_CRM_USER );
161 
162         if ( StringUtils.isBlank( strIdCRMUser ) || !StringUtils.isNumeric( strIdCRMUser ) )
163         {
164             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
165         }
166 
167         UrlItem url = new UrlItem( JSP_URL_ANONYMIZE_CRM_USER );
168         url.addParameter( PARAMETER_ID_CRM_USER, strIdCRMUser );
169 
170         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_ANONYMIZE, url.getUrl(  ),
171             AdminMessage.TYPE_CONFIRMATION );
172     }
173 
174     /**
175      * Gets the confirm reinit password.
176      *
177      * @param request the request
178      * @return the confirm reinit password
179      */
180     public String getConfirmReinitPassword( HttpServletRequest request )
181     {
182         String strIdCRMUser = request.getParameter( PARAMETER_ID_CRM_USER );
183 
184         if ( StringUtils.isBlank( strIdCRMUser ) || !StringUtils.isNumeric( strIdCRMUser ) )
185         {
186             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
187         }
188 
189         UrlItem url = new UrlItem( JSP_URL_REINIT_PASSWORD );
190         url.addParameter( PARAMETER_ID_CRM_USER, strIdCRMUser );
191 
192         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REINIT_PASSWORD, url.getUrl(  ),
193             AdminMessage.TYPE_CONFIRMATION );
194     }
195 
196     /**
197      * Do remove crm user.
198      *
199      * @param request the request
200      * @return the string
201      */
202     public String doRemoveCRMUser( HttpServletRequest request )
203     {
204         String strIdCRMUser = request.getParameter( PARAMETER_ID_CRM_USER );
205 
206         if ( StringUtils.isBlank( strIdCRMUser ) || !StringUtils.isNumeric( strIdCRMUser ) )
207         {
208             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
209         }
210 
211         int nIdCRMUser = Integer.parseInt( strIdCRMUser );
212         CRMUser user = _crmUserService.findByPrimaryKey( nIdCRMUser );
213 
214         if ( user != null )
215         {
216             _crmUserService.remove( nIdCRMUser );
217             MyLuteceUserManager.doRemoveMyLuteceUser( user.getUserGuid(  ), request, getLocale(  ) );
218         }
219 
220         return getUrlManageUsers( request, true ).getUrl(  );
221     }
222 
223     /**
224      * Do anonymize crm user.
225      *
226      * @param request the request
227      * @return the string
228      */
229     public String doAnonymizeCRMUser( HttpServletRequest request )
230     {
231         String strIdCRMUser = request.getParameter( PARAMETER_ID_CRM_USER );
232 
233         if ( StringUtils.isBlank( strIdCRMUser ) || !StringUtils.isNumeric( strIdCRMUser ) )
234         {
235             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
236         }
237 
238         int nIdCRMUser = Integer.parseInt( strIdCRMUser );
239         CRMUser user = _crmUserService.findByPrimaryKey( nIdCRMUser );
240 
241         if ( user != null )
242         {
243             _anonymizationService.anonymizeUser( user.getIdCRMUser(  ), getLocale(  ) );
244             MyLuteceUserManager.doAnonymizeMyLuteceUser( user.getUserGuid(  ), request, getLocale(  ) );
245         }
246 
247         return getUrlManageUsers( request, true ).getUrl(  );
248     }
249 
250     /**
251      * Do reinit password.
252      *
253      * @param request the request
254      * @return the string
255      */
256     public String doReinitPassword( HttpServletRequest request )
257     {
258         String strIdCRMUser = request.getParameter( PARAMETER_ID_CRM_USER );
259 
260         if ( StringUtils.isBlank( strIdCRMUser ) || !StringUtils.isNumeric( strIdCRMUser ) )
261         {
262             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
263         }
264 
265         int nIdCRMUser = Integer.parseInt( strIdCRMUser );
266         CRMUser user = _crmUserService.findByPrimaryKey( nIdCRMUser );
267 
268         if ( user != null )
269         {
270             MyLuteceUserManager.doReinitPassword( user.getUserGuid(  ), request, getLocale(  ) );
271         }
272 
273         return getUrlManageUsers( request, true ).getUrl(  );
274     }
275 
276     // PRIVATE METHODS
277 
278     /**
279      * Gets the url manage users.
280      *
281      * @param request the request
282      * @param bSession the b session
283      * @return the url manage users
284      */
285     private UrlItem getUrlManageUsers( HttpServletRequest request, boolean bSession )
286     {
287         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_USERS );
288 
289         if ( bSession )
290         {
291             url.addParameter( PARAMETER_SESSION, PARAMETER_SESSION );
292         }
293 
294         return url;
295     }
296 }