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.portal.business.rbac;
35  
36  import fr.paris.lutece.portal.service.i18n.Localizable;
37  import fr.paris.lutece.portal.service.rbac.Permission;
38  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
39  import fr.paris.lutece.portal.service.rbac.ResourceType;
40  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import java.util.Locale;
45  
46  /**
47   * This class is used for rbac control configuration. A role is associated to controls (given by the permission key) on resources (identified by a resource type
48   * and a resource id). Wilcards can be used for resource ids and permission keys.
49   */
50  public class RBAC implements Localizable
51  {
52      public static final String WILDCARD_RESOURCES_ID = "*";
53      public static final String WILDCARD_PERMISSIONS_KEY = "*";
54      private int _nRBACId;
55      private String _strRoleKey;
56      private String _strResourceTypeKey;
57      private String _strResourceId;
58      private String _strPermissionKey;
59      private Locale _locale;
60  
61      /**
62       * Implements Localizable
63       * 
64       * @param locale
65       *            The current locale
66       */
67      public void setLocale( Locale locale )
68      {
69          _locale = locale;
70      }
71  
72      /**
73       * Returns the RBAC ID
74       * 
75       * @return The RBAC ID
76       */
77      public int getRBACId( )
78      {
79          return _nRBACId;
80      }
81  
82      /**
83       * Sets the RBAC ID
84       * 
85       * @param nRBACId
86       *            The RBAC Id to set
87       */
88      public void setRBACId( int nRBACId )
89      {
90          _nRBACId = nRBACId;
91      }
92  
93      /**
94       * Returns the Permission Key
95       * 
96       * @return The Permission Key
97       */
98      public String getPermissionKey( )
99      {
100         return _strPermissionKey;
101     }
102 
103     /**
104      * Sets the Permission Key
105      * 
106      * @param strPermissionKey
107      *            The Permission Key to set
108      */
109     public void setPermissionKey( String strPermissionKey )
110     {
111         _strPermissionKey = strPermissionKey;
112     }
113 
114     /**
115      * Returns the Resource Id
116      * 
117      * @return The Resource Id
118      */
119     public String getResourceId( )
120     {
121         return _strResourceId;
122     }
123 
124     /**
125      * Sets the Resource Id
126      * 
127      * @param strResourceId
128      *            The Resource Id to set
129      */
130     public void setResourceId( String strResourceId )
131     {
132         _strResourceId = strResourceId;
133     }
134 
135     /**
136      * Returns the Resource Type Key
137      * 
138      * @return The Resource Type Key.
139      */
140     public String getResourceTypeKey( )
141     {
142         return _strResourceTypeKey;
143     }
144 
145     /**
146      * Sets the Resource Type Key
147      * 
148      * @param strResourceTypeKey
149      *            The Resource Type Key to set.
150      */
151     public void setResourceTypeKey( String strResourceTypeKey )
152     {
153         _strResourceTypeKey = strResourceTypeKey;
154     }
155 
156     /**
157      * Returns the Role Key.
158      * 
159      * @return The Role Key.
160      */
161     public String getRoleKey( )
162     {
163         return _strRoleKey;
164     }
165 
166     /**
167      * Sets the Role Key
168      * 
169      * @param strRoleKey
170      *            The Role Key to set.
171      */
172     public void setRoleKey( String strRoleKey )
173     {
174         _strRoleKey = strRoleKey;
175     }
176 
177     /**
178      * Retrieve the label of the resource type from the resource type key
179      * 
180      * @return the label of the resource type
181      */
182     public String getResourceTypeLabel( )
183     {
184         ResourceType resourceType = ResourceTypeManager.getResourceType( getResourceTypeKey( ) );
185 
186         if ( resourceType != null )
187         {
188             return resourceType.getResourceTypeLabel( );
189         }
190 
191         return StringUtils.EMPTY;
192     }
193 
194     /**
195      * Retrieve the label of the resource from the resource id
196      * 
197      * @return the label of the resource
198      */
199     public String getResourceIdLabel( )
200     {
201         if ( getResourceId( ).equals( WILDCARD_RESOURCES_ID ) )
202         {
203             return WILDCARD_RESOURCES_ID;
204         }
205         else
206         {
207             ResourceType resourceType = ResourceTypeManager.getResourceType( getResourceTypeKey( ) );
208 
209             if ( resourceType != null )
210             {
211                 ResourceIdService resourceManagerService = resourceType.getResourceIdService( );
212                 String strTitle = StringUtils.EMPTY;
213 
214                 if ( resourceManagerService != null )
215                 {
216                     strTitle = resourceManagerService.getTitle( getResourceId( ), _locale );
217                 }
218                 if ( strTitle != null && StringUtils.isNotEmpty( strTitle ) )
219                 {
220                     return strTitle;
221                 }
222                 else
223                 {
224                     return getResourceId( );
225                 }
226             }
227 
228             return StringUtils.EMPTY;
229         }
230     }
231 
232     /**
233      * Retrieve the label of the permission from the permission key
234      * 
235      * @return the label of the permission
236      */
237     public String getPermissionLabel( )
238     {
239         if ( getPermissionKey( ).equals( WILDCARD_PERMISSIONS_KEY ) )
240         {
241             return WILDCARD_PERMISSIONS_KEY;
242         }
243         else
244         {
245             ResourceType resourceType = ResourceTypeManager.getResourceType( getResourceTypeKey( ) );
246             if ( resourceType != null )
247             {
248                 Permission permission = resourceType.getPermission( getPermissionKey( ) );
249                 if ( permission != null )
250                 {
251                     permission.setLocale( _locale );
252                     return permission.getPermissionTitle( );
253                 }
254             }
255         }
256         return StringUtils.EMPTY;
257     }
258 }