View Javadoc
1   /*
2    * Copyright (c) 2002-2019, 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.appcenter.modules.sources.web;
35  
36  import java.util.ArrayList;
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.plugins.appcenter.business.Application;
44  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourceUserDemand;
45  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesRightDatas;
46  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesRightDemand;
47  import fr.paris.lutece.plugins.appcenter.service.DemandService;
48  import fr.paris.lutece.plugins.appcenter.web.AppCenterDemandXPage;
49  import fr.paris.lutece.plugins.appcenter.web.Constants;
50  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
51  import fr.paris.lutece.portal.service.message.SiteMessageException;
52  import fr.paris.lutece.portal.service.security.UserNotSignedException;
53  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
54  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
55  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
56  import fr.paris.lutece.portal.web.xpages.XPage;
57  
58  /**
59   * SourcesRightXPage
60   */
61  @Controller( xpageName = "sources_right", pageTitleI18nKey = "module.appcenter.sources.xpage.sourcesRight.pageTitle", pagePathI18nKey = "module.appcenter.sources.xpage.sourcesRight.pagePathLabel" )
62  public class SourcesRightXPage extends AppCenterDemandXPage
63  {
64      // Templates
65      private static final String TEMPLATE_MANAGE_SOURCES_RIGHT = "/skin/plugins/appcenter/modules/sources/manage_sources_right.html";
66  
67      private static final String VIEW_MANAGE_SOURCES_RIGHTS = "sources_rights";
68      private static final String ACTION_CHANGE_USER_REPOSITORY = "changeRepositoryUser";
69      private static final String PARAM_ADD_USER_NAME = "add_user_name";
70      private static final String PARAM_ADD_USER_MAIL = "add_user_email";
71      private static final String PARAM_REMOVE_USER_NAME = "remove_user_name";
72      private static final String PARAM_REMOVE_USER_MAIL = "remove_user_email";
73  
74      private static final int NB_MAX_USER = 100;
75  
76      private static final String PERMISSION_VIEW_SOURCES_RIGHT_DEMANDS = "PERMISSION_VIEW_SOURCES_RIGHT_DEMANDS";
77      private static final String PERMISSION_MODIFY_REPO_RIGHTS= "PERMISSION_MODIFY_REPO_RIGHTS";
78  
79  
80      /**
81       * Maganage sources rights view
82       * @param request The HttpServletRequest
83       * @return the view for managing sources rights
84       * @throws UserNotSignedException
85       * @throws SiteMessageException
86       */
87      @View( value = VIEW_MANAGE_SOURCES_RIGHTS, defaultView = true )
88      public XPage getManageSourcesRights( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
89      {
90          try
91          {
92              checkPermission( request, PERMISSION_VIEW_SOURCES_RIGHT_DEMANDS, null );
93          }
94          catch ( AccessDeniedException e )
95          {
96              getUnauthorizedAccessMessage( request );
97          }
98  
99          Map<String, Object> model = getModel( );
100         fillAppCenterCommons( model, request );
101         fillDisplayPermission( model, request, PERMISSION_MODIFY_REPO_RIGHTS , null );
102 
103         return getXPage( TEMPLATE_MANAGE_SOURCES_RIGHT, request.getLocale( ), model );
104     }
105 
106     /**
107      * Action change user repository
108      * @param request The HttpServletRequest
109      * @return the manage view after processing action
110      * @throws UserNotSignedException
111      * @throws SiteMessageException
112      */
113     @Action( ACTION_CHANGE_USER_REPOSITORY )
114     public XPage doChangeUserRepository (HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
115     {
116         try
117         {
118             checkPermission( request, PERMISSION_MODIFY_REPO_RIGHTS, null);
119         }
120         catch ( AccessDeniedException e )
121         {
122             getUnauthorizedAccessMessage( request );
123         }
124         int nId = Integer.parseInt( request.getParameter(Constants.PARAM_ID_APPLICATION ) );
125         Application application = getApplication( request );
126         SourcesRightDemands/sources/business/SourcesRightDemand.html#SourcesRightDemand">SourcesRightDemand sourcesRightDemand = new SourcesRightDemand( );
127         sourcesRightDemand.setIdApplication( application.getId( ) );
128         populate( sourcesRightDemand, request );
129 
130         //Change Users Authorized
131         String strUserName = null;
132         String strMail = null;
133         SourceUserDemand sourceUserDemand;
134 
135         sourcesRightDemand.setListSourceAddUserDemand( new ArrayList<>( ) );
136         for ( int i = 0; i < NB_MAX_USER; i++ )
137         {
138             strUserName = request.getParameter( PARAM_ADD_USER_NAME+"_"+i );
139             if( !StringUtils.isEmpty( strUserName ) )
140             {
141                 strMail = request.getParameter( PARAM_ADD_USER_MAIL+"_"+i );
142                 sourceUserDemand = new SourceUserDemand( );
143                 sourceUserDemand.setUserName( strUserName );
144                 sourceUserDemand.setEmail( strMail );
145                 sourcesRightDemand.getListSourceAddUserDemand( ).add( sourceUserDemand );
146             }
147         }
148 
149         sourcesRightDemand.setListSourceRemoveUserDemand( new ArrayList<>( ) );
150         for ( int i = 0; i < NB_MAX_USER; i++ )
151         {
152             strUserName = request.getParameter( PARAM_REMOVE_USER_NAME+"_"+i );
153             if( !StringUtils.isEmpty( strUserName ) )
154             {
155                 strMail = request.getParameter( PARAM_REMOVE_USER_MAIL+"_"+i );
156                 sourceUserDemand = new SourceUserDemand( );
157                 sourceUserDemand.setUserName( strUserName );
158                 sourceUserDemand.setEmail( strMail );
159                 sourcesRightDemand.getListSourceRemoveUserDemand( ).add( sourceUserDemand );
160             }
161         }
162 
163         // Check constraints
164         if ( !validateBean( sourcesRightDemand, getLocale( request ) ) )
165         {
166             return redirectView( request, VIEW_MANAGE_SOURCES_RIGHTS );
167         }
168         DemandService.saveDemand( sourcesRightDemand, application );
169 
170         return redirect(request, VIEW_MANAGE_SOURCES_RIGHTS, Constants.PARAM_ID_APPLICATION, nId );
171     }
172 
173     @Override
174     protected String getDemandType()
175     {
176         return SourcesRightDemand.DEMAND_TYPE;
177     }
178 
179     @Override
180     protected Class getDemandClass()
181     {
182         return SourcesRightDemand.class;
183     }
184 
185     @Override
186     protected String getDatasName()
187     {
188         return SourcesRightDatas.DATA_SOURCES_RIGHT_NAME;
189     }
190 
191     @Override
192     protected Class getDatasClass()
193     {
194         return SourcesRightDatas.class;
195     }
196 }