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 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtils.class);
59
60 private static KualiModuleService kualiModuleService;
61 private static Map<String,KimTypeService> kimTypeServiceCache = new HashMap<String,KimTypeService>();
62 private static IdentityManagementService identityManagementService;
63
64 private KimCommonUtils() {
65 }
66
67 private static KualiModuleService getKualiModuleService() {
68 if (kualiModuleService == null) {
69 kualiModuleService = KNSServiceLocator.getKualiModuleService();
70 }
71 return kualiModuleService;
72 }
73
74 public static String getClosestParentDocumentTypeName(
75 DocumentType documentType,
76 Set<String> potentialParentDocumentTypeNames) {
77 if ( potentialParentDocumentTypeNames == null || documentType == null ) {
78 return null;
79 }
80 if (potentialParentDocumentTypeNames.contains(documentType.getName())) {
81 return documentType.getName();
82 } else {
83 if ((documentType.getDocTypeParentId() == null)
84 || documentType.getDocTypeParentId().equals(
85 documentType.getDocumentTypeId())) {
86 return null;
87 } else {
88 return getClosestParentDocumentTypeName(documentType
89 .getParentDocType(), potentialParentDocumentTypeNames);
90 }
91 }
92 }
93
94 public static boolean storedValueNotSpecifiedOrInputValueMatches(AttributeSet storedValues, AttributeSet inputValues, String attributeName) {
95 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 if (StringUtils.isBlank(permissionDetailsPropertyName)) {
102 return true;
103 }
104 if ( requestedDetailsPropertyName == null ) {
105 requestedDetailsPropertyName = "";
106 }
107 return StringUtils.equals(requestedDetailsPropertyName, permissionDetailsPropertyName)
108 || (requestedDetailsPropertyName.startsWith(permissionDetailsPropertyName+"."));
109 }
110
111 public static AttributeSet getNamespaceAndComponentSimpleName( Class<? extends Object> clazz) {
112 AttributeSet attributeSet = new AttributeSet();
113 attributeSet.put(KimAttributes.NAMESPACE_CODE, getNamespaceCode(clazz));
114 attributeSet.put(KimAttributes.COMPONENT_NAME, getComponentSimpleName(clazz));
115 return attributeSet;
116 }
117
118 public static AttributeSet getNamespaceAndComponentFullName( Class<? extends Object> clazz) {
119 AttributeSet attributeSet = new AttributeSet();
120 attributeSet.put(KimAttributes.NAMESPACE_CODE, getNamespaceCode(clazz));
121 attributeSet.put(KimAttributes.COMPONENT_NAME, getComponentFullName(clazz));
122 return attributeSet;
123 }
124
125 public static AttributeSet getNamespaceAndActionClass( Class<? extends Object> clazz) {
126 AttributeSet attributeSet = new AttributeSet();
127 attributeSet.put(KimAttributes.NAMESPACE_CODE, getNamespaceCode(clazz));
128 attributeSet.put(KimAttributes.ACTION_CLASS, clazz.getName());
129 return attributeSet;
130 }
131
132 public static String getNamespaceCode(Class<? extends Object> clazz) {
133 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
134 if (moduleService == null) {
135 return KimConstants.KIM_TYPE_DEFAULT_NAMESPACE;
136 }
137 return moduleService.getModuleConfiguration().getNamespaceCode();
138 }
139
140 public static String getComponentSimpleName(Class<? extends Object> clazz) {
141 return clazz.getSimpleName();
142 }
143
144 public static String getComponentFullName(Class<? extends Object> clazz) {
145 return clazz.getName();
146 }
147
148 public static boolean isAttributeSetEntryEquals( AttributeSet map1, AttributeSet map2, String key ) {
149 return StringUtils.equals( map1.get( key ), map2.get( key ) );
150 }
151
152
153
154
155
156 public static QName resolveKimTypeServiceName(String kimTypeServiceName) {
157 if (StringUtils.isBlank(kimTypeServiceName)) {
158 return resolveKimTypeServiceName(KimConstants.DEFAULT_KIM_TYPE_SERVICE);
159 }
160 return QName.valueOf(kimTypeServiceName);
161 }
162
163
164
165
166 @Deprecated
167 public static KimTypeService getKimTypeService(KimType kimType){
168 return KIMServiceLocator.getKimTypeService(kimType);
169 }
170
171
172
173
174 @Deprecated
175 public static KimTypeService getKimTypeService( String serviceName ) {
176 return KIMServiceLocator.getKimTypeService(resolveKimTypeServiceName(serviceName));
177 }
178
179 public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){
180 if(targetToCopyTo!=null && sourceToCopyFrom!=null)
181 try{
182 PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom);
183 } catch(Exception ex){
184 throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex);
185 }
186 }
187
188 public static String getKimBasePath(){
189 String kimBaseUrl = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KimConstants.KimUIConstants.KIM_URL_KEY);
190 if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) {
191 kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR;
192 }
193 return kimBaseUrl;
194 }
195
196 public static String getPathWithKimContext(String path, String kimActionName){
197 String kimContext = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.URL_SEPARATOR;
198 String kimContextParameterized = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.PARAMETERIZED_URL_SEPARATOR;
199 if(path.contains(kimActionName) && !path.contains(kimContext + kimActionName)
200 && !path.contains(kimContextParameterized + kimActionName))
201 path = path.replace(kimActionName, kimContext+kimActionName);
202 return path;
203 }
204
205 public static String stripEnd(String toStripFrom, String toStrip){
206 String stripped;
207 if(toStripFrom==null) return null;
208 if(toStrip==null) return toStripFrom;
209 if(toStrip.length() > toStripFrom.length()) return toStripFrom;
210 if(toStripFrom.endsWith(toStrip)){
211 StringBuffer buffer = new StringBuffer(toStripFrom);
212 buffer.delete(buffer.length()-toStrip.length(), buffer.length());
213 stripped = buffer.toString();
214 } else stripped = toStripFrom;
215 return stripped;
216 }
217
218 protected static boolean canOverrideEntityPrivacyPreferences( String principalId ){
219 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 KimEntityPrivacyPreferences privacy = null;
229 KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
230 if (entityInfo != null) {
231 privacy = entityInfo.getPrivacyPreferences();
232 }
233 UserSession userSession = GlobalVariables.getUserSession();
234
235 boolean suppressName = false;
236 if (privacy != null) {
237 suppressName = privacy.isSuppressName();
238 }
239 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 KimEntityPrivacyPreferences privacy = null;
247 KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
248 if (entityInfo != null) {
249 privacy = entityInfo.getPrivacyPreferences();
250 }
251 UserSession userSession = GlobalVariables.getUserSession();
252
253 boolean suppressEmail = false;
254 if (privacy != null) {
255 suppressEmail = privacy.isSuppressEmail();
256 }
257 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 KimEntityPrivacyPreferences privacy = null;
265 KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
266 if (entityInfo != null) {
267 privacy = entityInfo.getPrivacyPreferences();
268 }
269 UserSession userSession = GlobalVariables.getUserSession();
270
271 boolean suppressAddress = false;
272 if (privacy != null) {
273 suppressAddress = privacy.isSuppressAddress();
274 }
275 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 KimEntityPrivacyPreferences privacy = null;
283 KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
284 if (entityInfo != null) {
285 privacy = entityInfo.getPrivacyPreferences();
286 }
287 UserSession userSession = GlobalVariables.getUserSession();
288
289 boolean suppressPhone = false;
290 if (privacy != null) {
291 suppressPhone = privacy.isSuppressPhone();
292 }
293 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 KimEntityPrivacyPreferences privacy = null;
301 KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
302 if (entityInfo != null) {
303 privacy = entityInfo.getPrivacyPreferences();
304 }
305 UserSession userSession = GlobalVariables.getUserSession();
306
307 boolean suppressPersonal = false;
308 if (privacy != null) {
309 suppressPersonal = privacy.isSuppressPersonal();
310 }
311 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 Map<String, String> criteria = new HashMap<String, String>();
319 criteria.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE, externalIdentifierType);
320 ExternalIdentifierType externalIdentifierTypeObject = (ExternalIdentifierType) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ExternalIdentifierTypeImpl.class, criteria);
321 if( externalIdentifierTypeObject!= null && externalIdentifierTypeObject.isEncryptionRequired()){
322 if(StringUtils.isNotEmpty(externalIdentifier)){
323 try{
324 return KNSServiceLocator.getEncryptionService().encrypt(externalIdentifier);
325 }catch (GeneralSecurityException e) {
326 LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted");
327 }
328 }
329 }
330 return externalIdentifier;
331 }
332
333 public static String decryptExternalIdentifier(String externalIdentifier, String externalIdentifierType){
334 Map<String, String> criteria = new HashMap<String, String>();
335 criteria.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE, externalIdentifierType);
336 ExternalIdentifierType externalIdentifierTypeObject = (ExternalIdentifierType) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ExternalIdentifierTypeImpl.class, criteria);
337 if( externalIdentifierTypeObject!= null && externalIdentifierTypeObject.isEncryptionRequired()){
338 if(StringUtils.isNotEmpty(externalIdentifier)){
339 try{
340 return KNSServiceLocator.getEncryptionService().decrypt(externalIdentifier);
341 }catch (GeneralSecurityException e) {
342 LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted");
343 }
344 }
345 }
346 return externalIdentifier;
347 }
348
349 public static IdentityManagementService getIdentityManagementService() {
350 if ( identityManagementService == null ) {
351 identityManagementService = KIMServiceLocator.getIdentityManagementService();
352 }
353 return identityManagementService;
354 }
355
356
357 public static GroupImpl copyInfoToGroup(Group info, GroupImpl group) {
358 group.setActive(info.isActive());
359 group.setGroupDescription(info.getGroupDescription());
360 group.setGroupId(info.getGroupId());
361 group.setGroupName(info.getGroupName());
362 group.setKimTypeId(info.getKimTypeId());
363 group.setNamespaceCode(info.getNamespaceCode());
364
365 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 List<GroupAttributeDataImpl> attrList = new ArrayList<GroupAttributeDataImpl>(infoMap.size());
378 List<KimTypeAttributeInfo> attributeInfoList = KIMServiceLocator.getTypeInfoService().getKimType(kimTypeId).getAttributeDefinitions();
379
380 for (String key : infoMap.keySet()) {
381 KimTypeAttributeInfo typeAttributeInfo = getAttributeInfo(attributeInfoList, key);
382
383 if (typeAttributeInfo != null) {
384 GroupAttributeDataImpl groupAttribute = new GroupAttributeDataImpl();
385 groupAttribute.setKimAttributeId(typeAttributeInfo.getKimAttributeId());
386 groupAttribute.setAttributeValue(infoMap.get(typeAttributeInfo.getAttributeName()));
387 groupAttribute.setGroupId(groupId);
388 groupAttribute.setKimTypeId(kimTypeId);
389 attrList.add(groupAttribute);
390 } else {
391 throw new IllegalArgumentException("KimAttribute not found: " + key);
392 }
393 }
394 return attrList;
395 }
396
397 private static KimTypeAttributeInfo getAttributeInfo(List<KimTypeAttributeInfo> attributeInfoList, String attributeName) {
398 KimTypeAttributeInfo kRet = null;
399 for (KimTypeAttributeInfo attributeInfo : attributeInfoList) {
400 if (attributeInfo.getAttributeName().equals(attributeName)) {
401 kRet = attributeInfo;
402 break;
403 }
404 }
405 return kRet;
406 }
407
408 }