View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.identity;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kim.api.KimConstants;
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.permission.PermissionService;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.krad.UserSession;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  
28  import java.util.Collections;
29  
30  class KimInternalSuppressUtils {
31  
32      private static IdentityService identityService;
33      private static PermissionService permissionService;
34  
35  	private KimInternalSuppressUtils() {
36  		throw new UnsupportedOperationException("do not call");
37  	}
38  
39      public static boolean isSuppressName(String entityId) {
40          EntityPrivacyPreferences privacy = null;
41          EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
42          if (entityInfo != null) {
43              privacy = entityInfo.getPrivacyPreferences();
44          } else {
45              return true;
46          }
47          UserSession userSession = GlobalVariables.getUserSession();
48  
49          boolean suppressName = false;
50          if (privacy != null) {
51              suppressName = privacy.isSuppressName();
52          }
53  
54          return suppressName
55                  && userSession != null
56                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
57                  && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
58      }
59  
60      public static boolean isSuppressEmail(String entityId) {
61          EntityPrivacyPreferences privacy = null;
62          EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
63          if (entityInfo != null) {
64              privacy = entityInfo.getPrivacyPreferences();
65          } else {
66              return true;
67          }
68          UserSession userSession = GlobalVariables.getUserSession();
69  
70          boolean suppressEmail = false;
71          if (privacy != null) {
72              suppressEmail = privacy.isSuppressEmail();
73          }
74          return suppressEmail
75                  && userSession != null
76                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
77                  && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
78      }
79  
80      public static boolean isSuppressAddress(String entityId) {
81          EntityPrivacyPreferences privacy = null;
82          EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
83          if (entityInfo != null) {
84              privacy = entityInfo.getPrivacyPreferences();
85          } else {
86              return false;
87          }
88          UserSession userSession = GlobalVariables.getUserSession();
89  
90          boolean suppressAddress = false;
91          if (privacy != null) {
92              suppressAddress = privacy.isSuppressAddress();
93          }
94          return suppressAddress
95                  && userSession != null
96                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
97                  && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
98      }
99  
100     public static boolean isSuppressPhone(String entityId) {
101         EntityPrivacyPreferences privacy = null;
102         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
103         if (entityInfo != null) {
104             privacy = entityInfo.getPrivacyPreferences();
105         } else {
106             return true;
107         }
108         UserSession userSession = GlobalVariables.getUserSession();
109 
110         boolean suppressPhone = false;
111         if (privacy != null) {
112             suppressPhone = privacy.isSuppressPhone();
113         }
114         return suppressPhone
115                 && userSession != null
116                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
117                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
118     }
119 
120     public static boolean isSuppressPersonal(String entityId) {
121         EntityPrivacyPreferences privacy = null;
122         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
123         if (entityInfo != null) {
124             privacy = entityInfo.getPrivacyPreferences();
125         } else {
126             return true;
127         }
128         UserSession userSession = GlobalVariables.getUserSession();
129 
130         boolean suppressPersonal = false;
131         if (privacy != null) {
132             suppressPersonal = privacy.isSuppressPersonal();
133         }
134         return suppressPersonal
135                 && userSession != null
136                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
137                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
138     }
139 
140     protected static boolean canOverrideEntityPrivacyPreferences( String principalId ){
141 		return getPermissionService().isAuthorized(
142 				GlobalVariables.getUserSession().getPrincipalId(),
143 				KimConstants.NAMESPACE_CODE,
144 				KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES,
145 				Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, principalId) );
146 	}
147 
148     private static IdentityService getIdentityService() {
149 		if ( identityService == null ) {
150 			identityService = KimApiServiceLocator.getIdentityService();
151 		}
152 		return identityService;
153 	}
154 
155     private static PermissionService getPermissionService() {
156 		if ( permissionService == null ) {
157 			permissionService = KimApiServiceLocator.getPermissionService();
158 		}
159 		return permissionService;
160     }
161 }