View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.accesscontrol.web;
35  
36  import java.io.IOException;
37  import java.sql.Date;
38  import java.time.LocalDate;
39  import java.util.Map;
40  import java.util.Scanner;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.fileupload.FileItem;
45  import org.apache.commons.lang3.math.NumberUtils;
46  
47  import fr.paris.lutece.plugins.accesscontrol.business.UserCodeControllerData;
48  import fr.paris.lutece.plugins.accesscontrol.business.UserCodeControllerDataHome;
49  import fr.paris.lutece.plugins.accesscontrol.service.AccessControlService;
50  import fr.paris.lutece.plugins.accesscontrol.service.IAccessControlService;
51  import fr.paris.lutece.portal.business.user.AdminUser;
52  import fr.paris.lutece.portal.service.spring.SpringContextService;
53  import fr.paris.lutece.portal.service.util.AppLogService;
54  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
55  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
56  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
57  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
58  
59  /**
60   * This class provides the user interface to manage ControllerUserCode features ( manage, create, modify, remove )
61   */
62  @Controller( controllerJsp = "ManageControllerUserCode.jsp", controllerPath = "jsp/admin/plugins/accesscontrol/", right = "USERCODES_MANAGEMENT" )
63  
64  public class UserCodeControllerJspBean extends AbstractManageAccessControlJspBean
65  {
66  
67      private static final long serialVersionUID = 5752966056141055327L;
68  
69      public static final String RIGHT_MANAGE_USER_CODES = "USERCODES_MANAGEMENT";
70  
71      // Messages
72      private static final String PROPERTY_PAGE_TITLE_MANAGE_ACCESSCONTROLS = "accesscontrol.manage_usercodes.pageTitle";
73  
74      // View
75      private static final String VIEW_MANAGE_USER_CODE = "manageUserCodeView";
76  
77      // Actions
78      private static final String ACTION_CANCEL_IMPORT = "cancelImport";
79      private static final String ACTION_DO_IMPORT = "doImport";
80  
81      // Marks
82      private static final String MARK_ACCESS_CONTROL_LIST = "access_control_list";
83  
84      private static final String PARAMETER_ACCESS_CONTROL = "access_control";
85      private static final String PARAMETER_FILE = "csv_file";
86  
87      // Templates
88      private static final String TEMPLATE_MANAGE_USER_CODE = "/admin/plugins/accesscontrol/manage_usercodes.html";
89  
90      private IAccessControlService _accessControlService = SpringContextService.getBean( AccessControlService.BEAN_NAME );
91  
92      /**
93       * Build the Manage View
94       * 
95       * @param request
96       *            The HTTP request
97       * @return The page
98       */
99      @View( value = VIEW_MANAGE_USER_CODE, defaultView = true )
100     public String getManageUserCode( HttpServletRequest request )
101     {
102         AdminUser adminUser = getUser( );
103 
104         Map<String, Object> model = getModel( );
105         model.put( MARK_ACCESS_CONTROL_LIST, _accessControlService.getAccessControlsEnabled( adminUser, getLocale( ) ) );
106 
107         return getPage( PROPERTY_PAGE_TITLE_MANAGE_ACCESSCONTROLS, TEMPLATE_MANAGE_USER_CODE, model );
108     }
109 
110     @Action( ACTION_CANCEL_IMPORT )
111     public String doCancelImport( HttpServletRequest request )
112     {
113         return redirectView( request, VIEW_MANAGE_USER_CODE );
114     }
115 
116     @Action( ACTION_DO_IMPORT )
117     public String doImport( HttpServletRequest request )
118     {
119         int accessControlId = NumberUtils.toInt( request.getParameter( PARAMETER_ACCESS_CONTROL ), -1 );
120         if ( request instanceof MultipartHttpServletRequest )
121         {
122             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
123             FileItem fileItem = multipartRequest.getFile( PARAMETER_FILE );
124             try ( Scanner scanner = new Scanner( fileItem.getInputStream( ) ) )
125             {
126                 while ( scanner.hasNextLine( ) )
127                 {
128                     String strLine = scanner.nextLine( );
129                     String [ ] strFields = strLine.split( ";" );
130 
131                     UserCodeControllerDatal/business/UserCodeControllerData.html#UserCodeControllerData">UserCodeControllerData data = new UserCodeControllerData( );
132                     data.setIdAccessControl( accessControlId );
133                     data.setUser( strFields [0] );
134                     data.setCode( strFields [1] );
135                     data.setValidityDate( Date.valueOf( LocalDate.parse( strFields [2] ) ) );
136 
137                     UserCodeControllerDataHome.remove( data.getUser( ), data.getIdAccessControl( ) );
138                     UserCodeControllerDataHome.create( data );
139                 }
140             }
141             catch( IOException e )
142             {
143                 AppLogService.error( "Error reading file" );
144             }
145         }
146 
147         return redirectView( request, VIEW_MANAGE_USER_CODE );
148     }
149 }