1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.lookup; |
17 | |
|
18 | |
import com.google.common.collect.MapMaker; |
19 | |
import org.apache.commons.beanutils.PropertyUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.kim.api.role.RoleService; |
23 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
24 | |
import org.kuali.rice.kim.bo.impl.GenericPermission; |
25 | |
import org.kuali.rice.kim.bo.impl.PermissionImpl; |
26 | |
import org.kuali.rice.kim.impl.permission.PermissionBo; |
27 | |
import org.kuali.rice.kim.impl.role.RoleBo; |
28 | |
import org.kuali.rice.kim.impl.role.RolePermissionBo; |
29 | |
import org.kuali.rice.kim.util.KimConstants; |
30 | |
import org.kuali.rice.kns.lookup.HtmlData; |
31 | |
import org.kuali.rice.krad.bo.BusinessObject; |
32 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
33 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
34 | |
import org.kuali.rice.krad.service.LookupService; |
35 | |
import org.kuali.rice.krad.util.KRADConstants; |
36 | |
import org.kuali.rice.krad.util.UrlFactory; |
37 | |
|
38 | |
import java.util.ArrayList; |
39 | |
import java.util.HashMap; |
40 | |
import java.util.List; |
41 | |
import java.util.Map; |
42 | |
import java.util.Properties; |
43 | |
import java.util.concurrent.ConcurrentMap; |
44 | |
import java.util.concurrent.TimeUnit; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class PermissionLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl { |
53 | |
|
54 | |
private static final long serialVersionUID = -3578448525862270477L; |
55 | |
|
56 | 0 | private static final Logger LOG = Logger.getLogger( PermissionLookupableHelperServiceImpl.class ); |
57 | |
|
58 | |
private static LookupService lookupService; |
59 | |
private static RoleService roleService; |
60 | |
|
61 | 0 | private static boolean genericPermissionDocumentTypeNameLoaded = false; |
62 | 0 | private static String genericPermissionDocumentTypeName = null; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
@SuppressWarnings("unchecked") |
70 | |
@Override |
71 | |
public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) { |
72 | 0 | List<HtmlData> htmlDataList = new ArrayList<HtmlData>(); |
73 | |
|
74 | 0 | businessObject = new GenericPermission( (PermissionImpl)businessObject ); |
75 | 0 | if (allowsMaintenanceEditAction(businessObject)) { |
76 | 0 | htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames)); |
77 | |
} |
78 | 0 | if (allowsMaintenanceNewOrCopyAction()) { |
79 | 0 | htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames)); |
80 | |
} |
81 | 0 | return htmlDataList; |
82 | |
} |
83 | |
|
84 | |
@SuppressWarnings("unchecked") |
85 | |
protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){ |
86 | 0 | Properties parameters = new Properties(); |
87 | 0 | parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall); |
88 | 0 | parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName()); |
89 | 0 | parameters.put(KRADConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.PERMISSION_ID); |
90 | 0 | parameters.put(KRADConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.PERMISSION_ID); |
91 | 0 | if (StringUtils.isNotBlank(getReturnLocation())) { |
92 | 0 | parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation()); |
93 | |
} |
94 | 0 | parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames)); |
95 | 0 | return UrlFactory.parameterizeUrl(KRADConstants.MAINTENANCE_ACTION, parameters); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
@Override |
104 | |
protected String getMaintenanceDocumentTypeName() { |
105 | 0 | if ( !genericPermissionDocumentTypeNameLoaded ) { |
106 | 0 | genericPermissionDocumentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(GenericPermission.class); |
107 | 0 | genericPermissionDocumentTypeNameLoaded = true; |
108 | |
} |
109 | 0 | return genericPermissionDocumentTypeName; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
@Override |
116 | |
protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) { |
117 | 0 | Map<String, String> permissionSearchCriteria = buildSearchCriteria(searchCriteria); |
118 | 0 | Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria); |
119 | 0 | boolean permissionCriteriaEmpty = permissionSearchCriteria==null || permissionSearchCriteria.isEmpty(); |
120 | 0 | boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty(); |
121 | |
|
122 | 0 | List<PermissionBo> permissionSearchResultsCopy = new CollectionIncomplete<PermissionBo>(new ArrayList<PermissionBo>(), new Long(0)); |
123 | 0 | if(!permissionCriteriaEmpty && !roleCriteriaEmpty){ |
124 | 0 | permissionSearchResultsCopy = getCombinedSearchResults(permissionSearchCriteria, roleSearchCriteria, unbounded); |
125 | 0 | } else if(permissionCriteriaEmpty && !roleCriteriaEmpty){ |
126 | 0 | permissionSearchResultsCopy = getPermissionsWithRoleSearchCriteria(roleSearchCriteria, unbounded); |
127 | 0 | } else if(!permissionCriteriaEmpty && roleCriteriaEmpty){ |
128 | 0 | permissionSearchResultsCopy = getPermissionsWithPermissionSearchCriteria(permissionSearchCriteria, unbounded); |
129 | 0 | } else if(permissionCriteriaEmpty && roleCriteriaEmpty){ |
130 | 0 | return getAllPermissions(unbounded); |
131 | |
} |
132 | 0 | return permissionSearchResultsCopy; |
133 | |
} |
134 | |
|
135 | |
private List<PermissionBo> getAllPermissions(boolean unbounded){ |
136 | 0 | List<PermissionBo> permissions = searchPermissions(new HashMap<String, String>(), unbounded); |
137 | 0 | for(PermissionBo permission: permissions) { |
138 | 0 | populateAssignedToRoles(permission); |
139 | |
} |
140 | 0 | return permissions; |
141 | |
} |
142 | |
|
143 | |
private List<PermissionBo> getCombinedSearchResults( |
144 | |
Map<String, String> permissionSearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){ |
145 | 0 | List<PermissionBo> permissionSearchResults = searchPermissions(permissionSearchCriteria, unbounded); |
146 | 0 | List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded); |
147 | 0 | List<PermissionBo> permissionsForRoleSearchResults = getPermissionsForRoleSearchResults(roleSearchResults, unbounded); |
148 | 0 | List<PermissionBo> matchedPermissions = new CollectionIncomplete<PermissionBo>( |
149 | |
new ArrayList<PermissionBo>(), getActualSizeIfTruncated(permissionsForRoleSearchResults)); |
150 | 0 | if((permissionSearchResults!=null && !permissionSearchResults.isEmpty()) && |
151 | |
(permissionsForRoleSearchResults!=null && !permissionsForRoleSearchResults.isEmpty())){ |
152 | 0 | for(PermissionBo permission: permissionSearchResults){ |
153 | 0 | for(PermissionBo permissionFromRoleSearch: permissionsForRoleSearchResults){ |
154 | 0 | if(permissionFromRoleSearch.getId().equals(permission.getId())) { |
155 | 0 | matchedPermissions.add(permissionFromRoleSearch); |
156 | |
} |
157 | |
} |
158 | |
} |
159 | |
} |
160 | 0 | return matchedPermissions; |
161 | |
} |
162 | |
|
163 | |
@SuppressWarnings("unchecked") |
164 | |
private List<PermissionBo> searchPermissions(Map<String, String> permissionSearchCriteria, boolean unbounded){ |
165 | 0 | return getPermissionsSearchResultsCopy(new ArrayList<PermissionBo>(getLookupService().findCollectionBySearchHelper( |
166 | |
PermissionBo.class, permissionSearchCriteria, unbounded))); |
167 | |
|
168 | |
} |
169 | |
|
170 | |
private List<PermissionBo> getPermissionsWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){ |
171 | 0 | return getPermissionsForRoleSearchResults(searchRoles(roleSearchCriteria, unbounded), unbounded); |
172 | |
} |
173 | |
|
174 | |
private List<PermissionBo> getPermissionsForRoleSearchResults(List<RoleBo> roleSearchResults, boolean unbounded){ |
175 | 0 | Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults); |
176 | 0 | List<PermissionBo> permissions = new ArrayList<PermissionBo>(); |
177 | |
List<PermissionBo> tempPermissions; |
178 | 0 | List<String> collectedPermissionIds = new ArrayList<String>(); |
179 | |
Map<String, String> permissionCriteria; |
180 | |
|
181 | 0 | for(RoleBo roleImpl: roleSearchResults){ |
182 | 0 | permissionCriteria = new HashMap<String, String>(); |
183 | 0 | permissionCriteria.put("rolePermissions.roleId", roleImpl.getId()); |
184 | 0 | tempPermissions = searchPermissions(permissionCriteria, unbounded); |
185 | 0 | actualSizeIfTruncated += getActualSizeIfTruncated(tempPermissions); |
186 | 0 | for(PermissionBo permission: tempPermissions){ |
187 | 0 | if(!collectedPermissionIds.contains(permission.getId())){ |
188 | 0 | populateAssignedToRoles(permission); |
189 | 0 | collectedPermissionIds.add(permission.getId()); |
190 | 0 | permissions.add(permission); |
191 | |
} |
192 | |
} |
193 | |
|
194 | 0 | List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, roleImpl.getId()); |
195 | 0 | for (String parentRoleId : parentRoleIds) { |
196 | 0 | Map<String, String> roleSearchCriteria = new HashMap<String, String>(); |
197 | 0 | roleSearchCriteria.put("roleId", parentRoleId); |
198 | |
|
199 | 0 | permissions = mergePermissionLists(permissions, getPermissionsWithRoleSearchCriteria(roleSearchCriteria, unbounded)); |
200 | 0 | } |
201 | 0 | } |
202 | |
|
203 | 0 | return new CollectionIncomplete<PermissionBo>(permissions, actualSizeIfTruncated); |
204 | |
} |
205 | |
|
206 | |
|
207 | |
private void populateAssignedToRoles(PermissionBo permission){ |
208 | |
Map<String, String> criteria; |
209 | 0 | for(RolePermissionBo rolePermission: permission.getRolePermissions()){ |
210 | 0 | if ( rolePermission.isActive() ) { |
211 | 0 | criteria = new HashMap<String, String>(); |
212 | 0 | criteria.put("id", rolePermission.getRoleId()); |
213 | |
|
214 | 0 | RoleBo roleBo = getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria); |
215 | |
|
216 | 0 | } |
217 | |
} |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
private static final long PERM_CACHE_EXPIRE_SECONDS = 30L; |
224 | 0 | private static final ConcurrentMap<Map<String,String>,List<PermissionBo>> permResultCache = new MapMaker().expireAfterAccess(PERM_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).softValues().makeMap(); |
225 | |
|
226 | |
|
227 | |
private List<PermissionBo> getPermissionsWithPermissionSearchCriteria( |
228 | |
Map<String, String> permissionSearchCriteria, boolean unbounded){ |
229 | 0 | String detailCriteriaStr = permissionSearchCriteria.remove( DETAIL_CRITERIA ); |
230 | 0 | Map<String, String> detailCriteria = parseDetailCriteria(detailCriteriaStr); |
231 | |
|
232 | 0 | List<PermissionBo> cachedResult = permResultCache.get(permissionSearchCriteria); |
233 | |
List<PermissionBo> permissions; |
234 | 0 | if ( cachedResult == null ) { |
235 | 0 | permissions = searchPermissions(permissionSearchCriteria, unbounded); |
236 | 0 | synchronized (permResultCache) { |
237 | 0 | permResultCache.put(permissionSearchCriteria, permissions); |
238 | 0 | } |
239 | |
} else { |
240 | 0 | permissions = cachedResult; |
241 | |
} |
242 | 0 | List<PermissionBo> filteredPermissions = new CollectionIncomplete<PermissionBo>( |
243 | |
new ArrayList<PermissionBo>(), getActualSizeIfTruncated(permissions)); |
244 | 0 | for(PermissionBo perm: permissions){ |
245 | 0 | if ( detailCriteria.isEmpty() ) { |
246 | 0 | filteredPermissions.add(perm); |
247 | 0 | populateAssignedToRoles(perm); |
248 | |
} else { |
249 | 0 | if ( isMapSubset( new HashMap<String, String>(perm.getDetails()), detailCriteria ) ) { |
250 | 0 | filteredPermissions.add(perm); |
251 | 0 | populateAssignedToRoles(perm); |
252 | |
} |
253 | |
} |
254 | |
} |
255 | 0 | return filteredPermissions; |
256 | |
} |
257 | |
|
258 | |
private List<PermissionBo> getPermissionsSearchResultsCopy(List<PermissionBo> permissionSearchResults){ |
259 | 0 | List<PermissionBo> permissionSearchResultsCopy = new CollectionIncomplete<PermissionBo>( |
260 | |
new ArrayList<PermissionBo>(), getActualSizeIfTruncated(permissionSearchResults)); |
261 | 0 | for(PermissionBo permissionImpl: permissionSearchResults){ |
262 | 0 | PermissionBo permissionCopy = new PermissionBo(); |
263 | |
try{ |
264 | 0 | PropertyUtils.copyProperties(permissionCopy, permissionImpl); |
265 | 0 | } catch(Exception ex){ |
266 | 0 | LOG.error( "Unable to copy properties from KimPermissionImpl to PermissionImpl, skipping.", ex ); |
267 | 0 | continue; |
268 | 0 | } |
269 | 0 | permissionSearchResultsCopy.add(permissionCopy); |
270 | 0 | } |
271 | 0 | return permissionSearchResultsCopy; |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
public LookupService getLookupService() { |
278 | 0 | if ( lookupService == null ) { |
279 | 0 | lookupService = KRADServiceLocatorWeb.getLookupService(); |
280 | |
} |
281 | 0 | return lookupService; |
282 | |
} |
283 | |
|
284 | |
public RoleService getRoleService() { |
285 | 0 | if (roleService == null) { |
286 | 0 | roleService = KimApiServiceLocator.getRoleService(); |
287 | |
} |
288 | 0 | return roleService; |
289 | |
} |
290 | |
|
291 | |
private List<PermissionBo> mergePermissionLists(List<PermissionBo> perm1, List<PermissionBo> perm2) { |
292 | 0 | List<PermissionBo> returnList = new ArrayList<PermissionBo>(perm1); |
293 | 0 | List<String> permissionIds = new ArrayList<String>(perm1.size()); |
294 | 0 | for (PermissionBo perm : returnList) { |
295 | 0 | permissionIds.add(perm.getId()); |
296 | |
} |
297 | 0 | for (int i=0; i<perm2.size(); i++) { |
298 | 0 | if (!permissionIds.contains(perm2.get(i).getId())) { |
299 | 0 | returnList.add(perm2.get(i)); |
300 | |
} |
301 | |
} |
302 | 0 | return returnList; |
303 | |
} |
304 | |
} |