View Javadoc

1   /**
2    * Copyright 2005-2015 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.api.cache;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.kim.api.permission.Permission;
21  import org.kuali.rice.kim.api.permission.PermissionService;
22  import org.kuali.rice.kim.api.responsibility.Responsibility;
23  import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
24  import org.kuali.rice.kim.api.role.RoleService;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  
31  
32  public final class KimCacheUtils {
33  
34      private KimCacheUtils() {
35          throw new UnsupportedOperationException();
36      }
37  
38      /**
39       * Used for a caching condition to determine if a role passed to a method is derived or not.
40       *
41       * @param roleIds list of role id values
42       *
43       * @return true if list contains a derived role.
44       */
45      public static boolean isDynamicRoleMembership(List<String> roleIds) {
46          if (CollectionUtils.isEmpty(roleIds)) {
47              return false;
48          }
49          RoleService roleService = KimApiServiceLocator.getRoleService();
50          for (String roleId : roleIds) {
51               if (roleService.isDynamicRoleMembership(roleId)) {
52                   return true;
53               }
54          }
55          return false;
56      }
57  
58      /**
59       * Used for a caching condition to determine if a role passed to a method is derived or not.
60       *
61       * @param namespaceCode namespaceCode of role
62       * @param roleName name of role
63       *
64       * @return true if list contains role.
65       */
66      public static boolean isDynamicMembshipRoleByNamespaceAndName(String namespaceCode, String roleName) {
67          List<String> roleIds = Collections.singletonList(
68                  KimApiServiceLocator.getRoleService().getRoleIdByNamespaceCodeAndName(namespaceCode, roleName));
69          return isDynamicRoleMembership(roleIds);
70      }
71  
72      /**
73       * Used for a caching condition to determine if a responsibility is assigned to a derived role.
74       *
75       * @param responsibilityId id of responsibility
76       *
77       * @return true if assigned to a derived role.
78       */
79      public static boolean isResponsibilityIdAssignedToDynamicRole(String responsibilityId) {
80          if ( StringUtils.isBlank(responsibilityId)) {
81              return false;
82          }
83  
84          List<String> roleIds = KimApiServiceLocator.getResponsibilityService().getRoleIdsForResponsibility(responsibilityId);
85          return isDynamicRoleMembership(roleIds);
86      }
87  
88      /**
89       * Used for a caching condition to determine if a responsibility is assigned to a derived role.
90       *
91       * @param namespaceCode namespaceCode of responsibility
92       * @param responsibilityName name of responsibility
93       *
94       * @return true if assigned to a derived role.
95       */
96      public static boolean isResponsibilityAssignedToDynamicRole(String namespaceCode, String responsibilityName) {
97          if (StringUtils.isBlank(namespaceCode) || StringUtils.isBlank(responsibilityName)) {
98              return false;
99          }
100         Responsibility responsibility = KimApiServiceLocator.getResponsibilityService().findRespByNamespaceCodeAndName(namespaceCode, responsibilityName);
101 
102         if (responsibility != null) {
103             return isResponsibilityIdAssignedToDynamicRole(responsibility.getId());
104         }
105         return false;
106     }
107 
108     /**
109      * Used for a caching condition to determine if a responsibility template is assigned to a derived role.
110      *
111      * @param namespaceCode namespaceCode of permission
112      * @param responsibilityTemplateName name of responsibility template
113      *
114      * @return true if assigned to a derived role.
115      */
116     public static boolean isResponsibilityTemplateAssignedToDynamicRole(String namespaceCode,
117             String responsibilityTemplateName) {
118         if (StringUtils.isBlank(namespaceCode) || StringUtils.isBlank(responsibilityTemplateName)) {
119             return false;
120         }
121         ResponsibilityService respService = KimApiServiceLocator.getResponsibilityService();
122         List<Responsibility> responsibilities = KimApiServiceLocator.getResponsibilityService().findResponsibilitiesByTemplate(namespaceCode, responsibilityTemplateName);
123         List<String> roleIds = new ArrayList<String>();
124         for (Responsibility resp : responsibilities) {
125             roleIds.addAll(respService.getRoleIdsForResponsibility(resp.getId()));
126         }
127 
128         return isDynamicRoleMembership(roleIds);
129     }
130 
131 }