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