View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.document.service.spaces;
35  
36  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
37  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
38  import fr.paris.lutece.plugins.document.service.DocumentPlugin;
39  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
40  import fr.paris.lutece.portal.service.rbac.Permission;
41  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
42  import fr.paris.lutece.portal.service.rbac.ResourceType;
43  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.util.ReferenceList;
46  
47  import org.apache.commons.lang3.StringUtils;
48  
49  import java.util.Locale;
50  
51  
52  /**
53   * Resource Id service for RBAC features to control access to document spaces
54   */
55  public class SpaceResourceIdService extends ResourceIdService
56  {
57      /** Permission for creating a space */
58      public static final String PERMISSION_CREATE = "CREATE";
59  
60      /** Permission for viewing a space */
61      public static final String PERMISSION_VIEW = "VIEW";
62  
63      /** Permission for deleting a space */
64      public static final String PERMISSION_DELETE = "DELETE";
65  
66      /** Permission for modifying a space */
67      public static final String PERMISSION_MODIFY = "MODIFY";
68  
69      /** Permission for moving a space */
70      public static final String PERMISSION_MOVE = "MOVE";
71      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "document.spaces.resourceType";
72      private static final String PROPERTY_LABEL_CREATE = "document.spaces.permission.label.create";
73      private static final String PROPERTY_LABEL_VIEW = "document.spaces.permission.label.view";
74      private static final String PROPERTY_LABEL_DELETE = "document.spaces.permission.label.delete";
75      private static final String PROPERTY_LABEL_MODIFY = "document.spaces.permission.label.modify";
76      private static final String PROPERTY_LABEL_MOVE = "document.spaces.permission.label.move";
77  
78      /** Creates a new instance of SpaceResourceIdService */
79      public SpaceResourceIdService(  )
80      {
81          setPluginName( DocumentPlugin.PLUGIN_NAME );
82      }
83  
84      /**
85       * Initializes the service
86       */
87      public void register(  )
88      {
89          ResourceType rt = new ResourceType(  );
90          rt.setResourceIdServiceClass( SpaceResourceIdService.class.getName(  ) );
91          rt.setPluginName( DocumentPlugin.PLUGIN_NAME );
92          rt.setResourceTypeKey( DocumentSpace.RESOURCE_TYPE );
93          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
94  
95          Permission p = new Permission(  );
96          p.setPermissionKey( PERMISSION_CREATE );
97          p.setPermissionTitleKey( PROPERTY_LABEL_CREATE );
98          rt.registerPermission( p );
99  
100         p = new Permission(  );
101         p.setPermissionKey( PERMISSION_VIEW );
102         p.setPermissionTitleKey( PROPERTY_LABEL_VIEW );
103         rt.registerPermission( p );
104 
105         p = new Permission(  );
106         p.setPermissionKey( PERMISSION_DELETE );
107         p.setPermissionTitleKey( PROPERTY_LABEL_DELETE );
108         rt.registerPermission( p );
109 
110         p = new Permission(  );
111         p.setPermissionKey( PERMISSION_MODIFY );
112         p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY );
113         rt.registerPermission( p );
114 
115         p = new Permission(  );
116         p.setPermissionKey( PERMISSION_MOVE );
117         p.setPermissionTitleKey( PROPERTY_LABEL_MOVE );
118         rt.registerPermission( p );
119 
120         ResourceTypeManager.registerResourceType( rt );
121     }
122 
123     /**
124      * Returns a list of resource ids
125      * @param locale The current locale
126      * @return A list of resource ids
127      */
128     public ReferenceList getResourceIdList( Locale locale )
129     {
130         return DocumentSpaceHome.getDocumentSpaceList(  );
131     }
132 
133     /**
134      * Returns the Title of a given resource
135      * @param strId The Id of the resource
136      * @param locale The current locale
137      * @return The Title of a given resource
138      */
139     public String getTitle( String strId, Locale locale )
140     {
141         if ( IntegerUtils.isNumeric( strId ) )
142         {
143             int nSpaceId = IntegerUtils.convert( strId );
144             DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
145 
146             if ( space != null )
147             {
148                 return space.getName(  );
149             }
150 
151             AppLogService.error( "SpaceResourceIdService - The space is not found." );
152         }
153         else
154         {
155             AppLogService.error( "SpaceResourceIdService - The id is blank or is not numeric." );
156         }
157 
158         return StringUtils.EMPTY;
159     }
160 }