View Javadoc
1   /**
2    * Copyright 2005-2014 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 java.util.Collections;
19  import java.util.List;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.kim.api.KimConstants;
24  import org.kuali.rice.kim.api.identity.IdentityService;
25  import org.kuali.rice.kim.api.identity.principal.Principal;
26  import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
27  import org.kuali.rice.kim.api.permission.PermissionService;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.krad.UserSession;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  class KimInternalSuppressUtils {
33  
34      private static IdentityService identityService;
35      private static PermissionService permissionService;
36  
37  	private KimInternalSuppressUtils() {
38  		throw new UnsupportedOperationException("do not call");
39  	}
40  
41      public static boolean isSuppressName(String entityId) {
42          EntityPrivacyPreferences privacy = getIdentityService().getEntityPrivacyPreferences(entityId);
43          if (privacy == null) {
44              return false; // no privacy preferences, assume unset
45          }
46          UserSession userSession = GlobalVariables.getUserSession();
47  
48          boolean suppressName = privacy.isSuppressName();
49  
50          return suppressName
51                  && userSession != null
52                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
53                  && !canOverrideEntityPrivacyPreferences(entityId);
54      }
55  
56      public static boolean isSuppressEmail(String entityId) {
57          EntityPrivacyPreferences privacy = getIdentityService().getEntityPrivacyPreferences(entityId);
58          if (privacy == null) {
59              return false; // no privacy preferences, assume unset
60          }
61          UserSession userSession = GlobalVariables.getUserSession();
62  
63          boolean suppressEmail = privacy.isSuppressEmail();
64          return suppressEmail
65                  && userSession != null
66                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
67                  && !canOverrideEntityPrivacyPreferences(entityId);
68      }
69  
70      public static boolean isSuppressAddress(String entityId) {
71          EntityPrivacyPreferences privacy = getIdentityService().getEntityPrivacyPreferences(entityId);
72          if (privacy == null) {
73              return false; // no privacy preferences, assume unset
74          }
75          UserSession userSession = GlobalVariables.getUserSession();
76  
77          boolean suppressAddress = privacy.isSuppressAddress();
78          return suppressAddress
79                  && userSession != null
80                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
81                  && !canOverrideEntityPrivacyPreferences(entityId);
82      }
83  
84      public static boolean isSuppressPhone(String entityId) {
85          EntityPrivacyPreferences privacy = getIdentityService().getEntityPrivacyPreferences(entityId);
86          if (privacy == null) {
87              return false; // no privacy preferences, assume unset
88          }
89          UserSession userSession = GlobalVariables.getUserSession();
90  
91          boolean suppressPhone = privacy.isSuppressPhone();
92          return suppressPhone
93                  && userSession != null
94                  && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
95                  && !canOverrideEntityPrivacyPreferences(entityId);
96      }
97  
98      public static boolean isSuppressPersonal(String entityId) {
99          EntityPrivacyPreferences privacy = getIdentityService().getEntityPrivacyPreferences(entityId);
100         if (privacy == null) {
101             return false; // no privacy preferences, assume unset
102         }
103         UserSession userSession = GlobalVariables.getUserSession();
104 
105         boolean suppressPersonal = privacy.isSuppressPersonal();
106         return suppressPersonal
107                 && userSession != null
108                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
109                 && !canOverrideEntityPrivacyPreferences(entityId);
110     }
111 
112     protected static boolean canOverrideEntityPrivacyPreferences( String entityId ){
113         List<Principal> principals = getIdentityService().getPrincipalsByEntityId(entityId);
114 
115         if (CollectionUtils.isEmpty(principals)) {
116             return false;
117         }
118         String principalId = principals.get(0).getPrincipalId();
119 		return getPermissionService().isAuthorized(
120 				GlobalVariables.getUserSession().getPrincipalId(),
121 				KimConstants.NAMESPACE_CODE,
122 				KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES,
123 				Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, principalId) );
124 	}
125 
126     private static IdentityService getIdentityService() {
127 		if ( identityService == null ) {
128 			identityService = KimApiServiceLocator.getIdentityService();
129 		}
130 		return identityService;
131 	}
132 
133     private static PermissionService getPermissionService() {
134 		if ( permissionService == null ) {
135 			permissionService = KimApiServiceLocator.getPermissionService();
136 		}
137 		return permissionService;
138     }
139 }