1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.permission; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.criteria.CriteriaLookupService; |
21 | |
import org.kuali.rice.core.api.criteria.GenericQueryResults; |
22 | |
import org.kuali.rice.core.api.criteria.LookupCustomizer; |
23 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
24 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
25 | |
import org.kuali.rice.core.api.exception.RiceIllegalStateException; |
26 | |
import org.kuali.rice.core.api.membership.MemberType; |
27 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
28 | |
import org.kuali.rice.kim.api.KimConstants; |
29 | |
import org.kuali.rice.kim.api.common.assignee.Assignee; |
30 | |
import org.kuali.rice.kim.api.common.delegate.DelegateType; |
31 | |
import org.kuali.rice.kim.api.common.template.Template; |
32 | |
import org.kuali.rice.kim.api.common.template.TemplateQueryResults; |
33 | |
import org.kuali.rice.kim.api.permission.Permission; |
34 | |
import org.kuali.rice.kim.api.permission.PermissionQueryResults; |
35 | |
import org.kuali.rice.kim.api.permission.PermissionService; |
36 | |
import org.kuali.rice.kim.api.role.Role; |
37 | |
import org.kuali.rice.kim.api.role.RoleMembership; |
38 | |
import org.kuali.rice.kim.api.role.RoleService; |
39 | |
import org.kuali.rice.kim.api.type.KimType; |
40 | |
import org.kuali.rice.kim.api.type.KimTypeInfoService; |
41 | |
import org.kuali.rice.kim.framework.permission.PermissionTypeService; |
42 | |
import org.kuali.rice.kim.impl.common.attribute.AttributeTransform; |
43 | |
import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo; |
44 | |
import org.kuali.rice.kim.impl.role.RolePermissionBo; |
45 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
46 | |
import org.kuali.rice.krad.util.KRADPropertyConstants; |
47 | |
|
48 | |
import java.util.ArrayList; |
49 | |
import java.util.Collection; |
50 | |
import java.util.Collections; |
51 | |
import java.util.Comparator; |
52 | |
import java.util.HashMap; |
53 | |
import java.util.List; |
54 | |
import java.util.Map; |
55 | |
import java.util.concurrent.CopyOnWriteArrayList; |
56 | |
|
57 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
58 | |
|
59 | 0 | public class PermissionServiceImpl implements PermissionService { |
60 | |
private RoleService roleService; |
61 | |
private PermissionTypeService defaultPermissionTypeService; |
62 | |
private KimTypeInfoService kimTypeInfoService; |
63 | |
private BusinessObjectService businessObjectService; |
64 | |
private CriteriaLookupService criteriaLookupService; |
65 | |
|
66 | 0 | private final CopyOnWriteArrayList<Template> allTemplates = new CopyOnWriteArrayList<Template>(); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
protected PermissionTypeService getPermissionTypeService( PermissionTemplateBo permissionTemplate ) { |
73 | 0 | if ( permissionTemplate == null ) { |
74 | 0 | throw new IllegalArgumentException( "permissionTemplate may not be null" ); |
75 | |
} |
76 | 0 | KimType kimType = kimTypeInfoService.getKimType( permissionTemplate.getKimTypeId() ); |
77 | 0 | String serviceName = kimType.getServiceName(); |
78 | |
|
79 | 0 | if ( StringUtils.isBlank( serviceName ) ) { |
80 | 0 | return defaultPermissionTypeService; |
81 | |
} |
82 | |
try { |
83 | 0 | Object service = GlobalResourceLoader.getService(serviceName); |
84 | |
|
85 | 0 | if ( service == null ) { |
86 | 0 | throw new RuntimeException("null returned for permission type service for service name: " + serviceName); |
87 | |
} |
88 | |
|
89 | 0 | if ( !(service instanceof PermissionTypeService) ) { |
90 | 0 | throw new RuntimeException( "Service " + serviceName + " was not a PermissionTypeService. Was: " + service.getClass().getName() ); |
91 | |
} |
92 | 0 | return (PermissionTypeService)service; |
93 | 0 | } catch( Exception ex ) { |
94 | |
|
95 | 0 | throw new RuntimeException( "Error retrieving service: " + serviceName + " from the KimImplServiceLocator.", ex ); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
@Override |
100 | |
public boolean hasPermission(String principalId, String namespaceCode, |
101 | |
String permissionName, Map<String, String> permissionDetails) throws RiceIllegalArgumentException { |
102 | 0 | incomingParamCheck(principalId, "principalId"); |
103 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
104 | 0 | incomingParamCheck(permissionName, "permissionName"); |
105 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
106 | |
|
107 | 0 | return isAuthorized( principalId, namespaceCode, permissionName, permissionDetails, Collections.<String, String>emptyMap() ); |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
public boolean isAuthorized(String principalId, String namespaceCode, |
112 | |
String permissionName, Map<String, String> permissionDetails, |
113 | |
Map<String, String> qualification ) throws RiceIllegalArgumentException { |
114 | 0 | incomingParamCheck(principalId, "principalId"); |
115 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
116 | 0 | incomingParamCheck(permissionName, "permissionName"); |
117 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
118 | 0 | incomingParamCheck(qualification, "qualification"); |
119 | |
|
120 | 0 | List<String> roleIds = getRoleIdsForPermission( namespaceCode, permissionName, permissionDetails ); |
121 | 0 | if ( roleIds.isEmpty() ) { |
122 | 0 | return false; |
123 | |
} |
124 | |
|
125 | 0 | return roleService.principalHasRole( principalId, roleIds, qualification); |
126 | |
|
127 | |
} |
128 | |
@Override |
129 | |
public boolean hasPermissionByTemplateName(String principalId, String namespaceCode, |
130 | |
String permissionTemplateName, Map<String, String> permissionDetails) throws RiceIllegalArgumentException { |
131 | 0 | incomingParamCheck(principalId, "principalId"); |
132 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
133 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
134 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
135 | |
|
136 | 0 | return isAuthorizedByTemplateName( principalId, namespaceCode, permissionTemplateName, permissionDetails, Collections.<String, String>emptyMap() ); |
137 | |
} |
138 | |
@Override |
139 | |
public boolean isAuthorizedByTemplateName(String principalId, String namespaceCode, |
140 | |
String permissionTemplateName, Map<String, String> permissionDetails, |
141 | |
Map<String, String> qualification ) throws RiceIllegalArgumentException { |
142 | 0 | incomingParamCheck(principalId, "principalId"); |
143 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
144 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
145 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
146 | 0 | incomingParamCheck(qualification, "qualification"); |
147 | |
|
148 | 0 | List<String> roleIds = getRoleIdsForPermissionTemplate( namespaceCode, permissionTemplateName, permissionDetails ); |
149 | 0 | if ( roleIds.isEmpty() ) { |
150 | 0 | return false; |
151 | |
} |
152 | 0 | return roleService.principalHasRole( principalId, roleIds, qualification); |
153 | |
} |
154 | |
@Override |
155 | |
public List<Permission> getAuthorizedPermissions( String principalId, |
156 | |
String namespaceCode, String permissionName, Map<String, String> permissionDetails, |
157 | |
Map<String, String> qualification ) throws RiceIllegalArgumentException { |
158 | 0 | incomingParamCheck(principalId, "principalId"); |
159 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
160 | 0 | incomingParamCheck(permissionName, "permissionName"); |
161 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
162 | 0 | incomingParamCheck(qualification, "qualification"); |
163 | |
|
164 | |
|
165 | 0 | List<PermissionBo> permissions = getPermissionImplsByName( namespaceCode, permissionName ); |
166 | |
|
167 | 0 | List<Permission> applicablePermissions = getMatchingPermissions( permissions, permissionDetails ); |
168 | 0 | return getPermissionsForUser(principalId, applicablePermissions, qualification); |
169 | |
} |
170 | |
@Override |
171 | |
public List<Permission> getAuthorizedPermissionsByTemplateName( String principalId, String namespaceCode, |
172 | |
String permissionTemplateName, Map<String, String> permissionDetails, |
173 | |
Map<String, String> qualification ) throws RiceIllegalArgumentException { |
174 | 0 | incomingParamCheck(principalId, "principalId"); |
175 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
176 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
177 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
178 | 0 | incomingParamCheck(qualification, "qualification"); |
179 | |
|
180 | |
|
181 | 0 | List<PermissionBo> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName ); |
182 | |
|
183 | 0 | List<Permission> applicablePermissions = getMatchingPermissions( permissions, permissionDetails ); |
184 | 0 | return getPermissionsForUser(principalId, applicablePermissions, qualification); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
protected List<Permission> getPermissionsForUser( String principalId, List<Permission> permissions, |
191 | |
Map<String, String> qualification ) { |
192 | 0 | ArrayList<Permission> results = new ArrayList<Permission>(); |
193 | 0 | List<Permission> tempList = new ArrayList<Permission>(1); |
194 | 0 | for ( Permission perm : permissions ) { |
195 | 0 | tempList.clear(); |
196 | 0 | tempList.add( perm ); |
197 | 0 | List<String> roleIds = getRoleIdsForPermissions( tempList ); |
198 | |
|
199 | |
|
200 | |
|
201 | 0 | if ( roleIds != null && !roleIds.isEmpty() ) { |
202 | 0 | if ( roleService.principalHasRole( principalId, roleIds, qualification) ) { |
203 | 0 | results.add( perm ); |
204 | |
} |
205 | |
} |
206 | 0 | } |
207 | |
|
208 | 0 | return Collections.unmodifiableList(results); |
209 | |
} |
210 | |
|
211 | |
protected Map<String,PermissionTypeService> getPermissionTypeServicesByTemplateId( Collection<PermissionBo> permissions ) { |
212 | 0 | Map<String,PermissionTypeService> permissionTypeServices = new HashMap<String, PermissionTypeService>( permissions.size() ); |
213 | 0 | for ( PermissionBo perm : permissions ) { |
214 | 0 | permissionTypeServices.put(perm.getTemplateId(), getPermissionTypeService( perm.getTemplate() ) ); |
215 | |
} |
216 | 0 | return permissionTypeServices; |
217 | |
} |
218 | |
|
219 | |
@SuppressWarnings("static-access") |
220 | |
protected Map<String,List<Permission>> groupPermissionsByTemplate( Collection<PermissionBo> permissions ) { |
221 | 0 | Map<String,List<Permission>> results = new HashMap<String,List<Permission>>(); |
222 | 0 | for ( PermissionBo perm : permissions ) { |
223 | 0 | List<Permission> perms = results.get( perm.getTemplateId() ); |
224 | 0 | if ( perms == null ) { |
225 | 0 | perms = new ArrayList<Permission>(); |
226 | 0 | results.put( perm.getTemplateId(), perms ); |
227 | |
} |
228 | 0 | perms.add( PermissionBo.to(perm) ); |
229 | 0 | } |
230 | 0 | return results; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
protected List<Permission> getMatchingPermissions( List<PermissionBo> permissions, Map<String, String> permissionDetails ) { |
238 | 0 | List<Permission> applicablePermissions = new ArrayList<Permission>(); |
239 | 0 | if ( permissionDetails == null || permissionDetails.isEmpty() ) { |
240 | |
|
241 | 0 | for ( PermissionBo perm : permissions ) { |
242 | 0 | applicablePermissions.add( PermissionBo.to(perm) ); |
243 | |
} |
244 | |
} else { |
245 | |
|
246 | |
|
247 | 0 | Map<String,PermissionTypeService> permissionTypeServices = getPermissionTypeServicesByTemplateId( permissions ); |
248 | |
|
249 | 0 | Map<String,List<Permission>> permissionMap = groupPermissionsByTemplate( permissions ); |
250 | |
|
251 | |
|
252 | 0 | for ( Map.Entry<String,List<Permission>> entry : permissionMap.entrySet() ) { |
253 | 0 | PermissionTypeService permissionTypeService = permissionTypeServices.get( entry.getKey() ); |
254 | 0 | List<Permission> permissionList = entry.getValue(); |
255 | 0 | applicablePermissions.addAll( permissionTypeService.getMatchingPermissions( permissionDetails, permissionList ) ); |
256 | 0 | } |
257 | |
} |
258 | 0 | return applicablePermissions; |
259 | |
} |
260 | |
@Override |
261 | |
public List<Assignee> getPermissionAssignees( String namespaceCode, String permissionName, |
262 | |
Map<String, String> permissionDetails, Map<String, String> qualification ) throws RiceIllegalArgumentException { |
263 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
264 | 0 | incomingParamCheck(permissionName, "permissionName"); |
265 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
266 | 0 | incomingParamCheck(qualification, "qualification"); |
267 | |
|
268 | |
|
269 | 0 | List<String> roleIds = getRoleIdsForPermission( namespaceCode, permissionName, permissionDetails); |
270 | 0 | if ( roleIds.isEmpty() ) { |
271 | 0 | return Collections.emptyList(); |
272 | |
} |
273 | 0 | Collection<RoleMembership> roleMembers = roleService.getRoleMembers( roleIds,qualification ); |
274 | 0 | List<Assignee> results = new ArrayList<Assignee>(); |
275 | 0 | for ( RoleMembership rm : roleMembers ) { |
276 | 0 | List<DelegateType.Builder> delegateBuilderList = new ArrayList<DelegateType.Builder>(); |
277 | 0 | if (!rm.getDelegates().isEmpty()) { |
278 | 0 | for (DelegateType delegate : rm.getDelegates()){ |
279 | 0 | delegateBuilderList.add(DelegateType.Builder.create(delegate)); |
280 | |
} |
281 | |
} |
282 | 0 | if ( MemberType.PRINCIPAL.equals(rm.getMemberType()) ) { |
283 | 0 | results.add (Assignee.Builder.create(rm.getMemberId(), null, delegateBuilderList).build()); |
284 | 0 | } else if ( MemberType.GROUP.equals(rm.getMemberType()) ) { |
285 | 0 | results.add (Assignee.Builder.create(null, rm.getMemberId(), delegateBuilderList).build()); |
286 | |
} |
287 | 0 | } |
288 | 0 | return Collections.unmodifiableList(results); |
289 | |
} |
290 | |
|
291 | |
@Override |
292 | |
public List<Assignee> getPermissionAssigneesByTemplateName(String namespaceCode, String permissionTemplateName, |
293 | |
Map<String, String> permissionDetails, Map<String, String> qualification) throws RiceIllegalArgumentException { |
294 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
295 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
296 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
297 | 0 | incomingParamCheck(qualification, "qualification"); |
298 | |
|
299 | 0 | List<String> roleIds = getRoleIdsForPermissionTemplate( namespaceCode, permissionTemplateName, permissionDetails); |
300 | 0 | if ( roleIds.isEmpty() ) { |
301 | 0 | return Collections.emptyList(); |
302 | |
} |
303 | 0 | Collection<RoleMembership> roleMembers = roleService.getRoleMembers( roleIds,qualification); |
304 | 0 | List<Assignee> results = new ArrayList<Assignee>(); |
305 | 0 | for ( RoleMembership rm : roleMembers ) { |
306 | 0 | List<DelegateType.Builder> delegateBuilderList = new ArrayList<DelegateType.Builder>(); |
307 | 0 | if (!rm.getDelegates().isEmpty()) { |
308 | 0 | for (DelegateType delegate : rm.getDelegates()){ |
309 | 0 | delegateBuilderList.add(DelegateType.Builder.create(delegate)); |
310 | |
} |
311 | |
} |
312 | 0 | if ( MemberType.PRINCIPAL.equals(rm.getMemberType()) ) { |
313 | 0 | results.add (Assignee.Builder.create(rm.getMemberId(), null, delegateBuilderList).build()); |
314 | |
} else { |
315 | 0 | results.add (Assignee.Builder.create(null, rm.getMemberId(), delegateBuilderList).build()); |
316 | |
} |
317 | 0 | } |
318 | 0 | return Collections.unmodifiableList(results); |
319 | |
} |
320 | |
|
321 | |
@Override |
322 | |
public boolean isPermissionDefined( String namespaceCode, String permissionName, |
323 | |
Map<String, String> permissionDetails ) throws RiceIllegalArgumentException { |
324 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
325 | 0 | incomingParamCheck(permissionName, "permissionName"); |
326 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
327 | |
|
328 | |
|
329 | 0 | List<PermissionBo> permissions = getPermissionImplsByName( namespaceCode, permissionName ); |
330 | |
|
331 | 0 | return !getMatchingPermissions( permissions, permissionDetails ).isEmpty(); |
332 | |
} |
333 | |
|
334 | |
@Override |
335 | |
public boolean isPermissionDefinedByTemplateName(String namespaceCode, String permissionTemplateName, |
336 | |
Map<String, String> permissionDetails) throws RiceIllegalArgumentException { |
337 | |
|
338 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
339 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
340 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
341 | |
|
342 | |
|
343 | 0 | List<PermissionBo> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName ); |
344 | |
|
345 | 0 | return !getMatchingPermissions( permissions, permissionDetails ).isEmpty(); |
346 | |
} |
347 | |
|
348 | |
@Override |
349 | |
public List<String> getRoleIdsForPermission(String namespaceCode, String permissionName, |
350 | |
Map<String, String> permissionDetails) throws RiceIllegalArgumentException { |
351 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
352 | 0 | incomingParamCheck(permissionName, "permissionName"); |
353 | 0 | incomingParamCheck(permissionDetails, "permissionDetails"); |
354 | |
|
355 | |
|
356 | 0 | List<PermissionBo> permissions = getPermissionImplsByName(namespaceCode, permissionName); |
357 | |
|
358 | 0 | List<Permission> applicablePermissions = getMatchingPermissions(permissions, permissionDetails); |
359 | 0 | return getRoleIdsForPermissions(applicablePermissions); |
360 | |
} |
361 | |
|
362 | |
protected List<String> getRoleIdsForPermissionTemplate( String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails ) { |
363 | |
|
364 | 0 | List<PermissionBo> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName ); |
365 | |
|
366 | 0 | List<Permission> applicablePermissions = getMatchingPermissions( permissions, permissionDetails ); |
367 | 0 | return getRoleIdsForPermissions( applicablePermissions ); |
368 | |
} |
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
@Override |
375 | |
public Permission getPermission(String permissionId) throws RiceIllegalArgumentException { |
376 | 0 | incomingParamCheck(permissionId, "permissionId"); |
377 | |
|
378 | 0 | PermissionBo impl = getPermissionImpl( permissionId ); |
379 | 0 | if ( impl != null ) { |
380 | 0 | return PermissionBo.to(impl); |
381 | |
} |
382 | 0 | return null; |
383 | |
} |
384 | |
|
385 | |
@Override |
386 | |
public List<Permission> findPermsByNamespaceCodeTemplateName(String namespaceCode, |
387 | |
String permissionTemplateName) throws RiceIllegalArgumentException { |
388 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
389 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
390 | |
|
391 | 0 | List<PermissionBo> impls = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName ); |
392 | 0 | List<Permission> results = new ArrayList<Permission>(impls.size()); |
393 | 0 | for (PermissionBo impl : impls) { |
394 | 0 | results.add(PermissionBo.to(impl)); |
395 | |
} |
396 | 0 | return Collections.unmodifiableList(results); |
397 | |
} |
398 | |
|
399 | |
protected PermissionBo getPermissionImpl(String permissionId) throws RiceIllegalArgumentException { |
400 | 0 | incomingParamCheck(permissionId, "permissionId"); |
401 | |
|
402 | 0 | HashMap<String,Object> pk = new HashMap<String,Object>( 1 ); |
403 | 0 | pk.put( KimConstants.PrimaryKeyConstants.PERMISSION_ID, permissionId ); |
404 | 0 | return businessObjectService.findByPrimaryKey( PermissionBo.class, pk ); |
405 | |
} |
406 | |
|
407 | |
protected List<PermissionBo> getPermissionImplsByTemplateName( String namespaceCode, String permissionTemplateName ){ |
408 | 0 | HashMap<String,Object> pk = new HashMap<String,Object>( 3 ); |
409 | 0 | pk.put( "template.namespaceCode", namespaceCode ); |
410 | 0 | pk.put( "template.name", permissionTemplateName ); |
411 | 0 | pk.put( KRADPropertyConstants.ACTIVE, "Y" ); |
412 | 0 | return ((List<PermissionBo>) businessObjectService.findMatching( PermissionBo.class, pk )); |
413 | |
} |
414 | |
|
415 | |
protected List<PermissionBo> getPermissionImplsByName( String namespaceCode, String permissionName ) { |
416 | 0 | HashMap<String,Object> pk = new HashMap<String,Object>( 3 ); |
417 | 0 | pk.put( KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode ); |
418 | 0 | pk.put( KimConstants.UniqueKeyConstants.PERMISSION_NAME, permissionName ); |
419 | 0 | pk.put( KRADPropertyConstants.ACTIVE, "Y" ); |
420 | |
|
421 | 0 | return ((List<PermissionBo>) businessObjectService.findMatching( PermissionBo.class, pk )); |
422 | |
} |
423 | |
|
424 | |
@Override |
425 | |
public Template getPermissionTemplate(String permissionTemplateId) throws RiceIllegalArgumentException { |
426 | 0 | incomingParamCheck(permissionTemplateId, "permissionTemplateId"); |
427 | |
|
428 | 0 | PermissionTemplateBo impl = businessObjectService.findBySinglePrimaryKey( PermissionTemplateBo.class, permissionTemplateId ); |
429 | 0 | if ( impl != null ) { |
430 | 0 | return PermissionTemplateBo.to(impl); |
431 | |
} |
432 | 0 | return null; |
433 | |
} |
434 | |
|
435 | |
@Override |
436 | |
public Template findPermTemplateByNamespaceCodeAndName(String namespaceCode, |
437 | |
String permissionTemplateName) throws RiceIllegalArgumentException { |
438 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
439 | 0 | incomingParamCheck(permissionTemplateName, "permissionTemplateName"); |
440 | |
|
441 | 0 | Map<String,String> criteria = new HashMap<String,String>(2); |
442 | 0 | criteria.put( KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode ); |
443 | 0 | criteria.put( KimConstants.UniqueKeyConstants.PERMISSION_TEMPLATE_NAME, permissionTemplateName ); |
444 | 0 | PermissionTemplateBo impl = businessObjectService.findByPrimaryKey( PermissionTemplateBo.class, criteria ); |
445 | 0 | if ( impl != null ) { |
446 | 0 | return PermissionTemplateBo.to(impl); |
447 | |
} |
448 | 0 | return null; |
449 | |
} |
450 | |
|
451 | |
@Override |
452 | |
public List<Template> getAllTemplates() { |
453 | 0 | if ( allTemplates.isEmpty() ) { |
454 | 0 | Map<String,String> criteria = new HashMap<String,String>(1); |
455 | 0 | criteria.put( KRADPropertyConstants.ACTIVE, "Y" ); |
456 | 0 | List<PermissionTemplateBo> impls = (List<PermissionTemplateBo>) businessObjectService.findMatching( PermissionTemplateBo.class, criteria ); |
457 | 0 | List<Template> infos = new ArrayList<Template>( impls.size() ); |
458 | 0 | for ( PermissionTemplateBo impl : impls ) { |
459 | 0 | infos.add( PermissionTemplateBo.to(impl) ); |
460 | |
} |
461 | 0 | Collections.sort(infos, new Comparator<Template>() { |
462 | |
@Override public int compare(Template tmpl1, |
463 | |
Template tmpl2) { |
464 | |
|
465 | 0 | int result = tmpl1.getNamespaceCode().compareTo(tmpl2.getNamespaceCode()); |
466 | 0 | if ( result != 0 ) { |
467 | 0 | return result; |
468 | |
} |
469 | 0 | result = tmpl1.getName().compareTo(tmpl2.getName()); |
470 | 0 | return result; |
471 | |
} |
472 | |
}); |
473 | 0 | allTemplates.addAll(infos); |
474 | |
} |
475 | 0 | return Collections.unmodifiableList(allTemplates); |
476 | |
} |
477 | |
|
478 | |
|
479 | |
@Override |
480 | |
public Permission createPermission(Permission permission) |
481 | |
throws RiceIllegalArgumentException, RiceIllegalStateException { |
482 | 0 | incomingParamCheck(permission, "permission"); |
483 | |
|
484 | 0 | if (StringUtils.isNotBlank(permission.getId()) && getPermission(permission.getId()) != null) { |
485 | 0 | throw new RiceIllegalStateException("the permission to create already exists: " + permission); |
486 | |
} |
487 | 0 | List<PermissionAttributeBo> attrBos = Collections.emptyList(); |
488 | 0 | if (permission.getTemplate() != null) { |
489 | 0 | attrBos = KimAttributeDataBo.createFrom(PermissionAttributeBo.class, permission.getAttributes(), permission.getTemplate().getKimTypeId()); |
490 | |
} |
491 | 0 | PermissionBo bo = PermissionBo.from(permission); |
492 | 0 | if (bo.getTemplate() == null && bo.getTemplateId() != null) { |
493 | 0 | bo.setTemplate(PermissionTemplateBo.from(getPermissionTemplate(bo.getTemplateId()))); |
494 | |
} |
495 | 0 | bo.setAttributeDetails(attrBos); |
496 | 0 | return PermissionBo.to(businessObjectService.save(bo)); |
497 | |
} |
498 | |
|
499 | |
@Override |
500 | |
public Permission updatePermission(Permission permission) |
501 | |
throws RiceIllegalArgumentException, RiceIllegalStateException { |
502 | 0 | incomingParamCheck(permission, "permission"); |
503 | |
|
504 | 0 | PermissionBo oldPermission = getPermissionImpl(permission.getId()); |
505 | 0 | if (StringUtils.isBlank(permission.getId()) || oldPermission == null) { |
506 | 0 | throw new RiceIllegalStateException("the permission does not exist: " + permission); |
507 | |
} |
508 | |
|
509 | |
|
510 | |
|
511 | 0 | List<PermissionAttributeBo> oldAttrBos = oldPermission.getAttributeDetails(); |
512 | |
|
513 | 0 | Map<String, PermissionAttributeBo> oldAttrMap = new HashMap<String, PermissionAttributeBo>(); |
514 | 0 | for (PermissionAttributeBo oldAttr : oldAttrBos) { |
515 | 0 | oldAttrMap.put(oldAttr.getKimAttribute().getAttributeName(), oldAttr); |
516 | |
} |
517 | 0 | List<PermissionAttributeBo> newAttrBos = new ArrayList<PermissionAttributeBo>(); |
518 | 0 | for (String key : permission.getAttributes().keySet()) { |
519 | 0 | if (oldAttrMap.containsKey(key)) { |
520 | 0 | PermissionAttributeBo updatedAttr = oldAttrMap.get(key); |
521 | 0 | updatedAttr.setAttributeValue(permission.getAttributes().get(key)); |
522 | 0 | newAttrBos.add(updatedAttr); |
523 | 0 | } else { |
524 | 0 | newAttrBos.addAll(KimAttributeDataBo.createFrom(PermissionAttributeBo.class, Collections.singletonMap(key, permission.getAttributes().get(key)), permission.getTemplate().getKimTypeId())); |
525 | |
} |
526 | |
} |
527 | 0 | PermissionBo bo = PermissionBo.from(permission); |
528 | 0 | if (CollectionUtils.isNotEmpty(newAttrBos)) { |
529 | 0 | bo.getAttributeDetails().clear(); |
530 | 0 | bo.setAttributeDetails(newAttrBos); |
531 | |
} |
532 | 0 | if (bo.getTemplate() == null && bo.getTemplateId() != null) { |
533 | 0 | bo.setTemplate(PermissionTemplateBo.from(getPermissionTemplate(bo.getTemplateId()))); |
534 | |
} |
535 | |
|
536 | 0 | return PermissionBo.to(businessObjectService.save(bo)); |
537 | |
} |
538 | |
|
539 | |
@Override |
540 | |
public Permission findPermByNamespaceCodeAndName(String namespaceCode, String permissionName) |
541 | |
throws RiceIllegalArgumentException { |
542 | 0 | incomingParamCheck(namespaceCode, "namespaceCode"); |
543 | 0 | incomingParamCheck(permissionName, "permissionName"); |
544 | |
|
545 | 0 | PermissionBo permissionBo = getPermissionBoByName(namespaceCode, permissionName); |
546 | 0 | if (permissionBo != null) { |
547 | 0 | return PermissionBo.to(permissionBo); |
548 | |
} |
549 | 0 | return null; |
550 | |
} |
551 | |
|
552 | |
protected PermissionBo getPermissionBoByName(String namespaceCode, String permissionName) { |
553 | 0 | if (StringUtils.isBlank(namespaceCode) |
554 | |
|| StringUtils.isBlank(permissionName)) { |
555 | 0 | return null; |
556 | |
} |
557 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
558 | 0 | criteria.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode); |
559 | 0 | criteria.put(KimConstants.UniqueKeyConstants.NAME, permissionName); |
560 | 0 | criteria.put(KRADPropertyConstants.ACTIVE, "Y"); |
561 | |
|
562 | 0 | return businessObjectService.findByPrimaryKey(PermissionBo.class, criteria); |
563 | |
} |
564 | |
|
565 | |
@Override |
566 | |
public PermissionQueryResults findPermissions(final QueryByCriteria queryByCriteria) |
567 | |
throws RiceIllegalArgumentException { |
568 | 0 | incomingParamCheck(queryByCriteria, "queryByCriteria"); |
569 | |
|
570 | 0 | LookupCustomizer.Builder<PermissionBo> lc = LookupCustomizer.Builder.create(); |
571 | 0 | lc.setPredicateTransform(AttributeTransform.getInstance()); |
572 | |
|
573 | 0 | GenericQueryResults<PermissionBo> results = criteriaLookupService.lookup(PermissionBo.class, queryByCriteria, lc.build()); |
574 | |
|
575 | 0 | PermissionQueryResults.Builder builder = PermissionQueryResults.Builder.create(); |
576 | 0 | builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); |
577 | 0 | builder.setTotalRowCount(results.getTotalRowCount()); |
578 | |
|
579 | 0 | final List<Permission.Builder> ims = new ArrayList<Permission.Builder>(); |
580 | 0 | for (PermissionBo bo : results.getResults()) { |
581 | 0 | ims.add(Permission.Builder.create(bo)); |
582 | |
} |
583 | |
|
584 | 0 | builder.setResults(ims); |
585 | 0 | return builder.build(); |
586 | |
} |
587 | |
|
588 | |
@Override |
589 | |
public TemplateQueryResults findPermissionTemplates(final QueryByCriteria queryByCriteria) |
590 | |
throws RiceIllegalArgumentException { |
591 | 0 | incomingParamCheck(queryByCriteria, "queryByCriteria"); |
592 | |
|
593 | 0 | GenericQueryResults<PermissionTemplateBo> results = criteriaLookupService.lookup(PermissionTemplateBo.class, queryByCriteria); |
594 | |
|
595 | 0 | TemplateQueryResults.Builder builder = TemplateQueryResults.Builder.create(); |
596 | 0 | builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); |
597 | 0 | builder.setTotalRowCount(results.getTotalRowCount()); |
598 | |
|
599 | 0 | final List<Template.Builder> ims = new ArrayList<Template.Builder>(); |
600 | 0 | for (PermissionTemplateBo bo : results.getResults()) { |
601 | 0 | ims.add(Template.Builder.create(bo)); |
602 | |
} |
603 | |
|
604 | 0 | builder.setResults(ims); |
605 | 0 | return builder.build(); |
606 | |
} |
607 | |
|
608 | |
private List<String> getRoleIdsForPermissions( Collection<Permission> permissions ) { |
609 | 0 | if (CollectionUtils.isEmpty(permissions)) { |
610 | 0 | return Collections.emptyList(); |
611 | |
} |
612 | 0 | List<String> ids = new ArrayList<String>(); |
613 | 0 | for (Permission p : permissions) { |
614 | 0 | ids.add(p.getId()); |
615 | |
} |
616 | |
|
617 | 0 | QueryByCriteria query = QueryByCriteria.Builder.fromPredicates(equal("active", "true"), in("permissionId", ids.toArray(new String[]{}))); |
618 | |
|
619 | 0 | GenericQueryResults<RolePermissionBo> results = criteriaLookupService.lookup(RolePermissionBo.class, query); |
620 | 0 | List<String> roleIds = new ArrayList<String>(); |
621 | 0 | for (RolePermissionBo bo : results.getResults()) { |
622 | 0 | roleIds.add(bo.getRoleId()); |
623 | |
} |
624 | 0 | return Collections.unmodifiableList(roleIds); |
625 | |
} |
626 | |
|
627 | |
|
628 | |
|
629 | |
|
630 | |
|
631 | |
|
632 | |
public void setKimTypeInfoService(KimTypeInfoService kimTypeInfoService) { |
633 | 0 | this.kimTypeInfoService = kimTypeInfoService; |
634 | 0 | } |
635 | |
|
636 | |
|
637 | |
|
638 | |
|
639 | |
|
640 | |
|
641 | |
public void setDefaultPermissionTypeService(PermissionTypeService defaultPermissionTypeService) { |
642 | 0 | this.defaultPermissionTypeService = defaultPermissionTypeService; |
643 | 0 | } |
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
public void setRoleService(RoleService roleService) { |
651 | 0 | this.roleService = roleService; |
652 | 0 | } |
653 | |
|
654 | |
|
655 | |
|
656 | |
|
657 | |
|
658 | |
|
659 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
660 | 0 | this.businessObjectService = businessObjectService; |
661 | 0 | } |
662 | |
|
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) { |
669 | 0 | this.criteriaLookupService = criteriaLookupService; |
670 | 0 | } |
671 | |
|
672 | |
private void incomingParamCheck(Object object, String name) { |
673 | 0 | if (object == null) { |
674 | 0 | throw new RiceIllegalArgumentException(name + " was null"); |
675 | 0 | } else if (object instanceof String |
676 | |
&& StringUtils.isBlank((String) object)) { |
677 | 0 | throw new RiceIllegalArgumentException(name + " was blank"); |
678 | |
} |
679 | 0 | } |
680 | |
} |