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.identitystore.web;
35  
36  import java.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import fr.paris.lutece.plugins.appcenter.business.Application;
41  import fr.paris.lutece.plugins.appcenter.business.Environment;
42  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.Attribute;
43  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.AttributeHome;
44  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.IdentitystoreDatas;
45  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.IdentitystoreDemand;
46  import fr.paris.lutece.plugins.appcenter.modules.identitystore.service.IAttributesProvider;
47  import fr.paris.lutece.plugins.appcenter.modules.identitystore.service.IdentityStoreDemandService;
48  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
49  import fr.paris.lutece.plugins.appcenter.service.DemandService;
50  import fr.paris.lutece.plugins.appcenter.service.UserService;
51  import fr.paris.lutece.plugins.appcenter.web.AppCenterDemandXPage;
52  import fr.paris.lutece.plugins.appcenter.web.Constants;
53  import static fr.paris.lutece.plugins.appcenter.web.Constants.MARK_USER;
54  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
55  import fr.paris.lutece.portal.service.message.SiteMessageException;
56  import fr.paris.lutece.portal.service.security.UserNotSignedException;
57  import fr.paris.lutece.portal.service.spring.SpringContextService;
58  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
59  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
60  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
61  import fr.paris.lutece.portal.web.xpages.XPage;
62  import fr.paris.lutece.util.ReferenceList;
63  import java.util.Arrays;
64  
65  /**
66   * IdentitystoreXPage
67   */
68  @Controller( xpageName = "identitystore", pageTitleI18nKey = "appcenter.xpage.identitystore.pageTitle", pagePathI18nKey = "appcenter.xpage.identitystore.pagePathLabel" )
69  public class IdentitystoreXPage extends AppCenterDemandXPage
70  {
71      // Templates
72      private static final String TEMPLATE_MANAGE_IDENTITYSTORE = "/skin/plugins/appcenter/modules/identitystore/manage_identitystore_demand.html";
73  
74      private static final String VIEW_MANAGE_IDENTITYSTORE = "identitystore";
75      private static final String ACTION_ADD_IDENTITYSTORE_DEMAND = "addIdentitystoreDemand";
76  
77      private static final String MARK_ATTRIBUTES = "attributes";
78      private static final String MARK_ENVIRONMENT = "environment";
79      private static final String MARK_MAP_ATTRIBUTES = "mapAttributes";
80  
81      private final IAttributesProvider _attributesProviderService = SpringContextService.getBean( "appcenter.attributesProvider" );
82  
83      // Permissions
84      private static final String PERMISSION_VIEW_IDENTITYSTORE_DEMANDS = "PERMISSION_VIEW_IDENTITYSTORE_DEMANDS";
85      private static final String PERMISSION_CREATE_IDENTITYSTORE_DEMAND = "PERMISSION_CREATE_IDENTITYSTORE_DEMAND";
86  
87      /**
88       * Maganage identitystore view
89       * 
90       * @param request
91       *            The HttpServletRequest
92       * @return the view for managing identitystore
93       * @throws UserNotSignedException
94       * @throws SiteMessageException
95       */
96      @View( value = VIEW_MANAGE_IDENTITYSTORE, defaultView = true )
97      public XPage getManageApplications( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
98      {
99          try
100         {
101             checkPermission( request, PERMISSION_VIEW_IDENTITYSTORE_DEMANDS, getActiveEnvironment( request ).getPrefix( ) );
102         }
103         catch( AccessDeniedException e )
104         {
105             getUnauthorizedAccessMessage( request );
106         }
107         Map<String, Attribute> mapAttributes = AttributeHome.getAttributesMap( );
108         Map<String, Object> model = getModel( );
109         fillAppCenterCommons( model, request );
110         fillDisplayPermission( model, request, PERMISSION_CREATE_IDENTITYSTORE_DEMAND, getActiveEnvironment( request ).getPrefix( ) );
111 
112         model.put( MARK_ATTRIBUTES, _attributesProviderService.getListAttributes( ) );
113         model.put( MARK_MAP_ATTRIBUTES, mapAttributes );
114 
115         return getXPage( TEMPLATE_MANAGE_IDENTITYSTORE, request.getLocale( ), model );
116     }
117 
118     /**
119      * Action : add access demand
120      * 
121      * @param request
122      *            the HttpServletRequest
123      * @return the manage view, after processing action
124      * @throws UserNotSignedException
125      * @throws SiteMessageException
126      */
127     @Action( value = ACTION_ADD_IDENTITYSTORE_DEMAND )
128     public XPage doAddAccessDemand( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
129     {
130         try
131         {
132             checkPermission( request, PERMISSION_CREATE_IDENTITYSTORE_DEMAND, getActiveEnvironment( request ).getPrefix( ) );
133         }
134         catch( AccessDeniedException e )
135         {
136             getUnauthorizedAccessMessage( request );
137         }
138         int nId = Integer.parseInt( request.getParameter( Constants.PARAM_ID_APPLICATION ) );
139         Application application = getApplication( request );
140         IdentitystoreDemandidentitystore/business/IdentitystoreDemand.html#IdentitystoreDemand">IdentitystoreDemand identitystoreDemand = new IdentitystoreDemand( );
141         identitystoreDemand.setIdApplication( application.getId( ) );
142 
143         populate( identitystoreDemand, request );
144         // Populate the identitystore attribute rights to the demand
145         identitystoreDemand.setAttributeRights( IdentityStoreDemandService.getMapAttributeRights( request ) );
146 
147         // Check constraints
148         if ( !validateBean( identitystoreDemand, getLocale( request ) ) )
149         {
150             return redirectView( request, VIEW_MANAGE_IDENTITYSTORE );
151         }
152 
153         DemandService.saveDemand( identitystoreDemand, application );
154 
155         return redirect( request, VIEW_MANAGE_IDENTITYSTORE, Constants.PARAM_ID_APPLICATION, nId );
156     }
157 
158     @Override
159     protected String getDemandType( )
160     {
161         return IdentitystoreDemand.DEMAND_TYPE;
162     }
163 
164     @Override
165     protected Class getDemandClass( )
166     {
167         return IdentitystoreDemand.class;
168     }
169 
170     @Override
171     protected String getDatasName( )
172     {
173         return IdentitystoreDatas.DATA_SUBSET_NAME;
174     }
175 
176     @Override
177     protected Class getDatasClass( )
178     {
179         return IdentitystoreDatas.class;
180     }
181 }