1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.rbac;
35
36 import fr.paris.lutece.api.user.User;
37 import fr.paris.lutece.api.user.UserRole;
38 import fr.paris.lutece.portal.business.rbac.RBAC;
39 import fr.paris.lutece.portal.business.rbac.RBACHome;
40 import fr.paris.lutece.portal.business.user.AdminUser;
41 import fr.paris.lutece.util.ReferenceItem;
42 import fr.paris.lutece.util.ReferenceList;
43
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.HashMap;
48 import java.util.Map;
49 import java.util.Set;
50 import java.util.stream.Collectors;
51
52
53
54
55 public final class RBACService
56 {
57
58
59
60
61 private static final class RBACReferenceItem implements RBACResource
62 {
63 private final String _strResourceType;
64 private final ReferenceItem _item;
65
66
67
68
69
70
71
72
73
74 public RBACReferenceItem( String strResourceType, ReferenceItem item )
75 {
76 _strResourceType = strResourceType;
77 _item = item;
78 }
79
80 @Override
81 public String getResourceTypeCode( )
82 {
83 return _strResourceType;
84 }
85
86 @Override
87 public String getResourceId( )
88 {
89 return _item.getCode( );
90 }
91
92
93
94
95
96
97 public ReferenceItem getItem( )
98 {
99 return _item;
100 }
101
102 }
103
104
105
106
107 private RBACService( )
108 {
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public static boolean isAuthorized( String strResourceTypeCode, String strResourceId, String strPermission, User user )
125 {
126
127 Collection<String> colRoles = RBACHome.findRoleKeys( strResourceTypeCode, strResourceId, strPermission );
128
129 for ( String strRole : colRoles )
130 {
131 if ( isUserInRole( user, strRole ) )
132 {
133 return true;
134 }
135 }
136
137 return false;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Deprecated
155 public static boolean isAuthorized( String strResourceTypeCode, String strResourceId, String strPermission, AdminUser user )
156 {
157 return isAuthorized( strResourceTypeCode, strResourceId, strPermission, (User) user );
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public static boolean isAuthorized( RBACResource resource, String strPermission, User user )
172 {
173 boolean bAuthorized = false;
174
175 if ( resource != null )
176 {
177 bAuthorized = isAuthorized( resource.getResourceTypeCode( ), resource.getResourceId( ), strPermission, user );
178 }
179
180 return bAuthorized;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195 @Deprecated
196 public static boolean isAuthorized( RBACResource resource, String strPermission, AdminUser user )
197 {
198 return isAuthorized( resource, strPermission, (User) user );
199 }
200
201
202
203
204
205
206
207
208
209
210 public static boolean isUserInRole( User user, String strRole )
211 {
212 Map<String, UserRole> userRoles = user.getUserRoles( );
213
214 return userRoles.containsKey( strRole );
215 }
216
217
218
219
220
221
222
223
224
225
226
227 @Deprecated
228 public static boolean isUserInRole( AdminUser user, String strRole )
229 {
230 return isUserInRole( (User) user, strRole );
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 public static <E extends RBACResource> Collection<E> getAuthorizedCollection( Collection<E> collection, String strPermission, User user )
247 {
248 if ( user == null )
249 {
250 return Collections.emptyList( );
251 }
252 Map<String, Collection<RBAC>> rbacsByResourceType = new HashMap<>( );
253 RBACHome.findByPermissionAndRoles( strPermission, user.getUserRoles( ).keySet( ) ).stream( ).forEach( rbac -> {
254 rbacsByResourceType.computeIfAbsent( rbac.getResourceTypeKey( ), t -> new ArrayList<>( ) ).add( rbac );
255 } );
256 return collection.stream( )
257 .filter( resource -> rbacsByResourceType
258 .getOrDefault( resource.getResourceTypeCode( ), Collections.emptyList( ) ).stream( )
259 .anyMatch( rbac -> RBAC.WILDCARD_RESOURCES_ID.equals( rbac.getResourceId( ) )
260 || resource.getResourceId( ).equals( rbac.getResourceId( ) ) ) )
261 .collect( Collectors.toList( ) );
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 @Deprecated
279 public static <E extends RBACResource> Collection<E> getAuthorizedCollection( Collection<E> collection, String strPermission, AdminUser user )
280 {
281 return getAuthorizedCollection( collection, strPermission, (User) user );
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 public static ReferenceLists/lutece/util/ReferenceList.html#ReferenceList">ReferenceList getAuthorizedReferenceList( ReferenceList listResources, String strResourceType, String strPermission, User user )
298 {
299 return getAuthorizedCollection( listResources.stream( )
300 .map( item -> new RBACReferenceItem( strResourceType, item ) ).collect( Collectors.toList( ) ),
301 strPermission, user ).stream( ).map( RBACReferenceItem::getItem )
302 .collect( Collectors.toCollection( ReferenceList::new ) );
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319 @Deprecated
320 public static ReferenceLists/lutece/util/ReferenceList.html#ReferenceList">ReferenceList getAuthorizedReferenceList( ReferenceList listResources, String strResourceType, String strPermission, AdminUser user )
321 {
322 return getAuthorizedReferenceList( listResources, strResourceType, strPermission, (User) user );
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public static <E extends RBACAction> Collection<E> getAuthorizedActionsCollection( Collection<E> collection, RBACResource resource, User user )
339 {
340 if ( collection.isEmpty( ) )
341 {
342 return collection;
343 }
344 Set<String> permissions = RBACHome
345 .findByPermissionsAndRoles(
346 collection.stream( ).map( RBACAction::getPermission ).collect( Collectors.toSet( ) ),
347 user.getUserRoles( ).keySet( ) )
348 .stream( ).filter( rbac -> resource.getResourceTypeCode( ).equals( rbac.getResourceTypeKey( ) ) )
349 .filter( rbac -> RBAC.WILDCARD_RESOURCES_ID.equals( rbac.getResourceId( ) )
350 || resource.getResourceId( ).equals( rbac.getResourceId( ) ) )
351 .map( RBAC::getPermissionKey ).collect( Collectors.toSet( ) );
352 return collection.stream( ).filter( action -> permissions.contains( action.getPermission( ) )
353 || permissions.contains( RBAC.WILDCARD_PERMISSIONS_KEY ) ).collect( Collectors.toList( ) );
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370 @Deprecated
371 public static <E extends RBACAction> Collection<E> getAuthorizedActionsCollection( Collection<E> collection, RBACResource resource, AdminUser user )
372 {
373 return getAuthorizedActionsCollection( collection, resource, (User) user );
374 }
375
376 }