1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.util; |
17 | |
|
18 | |
import java.security.GeneralSecurityException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import javax.xml.namespace.QName; |
26 | |
|
27 | |
import org.apache.commons.beanutils.PropertyUtils; |
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
30 | |
import org.kuali.rice.kim.bo.Group; |
31 | |
import org.kuali.rice.kim.bo.KimType; |
32 | |
import org.kuali.rice.kim.bo.entity.KimEntityPrivacyPreferences; |
33 | |
import org.kuali.rice.kim.bo.entity.dto.KimEntityDefaultInfo; |
34 | |
import org.kuali.rice.kim.bo.group.impl.GroupAttributeDataImpl; |
35 | |
import org.kuali.rice.kim.bo.impl.GroupImpl; |
36 | |
import org.kuali.rice.kim.bo.impl.KimAttributes; |
37 | |
import org.kuali.rice.kim.bo.reference.ExternalIdentifierType; |
38 | |
import org.kuali.rice.kim.bo.reference.impl.ExternalIdentifierTypeImpl; |
39 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
40 | |
import org.kuali.rice.kim.bo.types.dto.KimTypeAttributeInfo; |
41 | |
import org.kuali.rice.kim.service.IdentityManagementService; |
42 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
43 | |
import org.kuali.rice.kim.service.support.KimTypeService; |
44 | |
import org.kuali.rice.kns.UserSession; |
45 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
46 | |
import org.kuali.rice.kns.service.KualiModuleService; |
47 | |
import org.kuali.rice.kns.service.ModuleService; |
48 | |
import org.kuali.rice.kns.util.GlobalVariables; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public class KimCommonUtils { |
58 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtils.class); |
59 | |
|
60 | |
private static KualiModuleService kualiModuleService; |
61 | 0 | private static Map<String,KimTypeService> kimTypeServiceCache = new HashMap<String,KimTypeService>(); |
62 | |
private static IdentityManagementService identityManagementService; |
63 | |
|
64 | 0 | private KimCommonUtils() { |
65 | 0 | } |
66 | |
|
67 | |
private static KualiModuleService getKualiModuleService() { |
68 | 0 | if (kualiModuleService == null) { |
69 | 0 | kualiModuleService = KNSServiceLocator.getKualiModuleService(); |
70 | |
} |
71 | 0 | return kualiModuleService; |
72 | |
} |
73 | |
|
74 | |
public static String getClosestParentDocumentTypeName( |
75 | |
DocumentType documentType, |
76 | |
Set<String> potentialParentDocumentTypeNames) { |
77 | 0 | if ( potentialParentDocumentTypeNames == null || documentType == null ) { |
78 | 0 | return null; |
79 | |
} |
80 | 0 | if (potentialParentDocumentTypeNames.contains(documentType.getName())) { |
81 | 0 | return documentType.getName(); |
82 | |
} else { |
83 | 0 | if ((documentType.getDocTypeParentId() == null) |
84 | |
|| documentType.getDocTypeParentId().equals( |
85 | |
documentType.getDocumentTypeId())) { |
86 | 0 | return null; |
87 | |
} else { |
88 | 0 | return getClosestParentDocumentTypeName(documentType |
89 | |
.getParentDocType(), potentialParentDocumentTypeNames); |
90 | |
} |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
public static boolean storedValueNotSpecifiedOrInputValueMatches(AttributeSet storedValues, AttributeSet inputValues, String attributeName) { |
95 | 0 | return ((storedValues == null) || (inputValues == null)) || !storedValues.containsKey(attributeName) || storedValues.get(attributeName).equals(inputValues.get(attributeName)); |
96 | |
} |
97 | |
|
98 | |
public static boolean doesPropertyNameMatch( |
99 | |
String requestedDetailsPropertyName, |
100 | |
String permissionDetailsPropertyName) { |
101 | 0 | if (StringUtils.isBlank(permissionDetailsPropertyName)) { |
102 | 0 | return true; |
103 | |
} |
104 | 0 | if ( requestedDetailsPropertyName == null ) { |
105 | 0 | requestedDetailsPropertyName = ""; |
106 | |
} |
107 | 0 | return StringUtils.equals(requestedDetailsPropertyName, permissionDetailsPropertyName) |
108 | |
|| (requestedDetailsPropertyName.startsWith(permissionDetailsPropertyName+".")); |
109 | |
} |
110 | |
|
111 | |
public static AttributeSet getNamespaceAndComponentSimpleName( Class<? extends Object> clazz) { |
112 | 0 | AttributeSet attributeSet = new AttributeSet(); |
113 | 0 | attributeSet.put(KimAttributes.NAMESPACE_CODE, getNamespaceCode(clazz)); |
114 | 0 | attributeSet.put(KimAttributes.COMPONENT_NAME, getComponentSimpleName(clazz)); |
115 | 0 | return attributeSet; |
116 | |
} |
117 | |
|
118 | |
public static AttributeSet getNamespaceAndComponentFullName( Class<? extends Object> clazz) { |
119 | 0 | AttributeSet attributeSet = new AttributeSet(); |
120 | 0 | attributeSet.put(KimAttributes.NAMESPACE_CODE, getNamespaceCode(clazz)); |
121 | 0 | attributeSet.put(KimAttributes.COMPONENT_NAME, getComponentFullName(clazz)); |
122 | 0 | return attributeSet; |
123 | |
} |
124 | |
|
125 | |
public static AttributeSet getNamespaceAndActionClass( Class<? extends Object> clazz) { |
126 | 0 | AttributeSet attributeSet = new AttributeSet(); |
127 | 0 | attributeSet.put(KimAttributes.NAMESPACE_CODE, getNamespaceCode(clazz)); |
128 | 0 | attributeSet.put(KimAttributes.ACTION_CLASS, clazz.getName()); |
129 | 0 | return attributeSet; |
130 | |
} |
131 | |
|
132 | |
public static String getNamespaceCode(Class<? extends Object> clazz) { |
133 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); |
134 | 0 | if (moduleService == null) { |
135 | 0 | return KimConstants.KIM_TYPE_DEFAULT_NAMESPACE; |
136 | |
} |
137 | 0 | return moduleService.getModuleConfiguration().getNamespaceCode(); |
138 | |
} |
139 | |
|
140 | |
public static String getComponentSimpleName(Class<? extends Object> clazz) { |
141 | 0 | return clazz.getSimpleName(); |
142 | |
} |
143 | |
|
144 | |
public static String getComponentFullName(Class<? extends Object> clazz) { |
145 | 0 | return clazz.getName(); |
146 | |
} |
147 | |
|
148 | |
public static boolean isAttributeSetEntryEquals( AttributeSet map1, AttributeSet map2, String key ) { |
149 | 0 | return StringUtils.equals( map1.get( key ), map2.get( key ) ); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public static QName resolveKimTypeServiceName(String kimTypeServiceName) { |
157 | 0 | if (StringUtils.isBlank(kimTypeServiceName)) { |
158 | 0 | return resolveKimTypeServiceName(KimConstants.DEFAULT_KIM_TYPE_SERVICE); |
159 | |
} |
160 | 0 | return QName.valueOf(kimTypeServiceName); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
@Deprecated |
167 | |
public static KimTypeService getKimTypeService(KimType kimType){ |
168 | 0 | return KIMServiceLocator.getKimTypeService(kimType); |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
@Deprecated |
175 | |
public static KimTypeService getKimTypeService( String serviceName ) { |
176 | 0 | return KIMServiceLocator.getKimTypeService(resolveKimTypeServiceName(serviceName)); |
177 | |
} |
178 | |
|
179 | |
public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){ |
180 | 0 | if(targetToCopyTo!=null && sourceToCopyFrom!=null) |
181 | |
try{ |
182 | 0 | PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom); |
183 | 0 | } catch(Exception ex){ |
184 | 0 | throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex); |
185 | 0 | } |
186 | 0 | } |
187 | |
|
188 | |
public static String getKimBasePath(){ |
189 | 0 | String kimBaseUrl = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KimConstants.KimUIConstants.KIM_URL_KEY); |
190 | 0 | if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) { |
191 | 0 | kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR; |
192 | |
} |
193 | 0 | return kimBaseUrl; |
194 | |
} |
195 | |
|
196 | |
public static String getPathWithKimContext(String path, String kimActionName){ |
197 | 0 | String kimContext = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.URL_SEPARATOR; |
198 | 0 | String kimContextParameterized = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.PARAMETERIZED_URL_SEPARATOR; |
199 | 0 | if(path.contains(kimActionName) && !path.contains(kimContext + kimActionName) |
200 | |
&& !path.contains(kimContextParameterized + kimActionName)) |
201 | 0 | path = path.replace(kimActionName, kimContext+kimActionName); |
202 | 0 | return path; |
203 | |
} |
204 | |
|
205 | |
public static String stripEnd(String toStripFrom, String toStrip){ |
206 | |
String stripped; |
207 | 0 | if(toStripFrom==null) return null; |
208 | 0 | if(toStrip==null) return toStripFrom; |
209 | 0 | if(toStrip.length() > toStripFrom.length()) return toStripFrom; |
210 | 0 | if(toStripFrom.endsWith(toStrip)){ |
211 | 0 | StringBuffer buffer = new StringBuffer(toStripFrom); |
212 | 0 | buffer.delete(buffer.length()-toStrip.length(), buffer.length()); |
213 | 0 | stripped = buffer.toString(); |
214 | 0 | } else stripped = toStripFrom; |
215 | 0 | return stripped; |
216 | |
} |
217 | |
|
218 | |
protected static boolean canOverrideEntityPrivacyPreferences( String principalId ){ |
219 | 0 | return getIdentityManagementService().isAuthorized( |
220 | |
GlobalVariables.getUserSession().getPrincipalId(), |
221 | |
KimConstants.NAMESPACE_CODE, |
222 | |
KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES, |
223 | |
null, |
224 | |
new AttributeSet(KimAttributes.PRINCIPAL_ID, principalId) ); |
225 | |
} |
226 | |
|
227 | |
public static boolean isSuppressName(String entityId) { |
228 | 0 | KimEntityPrivacyPreferences privacy = null; |
229 | 0 | KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId); |
230 | 0 | if (entityInfo != null) { |
231 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
232 | |
} |
233 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
234 | |
|
235 | 0 | boolean suppressName = false; |
236 | 0 | if (privacy != null) { |
237 | 0 | suppressName = privacy.isSuppressName(); |
238 | |
} |
239 | 0 | return suppressName |
240 | |
&& userSession != null |
241 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
242 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
243 | |
} |
244 | |
|
245 | |
public static boolean isSuppressEmail(String entityId) { |
246 | 0 | KimEntityPrivacyPreferences privacy = null; |
247 | 0 | KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId); |
248 | 0 | if (entityInfo != null) { |
249 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
250 | |
} |
251 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
252 | |
|
253 | 0 | boolean suppressEmail = false; |
254 | 0 | if (privacy != null) { |
255 | 0 | suppressEmail = privacy.isSuppressEmail(); |
256 | |
} |
257 | 0 | return suppressEmail |
258 | |
&& userSession != null |
259 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
260 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
261 | |
} |
262 | |
|
263 | |
public static boolean isSuppressAddress(String entityId) { |
264 | 0 | KimEntityPrivacyPreferences privacy = null; |
265 | 0 | KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId); |
266 | 0 | if (entityInfo != null) { |
267 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
268 | |
} |
269 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
270 | |
|
271 | 0 | boolean suppressAddress = false; |
272 | 0 | if (privacy != null) { |
273 | 0 | suppressAddress = privacy.isSuppressAddress(); |
274 | |
} |
275 | 0 | return suppressAddress |
276 | |
&& userSession != null |
277 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
278 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
279 | |
} |
280 | |
|
281 | |
public static boolean isSuppressPhone(String entityId) { |
282 | 0 | KimEntityPrivacyPreferences privacy = null; |
283 | 0 | KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId); |
284 | 0 | if (entityInfo != null) { |
285 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
286 | |
} |
287 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
288 | |
|
289 | 0 | boolean suppressPhone = false; |
290 | 0 | if (privacy != null) { |
291 | 0 | suppressPhone = privacy.isSuppressPhone(); |
292 | |
} |
293 | 0 | return suppressPhone |
294 | |
&& userSession != null |
295 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
296 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
297 | |
} |
298 | |
|
299 | |
public static boolean isSuppressPersonal(String entityId) { |
300 | 0 | KimEntityPrivacyPreferences privacy = null; |
301 | 0 | KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId); |
302 | 0 | if (entityInfo != null) { |
303 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
304 | |
} |
305 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
306 | |
|
307 | 0 | boolean suppressPersonal = false; |
308 | 0 | if (privacy != null) { |
309 | 0 | suppressPersonal = privacy.isSuppressPersonal(); |
310 | |
} |
311 | 0 | return suppressPersonal |
312 | |
&& userSession != null |
313 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
314 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
315 | |
} |
316 | |
|
317 | |
public static String encryptExternalIdentifier(String externalIdentifier, String externalIdentifierType){ |
318 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
319 | 0 | criteria.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE, externalIdentifierType); |
320 | 0 | ExternalIdentifierType externalIdentifierTypeObject = (ExternalIdentifierType) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ExternalIdentifierTypeImpl.class, criteria); |
321 | 0 | if( externalIdentifierTypeObject!= null && externalIdentifierTypeObject.isEncryptionRequired()){ |
322 | 0 | if(StringUtils.isNotEmpty(externalIdentifier)){ |
323 | |
try{ |
324 | 0 | return KNSServiceLocator.getEncryptionService().encrypt(externalIdentifier); |
325 | 0 | }catch (GeneralSecurityException e) { |
326 | 0 | LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted"); |
327 | |
} |
328 | |
} |
329 | |
} |
330 | 0 | return externalIdentifier; |
331 | |
} |
332 | |
|
333 | |
public static String decryptExternalIdentifier(String externalIdentifier, String externalIdentifierType){ |
334 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
335 | 0 | criteria.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE, externalIdentifierType); |
336 | 0 | ExternalIdentifierType externalIdentifierTypeObject = (ExternalIdentifierType) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ExternalIdentifierTypeImpl.class, criteria); |
337 | 0 | if( externalIdentifierTypeObject!= null && externalIdentifierTypeObject.isEncryptionRequired()){ |
338 | 0 | if(StringUtils.isNotEmpty(externalIdentifier)){ |
339 | |
try{ |
340 | 0 | return KNSServiceLocator.getEncryptionService().decrypt(externalIdentifier); |
341 | 0 | }catch (GeneralSecurityException e) { |
342 | 0 | LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted"); |
343 | |
} |
344 | |
} |
345 | |
} |
346 | 0 | return externalIdentifier; |
347 | |
} |
348 | |
|
349 | |
public static IdentityManagementService getIdentityManagementService() { |
350 | 0 | if ( identityManagementService == null ) { |
351 | 0 | identityManagementService = KIMServiceLocator.getIdentityManagementService(); |
352 | |
} |
353 | 0 | return identityManagementService; |
354 | |
} |
355 | |
|
356 | |
|
357 | |
public static GroupImpl copyInfoToGroup(Group info, GroupImpl group) { |
358 | 0 | group.setActive(info.isActive()); |
359 | 0 | group.setGroupDescription(info.getGroupDescription()); |
360 | 0 | group.setGroupId(info.getGroupId()); |
361 | 0 | group.setGroupName(info.getGroupName()); |
362 | 0 | group.setKimTypeId(info.getKimTypeId()); |
363 | 0 | group.setNamespaceCode(info.getNamespaceCode()); |
364 | |
|
365 | 0 | return group; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
public static List<GroupAttributeDataImpl> copyInfoAttributesToGroupAttributes(Map<String, String> infoMap, String groupId, String kimTypeId) { |
377 | 0 | List<GroupAttributeDataImpl> attrList = new ArrayList<GroupAttributeDataImpl>(infoMap.size()); |
378 | 0 | List<KimTypeAttributeInfo> attributeInfoList = KIMServiceLocator.getTypeInfoService().getKimType(kimTypeId).getAttributeDefinitions(); |
379 | |
|
380 | 0 | for (String key : infoMap.keySet()) { |
381 | 0 | KimTypeAttributeInfo typeAttributeInfo = getAttributeInfo(attributeInfoList, key); |
382 | |
|
383 | 0 | if (typeAttributeInfo != null) { |
384 | 0 | GroupAttributeDataImpl groupAttribute = new GroupAttributeDataImpl(); |
385 | 0 | groupAttribute.setKimAttributeId(typeAttributeInfo.getKimAttributeId()); |
386 | 0 | groupAttribute.setAttributeValue(infoMap.get(typeAttributeInfo.getAttributeName())); |
387 | 0 | groupAttribute.setGroupId(groupId); |
388 | 0 | groupAttribute.setKimTypeId(kimTypeId); |
389 | 0 | attrList.add(groupAttribute); |
390 | 0 | } else { |
391 | 0 | throw new IllegalArgumentException("KimAttribute not found: " + key); |
392 | |
} |
393 | 0 | } |
394 | 0 | return attrList; |
395 | |
} |
396 | |
|
397 | |
private static KimTypeAttributeInfo getAttributeInfo(List<KimTypeAttributeInfo> attributeInfoList, String attributeName) { |
398 | 0 | KimTypeAttributeInfo kRet = null; |
399 | 0 | for (KimTypeAttributeInfo attributeInfo : attributeInfoList) { |
400 | 0 | if (attributeInfo.getAttributeName().equals(attributeName)) { |
401 | 0 | kRet = attributeInfo; |
402 | 0 | break; |
403 | |
} |
404 | |
} |
405 | 0 | return kRet; |
406 | |
} |
407 | |
|
408 | |
} |