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 org.apache.commons.beanutils.PropertyUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.kim.api.identity.IdentityService; |
21 | |
import org.kuali.rice.kim.api.identity.entity.EntityDefault; |
22 | |
import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences; |
23 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
24 | |
import org.kuali.rice.kim.api.type.KimTypeAttribute; |
25 | |
import org.kuali.rice.kim.service.PermissionService; |
26 | |
import org.kuali.rice.krad.UserSession; |
27 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
28 | |
import org.kuali.rice.krad.util.GlobalVariables; |
29 | |
|
30 | |
import java.util.Collections; |
31 | |
import java.util.List; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public final class KimCommonUtilsInternal { |
41 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtilsInternal.class); |
42 | |
|
43 | |
private static IdentityService identityService; |
44 | |
private static PermissionService permissionService; |
45 | |
|
46 | 0 | private KimCommonUtilsInternal() { |
47 | 0 | throw new UnsupportedOperationException("do not call"); |
48 | |
} |
49 | |
|
50 | |
public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){ |
51 | 0 | if(targetToCopyTo!=null && sourceToCopyFrom!=null) |
52 | |
try{ |
53 | 0 | PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom); |
54 | 0 | } catch(Exception ex){ |
55 | 0 | throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex); |
56 | 0 | } |
57 | 0 | } |
58 | |
|
59 | |
public static String getKimBasePath(){ |
60 | 0 | String kimBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString( |
61 | |
KimConstants.KimUIConstants.KIM_URL_KEY); |
62 | 0 | if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) { |
63 | 0 | kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR; |
64 | |
} |
65 | 0 | return kimBaseUrl; |
66 | |
} |
67 | |
|
68 | |
public static String getPathWithKimContext(String path, String kimActionName){ |
69 | 0 | String kimContext = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.URL_SEPARATOR; |
70 | 0 | String kimContextParameterized = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.PARAMETERIZED_URL_SEPARATOR; |
71 | 0 | if(path.contains(kimActionName) && !path.contains(kimContext + kimActionName) |
72 | |
&& !path.contains(kimContextParameterized + kimActionName)) |
73 | 0 | path = path.replace(kimActionName, kimContext+kimActionName); |
74 | 0 | return path; |
75 | |
} |
76 | |
|
77 | |
public static String stripEnd(String toStripFrom, String toStrip){ |
78 | |
String stripped; |
79 | 0 | if(toStripFrom==null) return null; |
80 | 0 | if(toStrip==null) return toStripFrom; |
81 | 0 | if(toStrip.length() > toStripFrom.length()) return toStripFrom; |
82 | 0 | if(toStripFrom.endsWith(toStrip)){ |
83 | 0 | StringBuffer buffer = new StringBuffer(toStripFrom); |
84 | 0 | buffer.delete(buffer.length()-toStrip.length(), buffer.length()); |
85 | 0 | stripped = buffer.toString(); |
86 | 0 | } else stripped = toStripFrom; |
87 | 0 | return stripped; |
88 | |
} |
89 | |
|
90 | |
protected static boolean canOverrideEntityPrivacyPreferences( String principalId ){ |
91 | 0 | return getPermissionService().isAuthorized( |
92 | |
GlobalVariables.getUserSession().getPrincipalId(), |
93 | |
KimConstants.NAMESPACE_CODE, |
94 | |
KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES, |
95 | |
null, |
96 | |
Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, principalId) ); |
97 | |
} |
98 | |
|
99 | |
public static boolean isSuppressName(String entityId) { |
100 | 0 | EntityPrivacyPreferences privacy = null; |
101 | 0 | EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId); |
102 | 0 | if (entityInfo != null) { |
103 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
104 | |
} else { |
105 | 0 | return true; |
106 | |
} |
107 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
108 | |
|
109 | 0 | boolean suppressName = false; |
110 | 0 | if (privacy != null) { |
111 | 0 | suppressName = privacy.isSuppressName(); |
112 | |
} |
113 | |
|
114 | 0 | return suppressName |
115 | |
&& userSession != null |
116 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
117 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
118 | |
} |
119 | |
|
120 | |
public static boolean isSuppressEmail(String entityId) { |
121 | 0 | EntityPrivacyPreferences privacy = null; |
122 | 0 | EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId); |
123 | 0 | if (entityInfo != null) { |
124 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
125 | |
} else { |
126 | 0 | return true; |
127 | |
} |
128 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
129 | |
|
130 | 0 | boolean suppressEmail = false; |
131 | 0 | if (privacy != null) { |
132 | 0 | suppressEmail = privacy.isSuppressEmail(); |
133 | |
} |
134 | 0 | return suppressEmail |
135 | |
&& userSession != null |
136 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
137 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
138 | |
} |
139 | |
|
140 | |
public static boolean isSuppressAddress(String entityId) { |
141 | 0 | EntityPrivacyPreferences privacy = null; |
142 | 0 | EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId); |
143 | 0 | if (entityInfo != null) { |
144 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
145 | |
} else { |
146 | 0 | return false; |
147 | |
} |
148 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
149 | |
|
150 | 0 | boolean suppressAddress = false; |
151 | 0 | if (privacy != null) { |
152 | 0 | suppressAddress = privacy.isSuppressAddress(); |
153 | |
} |
154 | 0 | return suppressAddress |
155 | |
&& userSession != null |
156 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
157 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
158 | |
} |
159 | |
|
160 | |
public static boolean isSuppressPhone(String entityId) { |
161 | 0 | EntityPrivacyPreferences privacy = null; |
162 | 0 | EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId); |
163 | 0 | if (entityInfo != null) { |
164 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
165 | |
} else { |
166 | 0 | return true; |
167 | |
} |
168 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
169 | |
|
170 | 0 | boolean suppressPhone = false; |
171 | 0 | if (privacy != null) { |
172 | 0 | suppressPhone = privacy.isSuppressPhone(); |
173 | |
} |
174 | 0 | return suppressPhone |
175 | |
&& userSession != null |
176 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
177 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
178 | |
} |
179 | |
|
180 | |
public static boolean isSuppressPersonal(String entityId) { |
181 | 0 | EntityPrivacyPreferences privacy = null; |
182 | 0 | EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId); |
183 | 0 | if (entityInfo != null) { |
184 | 0 | privacy = entityInfo.getPrivacyPreferences(); |
185 | |
} else { |
186 | 0 | return true; |
187 | |
} |
188 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
189 | |
|
190 | 0 | boolean suppressPersonal = false; |
191 | 0 | if (privacy != null) { |
192 | 0 | suppressPersonal = privacy.isSuppressPersonal(); |
193 | |
} |
194 | 0 | return suppressPersonal |
195 | |
&& userSession != null |
196 | |
&& !StringUtils.equals(userSession.getPerson().getEntityId(), entityId) |
197 | |
&& !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId()); |
198 | |
} |
199 | |
|
200 | |
private static IdentityService getIdentityService() { |
201 | 0 | if ( identityService == null ) { |
202 | 0 | identityService = KimApiServiceLocator.getIdentityService(); |
203 | |
} |
204 | 0 | return identityService; |
205 | |
} |
206 | |
|
207 | |
private static PermissionService getPermissionService() { |
208 | 0 | if ( permissionService == null ) { |
209 | 0 | permissionService = KimApiServiceLocator.getPermissionService(); |
210 | |
} |
211 | 0 | return permissionService; |
212 | |
} |
213 | |
|
214 | |
private static KimTypeAttribute getAttributeInfo(List<KimTypeAttribute> attributeInfoList, String attributeName) { |
215 | 0 | KimTypeAttribute kRet = null; |
216 | 0 | for (KimTypeAttribute attributeInfo : attributeInfoList) { |
217 | 0 | if (attributeInfo.getKimAttribute().getAttributeName().equals(attributeName)) { |
218 | 0 | kRet = attributeInfo; |
219 | 0 | break; |
220 | |
} |
221 | |
} |
222 | 0 | return kRet; |
223 | |
} |
224 | |
|
225 | |
} |