Coverage Report - org.kuali.rice.kim.service.impl.PermissionServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PermissionServiceImpl
0%
0/282
0%
0/146
3.19
PermissionServiceImpl$1
0%
0/7
0%
0/2
3.19
 
 1  
 /*
 2  
  * Copyright 2007-2008 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.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 21  
 import org.kuali.rice.kim.api.permission.Permission;
 22  
 import org.kuali.rice.kim.api.role.Role;
 23  
 import org.kuali.rice.kim.api.role.RoleMembership;
 24  
 import org.kuali.rice.kim.api.role.RoleService;
 25  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 26  
 import org.kuali.rice.kim.api.type.KimType;
 27  
 import org.kuali.rice.kim.bo.impl.PermissionImpl;
 28  
 import org.kuali.rice.kim.bo.role.dto.KimPermissionInfo;
 29  
 import org.kuali.rice.kim.bo.role.dto.KimPermissionTemplateInfo;
 30  
 import org.kuali.rice.kim.bo.role.dto.PermissionAssigneeInfo;
 31  
 import org.kuali.rice.kim.bo.role.impl.KimPermissionTemplateImpl;
 32  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 33  
 import org.kuali.rice.kim.dao.KimPermissionDao;
 34  
 import org.kuali.rice.kim.impl.permission.PermissionBo;
 35  
 import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
 36  
 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
 37  
 import org.kuali.rice.kim.service.PermissionService;
 38  
 import org.kuali.rice.kim.service.support.KimPermissionTypeService;
 39  
 import org.kuali.rice.kim.util.KIMWebServiceConstants;
 40  
 import org.kuali.rice.kim.util.KimConstants;
 41  
 import org.kuali.rice.kns.lookup.Lookupable;
 42  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 43  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 44  
 import org.kuali.rice.krad.lookup.CollectionIncomplete;
 45  
 import org.kuali.rice.krad.service.DataDictionaryService;
 46  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 47  
 import org.kuali.rice.krad.util.KRADPropertyConstants;
 48  
 
 49  
 import javax.jws.WebService;
 50  
 import java.util.ArrayList;
 51  
 import java.util.Collection;
 52  
 import java.util.Collections;
 53  
 import java.util.Comparator;
 54  
 import java.util.HashMap;
 55  
 import java.util.List;
 56  
 import java.util.Map;
 57  
 
 58  
 /**
 59  
  * This is a description of what this class does - jonathan don't forget to fill this in. 
 60  
  * 
 61  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 62  
  *
 63  
  */
 64  
 @WebService(endpointInterface = KIMWebServiceConstants.PermissionService.INTERFACE_CLASS, serviceName = KIMWebServiceConstants.PermissionService.WEB_SERVICE_NAME, portName = KIMWebServiceConstants.PermissionService.WEB_SERVICE_PORT, targetNamespace = KIMWebServiceConstants.MODULE_TARGET_NAMESPACE)
 65  0
 public class PermissionServiceImpl extends PermissionServiceBase implements PermissionService {
 66  0
         private static final Logger LOG = Logger.getLogger( PermissionServiceImpl.class );
 67  
 
 68  
            private RoleService roleService;
 69  
         private KimPermissionDao permissionDao;
 70  
     private KimPermissionTypeService defaultPermissionTypeService;
 71  
         
 72  
         private List<KimPermissionTemplateInfo> allTemplates;
 73  
         
 74  
     // --------------------
 75  
     // Authorization Checks
 76  
     // --------------------
 77  
     
 78  
         protected KimPermissionTypeService getPermissionTypeService( String namespaceCode, String permissionTemplateName, String permissionName, String permissionId ) {
 79  0
                 StringBuffer cacheKey = new StringBuffer();
 80  0
                 if ( namespaceCode != null ) {
 81  0
                         cacheKey.append( namespaceCode );
 82  
                 }
 83  0
                 cacheKey.append( '|' );
 84  0
                 if ( permissionTemplateName != null ) {
 85  0
                         cacheKey.append( permissionTemplateName );
 86  
                 }
 87  0
                 cacheKey.append( '|' );
 88  0
                 if ( permissionName != null ) {
 89  0
                         cacheKey.append( permissionName );
 90  
                 }
 91  0
                 cacheKey.append( '|' );
 92  0
                 if ( permissionId != null ) {
 93  0
                         cacheKey.append( permissionId );
 94  
                 }
 95  0
                 String key = cacheKey.toString();
 96  0
                 KimPermissionTypeService service = getPermissionTypeServiceByNameCache().get(key);
 97  0
                 if ( service == null ) {
 98  0
                         PermissionTemplateBo permTemplate = null;
 99  0
                         if ( permissionTemplateName != null ) {
 100  0
                                 List<PermissionBo> perms = getPermissionImplsByTemplateName(namespaceCode, permissionTemplateName);
 101  0
                 if ( !perms.isEmpty() ) {
 102  0
                                         permTemplate = perms.get(0).getTemplate();
 103  
                                 }
 104  0
                         } else if ( permissionName != null ) {
 105  0
                                 PermissionBo perms = getPermissionImplsByName(namespaceCode, permissionName);
 106  0
                                 permTemplate = perms.getTemplate();
 107  0
                         } else if ( permissionId != null ) {
 108  0
                                 PermissionBo perm = getPermissionImpl(permissionId);
 109  0
                                 if ( perm != null ) {
 110  0
                                         permTemplate = perm.getTemplate();
 111  
                                 }
 112  
                         }
 113  0
                         service = getPermissionTypeService( permTemplate );
 114  0
                         getPermissionTypeServiceByNameCache().put(key, service);
 115  
                 }
 116  0
                 return service;
 117  
         }
 118  
 
 119  
     protected KimPermissionTypeService getPermissionTypeService( PermissionTemplateBo permissionTemplate ) {
 120  0
             if ( permissionTemplate == null ) {
 121  0
                     throw new IllegalArgumentException( "permissionTemplate may not be null" );
 122  
             }
 123  0
             KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType( permissionTemplate.getKimTypeId() );
 124  0
             String serviceName = kimType.getServiceName();
 125  
             // if no service specified, return a default implementation
 126  0
             if ( StringUtils.isBlank( serviceName ) ) {
 127  0
                     return getDefaultPermissionTypeService();
 128  
             }
 129  
             try {
 130  0
                     Object service = KIMServiceLocatorInternal.getService(serviceName);
 131  
                     // if we have a service name, it must exist
 132  0
                     if ( service == null ) {
 133  0
                                 throw new RuntimeException("null returned for permission type service for service name: " + serviceName);
 134  
                     }
 135  
                     // whatever we retrieved must be of the correct type
 136  0
                     if ( !(service instanceof KimPermissionTypeService)  ) {
 137  0
                             throw new RuntimeException( "Service " + serviceName + " was not a KimPermissionTypeService.  Was: " + service.getClass().getName() );
 138  
                     }
 139  0
                     return (KimPermissionTypeService)service;
 140  0
             } catch( Exception ex ) {
 141  
                     // sometimes service locators throw exceptions rather than returning null, handle that
 142  0
                     throw new RuntimeException( "Error retrieving service: " + serviceName + " from the KIMServiceLocatorInternal.", ex );
 143  
             }
 144  
     }
 145  
     
 146  
     protected KimPermissionTypeService getDefaultPermissionTypeService() {
 147  0
             if ( defaultPermissionTypeService == null ) {
 148  0
                     defaultPermissionTypeService = (KimPermissionTypeService) KIMServiceLocatorInternal.getBean(DEFAULT_PERMISSION_TYPE_SERVICE);
 149  
             }
 150  0
                 return defaultPermissionTypeService;
 151  
         }
 152  
         
 153  
     /**
 154  
      * @see org.kuali.rice.kim.service.PermissionService#hasPermission(java.lang.String, String, java.lang.String, Map<String, String>)
 155  
      */
 156  
     public boolean hasPermission(String principalId, String namespaceCode, String permissionName, Map<String, String> permissionDetails) {
 157  0
             return isAuthorized( principalId, namespaceCode, permissionName, permissionDetails, null );
 158  
     }
 159  
 
 160  
     /**
 161  
      * @see org.kuali.rice.kim.service.PermissionService#isAuthorized( java.lang.String, String, java.lang.String, Map<String, String>, Map<String, String>)
 162  
      */
 163  
     public boolean isAuthorized(String principalId, String namespaceCode, String permissionName, Map<String, String> permissionDetails, Map<String, String> qualification ) {
 164  0
             List<String> roleIds = getRoleIdsForPermission( namespaceCode, permissionName, permissionDetails );
 165  0
             if ( roleIds.isEmpty() ) {
 166  0
                     return false;
 167  
             }
 168  0
                 return getRoleService().principalHasRole( principalId, roleIds, qualification );
 169  
     }
 170  
 
 171  
     /**
 172  
      * @see org.kuali.rice.kim.service.PermissionService#hasPermission(String, String, String, Map<String, String>)
 173  
      */
 174  
     public boolean hasPermissionByTemplateName(String principalId, String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails) {
 175  0
             return isAuthorizedByTemplateName( principalId, namespaceCode, permissionTemplateName, permissionDetails, null );
 176  
     }
 177  
 
 178  
     /**
 179  
      * @see org.kuali.rice.kim.service.PermissionService#isAuthorized( java.lang.String, String, java.lang.String, Map<String, String>, Map<String, String>)
 180  
      */
 181  
     public boolean isAuthorizedByTemplateName(String principalId, String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails, Map<String, String> qualification ) {
 182  0
             List<String> roleIds = getRoleIdsForPermissionTemplate( namespaceCode, permissionTemplateName, permissionDetails );
 183  0
             if ( roleIds.isEmpty() ) {
 184  0
                     return false;
 185  
             }
 186  0
             return getRoleService().principalHasRole( principalId, roleIds, qualification );
 187  
     }
 188  
 
 189  
     /**
 190  
      * @see org.kuali.rice.kim.service.PermissionService#getAuthorizedPermissions(String, String, String, Map<String, String>, Map<String, String>)
 191  
      */
 192  
     public List<Permission> getAuthorizedPermissions(String principalId, String namespaceCode, String permissionName, Map<String, String> permissionDetails, Map<String, String> qualification) {
 193  
             // get all the permission objects whose name match that requested
 194  0
             PermissionBo permissions = getPermissionImplsByName( namespaceCode, permissionName );
 195  
             // now, filter the full list by the detail passed
 196  0
             List<PermissionBo> applicablePermissions = getMatchingPermissions( Collections.singletonList(permissions), permissionDetails );
 197  0
             List<PermissionBo> permissionsForUser =
 198  
                 getPermissionsForUser(principalId, applicablePermissions, qualification);
 199  0
         List<Permission> returnList = new ArrayList<Permission>();
 200  0
         for (PermissionBo bo : permissionsForUser) { returnList.add(PermissionBo.to(bo));}
 201  0
         return returnList;
 202  
 
 203  
 
 204  
     }
 205  
 
 206  
     /**
 207  
      * @see org.kuali.rice.kim.service.PermissionService#getAuthorizedPermissionsByTemplateName(String, String, String, Map<String, String>, Map<String, String>)
 208  
      */
 209  
     public List<Permission> getAuthorizedPermissionsByTemplateName(String principalId, String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails, Map<String, String> qualification) {
 210  
             // get all the permission objects whose name match that requested
 211  0
             List<PermissionBo> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 212  
             // now, filter the full list by the detail passed
 213  
 
 214  0
             List<PermissionBo> applicablePermissions = getMatchingPermissions( permissions, permissionDetails );
 215  0
         List<PermissionBo> permissionsForUser = getPermissionsForUser(principalId, applicablePermissions,
 216  
                 qualification);
 217  0
         List<Permission> returnList = new ArrayList<Permission>();
 218  0
         for (PermissionBo bo : permissionsForUser) { returnList.add(PermissionBo.to(bo)); }
 219  0
         return returnList;
 220  
 
 221  
     }
 222  
     
 223  
     /**
 224  
      * Checks the list of permissions against the principal's roles and returns a subset of the list which match.
 225  
      */
 226  
     protected List<PermissionBo> getPermissionsForUser(String principalId, List<PermissionBo> permissions, Map<String, String> qualification) {
 227  0
             ArrayList<PermissionBo> results = new ArrayList<PermissionBo>();
 228  0
             List<PermissionBo> tempList = new ArrayList<PermissionBo>(1);
 229  0
             for ( PermissionBo perm : permissions ) {
 230  0
                     tempList.clear();
 231  0
                     tempList.add( perm );
 232  0
                     List<String> roleIds = permissionDao.getRoleIdsForPermissions( tempList );
 233  
                     // TODO: This could be made a little better by collecting the role IDs into
 234  
                     // a set and then processing the distinct list rather than a check
 235  
                     // for every permission
 236  0
                     if ( roleIds != null && !roleIds.isEmpty() ) {
 237  0
                             if ( getRoleService().principalHasRole( principalId, roleIds, qualification)) {
 238  0
                                     results.add( perm );
 239  
                             }
 240  
                     }
 241  0
             }
 242  
             
 243  0
             return results;            
 244  
     }
 245  
 
 246  
     protected Map<String,KimPermissionTypeService> getPermissionTypeServicesByTemplateId( Collection<PermissionBo> permissions ) {
 247  0
             Map<String,KimPermissionTypeService> permissionTypeServices = new HashMap<String, KimPermissionTypeService>( permissions.size() );
 248  0
             for ( PermissionBo perm : permissions ) {
 249  0
                     permissionTypeServices.put(perm.getTemplateId(), getPermissionTypeService( perm.getTemplate() ) );                                    
 250  
             }
 251  0
             return permissionTypeServices;
 252  
     }
 253  
     
 254  
     protected Map<String, List<PermissionBo>> groupPermissionsByTemplate(Collection<PermissionBo> permissions) {
 255  0
             Map<String, List<PermissionBo>> results = new HashMap<String, List<PermissionBo>>();
 256  0
             for ( PermissionBo perm : permissions ) {
 257  0
                     List<PermissionBo> perms = results.get(perm.getTemplateId());
 258  0
                     if ( perms == null ) {
 259  0
                             perms = new ArrayList<PermissionBo>();
 260  0
                             results.put( perm.getTemplateId(), perms );
 261  
                     }
 262  0
                     perms.add( perm);
 263  0
             }
 264  0
             return results;
 265  
     }
 266  
     
 267  
         /**
 268  
      * Compare each of the passed in permissions with the given permissionDetails.  Those that
 269  
      * match are added to the result list.
 270  
      */
 271  
     protected List<PermissionBo> getMatchingPermissions(List<PermissionBo> permissions, Map<String, String> permissionDetails) {
 272  0
             List<PermissionBo> applicablePermissions = new ArrayList<PermissionBo>();
 273  0
             if ( permissionDetails == null || permissionDetails.isEmpty() ) {
 274  
                     // if no details passed, assume that all match
 275  0
                     for ( PermissionBo perm : permissions ) {
 276  0
                             if (perm != null) {
 277  0
                     applicablePermissions.add( perm );
 278  
                 }
 279  
                     }
 280  
             } else {
 281  
                     // otherwise, attempt to match the permission details
 282  
                     // build a map of the template IDs to the type services
 283  0
                     Map<String,KimPermissionTypeService> permissionTypeServices = getPermissionTypeServicesByTemplateId( permissions );
 284  
                     // build a map of permissions by template ID
 285  0
                     Map<String, List<PermissionBo>> permissionMap = groupPermissionsByTemplate(permissions);
 286  
                     // loop over the different templates, matching all of the same template against the type
 287  
                     // service at once
 288  0
                     for ( String templateId : permissionMap.keySet() ) {
 289  0
                             KimPermissionTypeService permissionTypeService = permissionTypeServices.get( templateId );
 290  0
                             List<PermissionBo> permissionList = permissionMap.get(templateId);
 291  0
                 List<Permission> immutablePermissionList = new ArrayList<Permission>();
 292  0
                 for (PermissionBo bo : permissionList) {immutablePermissionList.add(PermissionBo.to(bo));}
 293  0
                 List<Permission> matchingPermissions = permissionTypeService.getMatchingPermissions(
 294  
                         permissionDetails, immutablePermissionList );
 295  0
                 List<PermissionBo> matchingPermissionBos = new ArrayList<PermissionBo>();
 296  0
                 for (Permission perm : matchingPermissions) {matchingPermissionBos.add(PermissionBo.from(perm)); }
 297  0
                                 applicablePermissions.addAll(matchingPermissionBos);
 298  
 
 299  0
                     }
 300  
             }
 301  0
             return applicablePermissions;
 302  
     }
 303  
 
 304  
     /**
 305  
      * @see org.kuali.rice.kim.service.PermissionService#getPermissionAssignees(String, String, Map<String, String>, Map<String, String>)
 306  
      */
 307  
     public List<PermissionAssigneeInfo> getPermissionAssignees( String namespaceCode, String permissionName, Map<String, String> permissionDetails, Map<String, String> qualification ) {
 308  0
             List<PermissionAssigneeInfo> results = new ArrayList<PermissionAssigneeInfo>();
 309  0
             List<String> roleIds = getRoleIdsForPermission( namespaceCode, permissionName, permissionDetails);
 310  0
             if ( roleIds.isEmpty() ) {
 311  0
                     return results;
 312  
             }
 313  0
             Collection<RoleMembership> roleMembers = getRoleService().getRoleMembers( roleIds,
 314  
                 qualification );
 315  0
             for ( RoleMembership rm : roleMembers ) {
 316  0
                     if ( rm.getMemberTypeCode().equals( Role.PRINCIPAL_MEMBER_TYPE ) ) {
 317  0
                             results.add( new PermissionAssigneeInfo( rm.getMemberId(), null, rm.getDelegates() ) );
 318  0
                     } else if ( rm.getMemberTypeCode().equals( Role.GROUP_MEMBER_TYPE ) ) {
 319  0
                             results.add( new PermissionAssigneeInfo( null, rm.getMemberId(), rm.getDelegates() ) );
 320  
                     }
 321  
             }
 322  0
             return results;
 323  
     }
 324  
     
 325  
     public List<PermissionAssigneeInfo> getPermissionAssigneesForTemplateName( String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails, Map<String, String> qualification ) {
 326  0
             List<PermissionAssigneeInfo> results = new ArrayList<PermissionAssigneeInfo>();
 327  0
             List<String> roleIds = getRoleIdsForPermissionTemplate( namespaceCode, permissionTemplateName, permissionDetails);
 328  0
             if ( roleIds.isEmpty() ) {
 329  0
                     return results;
 330  
             }
 331  0
             Collection<RoleMembership> roleMembers = getRoleService().getRoleMembers( roleIds,
 332  
                 qualification);
 333  0
             for ( RoleMembership rm : roleMembers ) {
 334  0
                     if ( rm.getMemberTypeCode().equals( Role.PRINCIPAL_MEMBER_TYPE ) ) {
 335  0
                             results.add( new PermissionAssigneeInfo( rm.getMemberId(), null, rm.getDelegates() ) );
 336  
                     } else { // a group membership
 337  0
                             results.add( new PermissionAssigneeInfo( null, rm.getMemberId(), rm.getDelegates() ) );
 338  
                     }
 339  
             }
 340  0
             return results;
 341  
     }
 342  
     
 343  
     public boolean isPermissionAssigned( String namespaceCode, String permissionName, Map<String, String> permissionDetails ) {
 344  0
             return !getRoleIdsForPermission(namespaceCode, permissionName, permissionDetails).isEmpty();
 345  
     }
 346  
     
 347  
     public boolean isPermissionDefined( String namespaceCode, String permissionName, Map<String, String> permissionDetails ) {
 348  
             // get all the permission objects whose name match that requested
 349  0
             PermissionBo permissions = getPermissionImplsByName( namespaceCode, permissionName );
 350  
             // now, filter the full list by the detail passed
 351  0
             return !getMatchingPermissions( Collections.singletonList(permissions), permissionDetails ).isEmpty();
 352  
     }
 353  
     
 354  
     public boolean isPermissionDefinedForTemplateName( String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails ) {
 355  
             // get all the permission objects whose name match that requested
 356  0
             List<PermissionBo> permission = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 357  
             // now, filter the full list by the detail passed
 358  0
         if (permission != null) {
 359  0
                 return !getMatchingPermissions( permission, permissionDetails ).isEmpty();
 360  
         }
 361  0
         return false;
 362  
     }
 363  
  
 364  
     public List<String> getRoleIdsForPermission( String namespaceCode, String permissionName, Map<String, String> permissionDetails) {
 365  
             // get all the permission objects whose name match that requested
 366  0
             PermissionBo permissions = getPermissionImplsByName( namespaceCode, permissionName );
 367  
             // now, filter the full list by the detail passed
 368  0
             List<PermissionBo> applicablePermissions = getMatchingPermissions( Collections.singletonList( permissions ), permissionDetails );
 369  0
             List<String> roleIds = getRolesForPermissionsFromCache( applicablePermissions );
 370  0
             if ( roleIds == null ) {
 371  0
                     roleIds = permissionDao.getRoleIdsForPermissions( applicablePermissions );
 372  0
                     addRolesForPermissionsToCache( applicablePermissions, roleIds );
 373  
             }
 374  0
             return roleIds;            
 375  
     }
 376  
 
 377  
     protected List<String> getRoleIdsForPermissionTemplate( String namespaceCode, String permissionTemplateName, Map<String, String> permissionDetails ) {
 378  
             // get all the permission objects whose name match that requested
 379  0
             List<PermissionBo> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 380  
             // now, filter the full list by the detail passed
 381  0
             List<PermissionBo> applicablePermissions = getMatchingPermissions( permissions, permissionDetails );
 382  0
             List<String> roleIds = getRolesForPermissionsFromCache( applicablePermissions );
 383  0
             if ( roleIds == null ) {
 384  0
                     roleIds = permissionDao.getRoleIdsForPermissions( applicablePermissions );
 385  0
                     addRolesForPermissionsToCache( applicablePermissions, roleIds );
 386  
             }
 387  0
             return roleIds;
 388  
     }
 389  
 
 390  
     @Override
 391  
     public List<String> getRoleIdsForPermissions( List<Permission> permissions ) {
 392  0
         List<PermissionBo> permBos = new ArrayList<PermissionBo>();
 393  0
         for (Permission perm : permissions) { permBos.add(PermissionBo.from(perm));}
 394  0
             List<String> roleIds = getRolesForPermissionsFromCache( permBos );
 395  0
             if ( roleIds == null ) {
 396  0
                     roleIds = permissionDao.getRoleIdsForPermissions( permBos );
 397  0
                     addRolesForPermissionsToCache( permBos, roleIds );
 398  
             }
 399  0
             return roleIds;
 400  
     }
 401  
 
 402  
     // --------------------
 403  
     // Permission Data
 404  
     // --------------------
 405  
     
 406  
     /**
 407  
      * @see org.kuali.rice.kim.service.PermissionService#getPermission(java.lang.String)
 408  
      */
 409  
     public Permission getPermission(String permissionId) {
 410  0
             PermissionBo impl = getPermissionImpl( permissionId );
 411  0
             if ( impl != null ) {
 412  0
                     return PermissionBo.to(impl);
 413  
             }
 414  0
             return null;
 415  
     }
 416  
     
 417  
     /**
 418  
      * @see org.kuali.rice.kim.service.PermissionService#getPermissionsByTemplateName(String, String)
 419  
      */
 420  
     public List<Permission> getPermissionsByTemplateName(String namespaceCode, String permissionTemplateName) {
 421  0
         List<PermissionBo> impls = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 422  0
         List<Permission> permissions = new ArrayList<Permission>(impls.size());
 423  0
         for (PermissionBo impl : impls) {
 424  0
             permissions.add(PermissionBo.to(impl));
 425  
         }
 426  0
             return permissions;
 427  
     }
 428  
 
 429  
         /**
 430  
      * @see org.kuali.rice.kim.service.PermissionService#getPermissionsByName(String, String)
 431  
      */
 432  
 
 433  
     public Permission getPermissionsByName(String namespaceCode, String permissionName) {
 434  0
             PermissionBo impls = getPermissionImplsByName( namespaceCode, permissionName );
 435  0
         return PermissionBo.to(impls);
 436  
     }
 437  
     
 438  
     @SuppressWarnings("unchecked")
 439  
         protected PermissionBo getPermissionImpl(String permissionId) {
 440  0
             if ( StringUtils.isBlank( permissionId ) ) {
 441  0
                     return null;
 442  
             }
 443  0
             String cacheKey = getPermissionImplByIdCacheKey(permissionId);
 444  0
             List<PermissionBo> permissions = (List<PermissionBo>)getCacheAdministrator().getFromCache(cacheKey, getRefreshPeriodInSeconds());
 445  0
             if ( permissions == null ) {
 446  0
                     HashMap<String,Object> pk = new HashMap<String,Object>( 1 );
 447  0
                     pk.put( KimConstants.PrimaryKeyConstants.ID, permissionId );
 448  0
                     permissions = Collections.singletonList(getBusinessObjectService().findByPrimaryKey( PermissionBo.class, pk ) );
 449  0
                     getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 450  
             }
 451  0
             return permissions.get( 0 );
 452  
     }
 453  
     
 454  
     @SuppressWarnings("unchecked")
 455  
         protected List<PermissionBo> getPermissionImplsByTemplateName( String namespaceCode, String permissionTemplateName ) {
 456  0
             String cacheKey = getPermissionImplByTemplateNameCacheKey(namespaceCode, permissionTemplateName);
 457  0
             List<PermissionBo> permissions = (List<PermissionBo>)getCacheAdministrator().getFromCache(cacheKey, getRefreshPeriodInSeconds());
 458  0
             if ( permissions == null ) {
 459  0
                     HashMap<String,Object> pk = new HashMap<String,Object>( 3 );
 460  0
                     pk.put( "template.namespaceCode", namespaceCode );
 461  0
                     pk.put( "template.name", permissionTemplateName );
 462  0
                         pk.put( KRADPropertyConstants.ACTIVE, "Y" );
 463  0
                     permissions = new ArrayList<PermissionBo>(getBusinessObjectService().findMatching( PermissionBo.class, pk ));
 464  0
                     getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 465  
             }
 466  0
             return permissions;
 467  
     }
 468  
 
 469  
     @SuppressWarnings("unchecked")
 470  
         protected PermissionBo getPermissionImplsByName( String namespaceCode, String permissionName ) {
 471  0
             String cacheKey = getPermissionImplByNameCacheKey(namespaceCode, permissionName);
 472  0
             PermissionBo permissions = (PermissionBo)getCacheAdministrator().getFromCache(cacheKey, getRefreshPeriodInSeconds());
 473  0
             if ( permissions == null ) {
 474  0
                     HashMap<String,Object> pk = new HashMap<String,Object>( 3 );
 475  0
                     pk.put( KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode );
 476  0
                     pk.put( KimConstants.UniqueKeyConstants.PERMISSION_NAME, permissionName );
 477  0
                         pk.put( KRADPropertyConstants.ACTIVE, "Y" );
 478  0
             Collection<PermissionBo> c = getBusinessObjectService().findMatching( PermissionBo.class, pk );
 479  0
                     permissions = c != null && c.iterator().hasNext() ? c.iterator().next() : null;
 480  0
                     getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 481  
             }
 482  0
             return permissions;
 483  
     }
 484  
 
 485  
     
 486  
     
 487  
     // --------------------
 488  
     // Support Methods
 489  
     // --------------------
 490  
         
 491  
         
 492  
         protected RoleService getRoleService() {
 493  0
                 if ( roleService == null ) {
 494  0
                         roleService = KimApiServiceLocator.getRoleService();
 495  
                 }
 496  
 
 497  0
                 return roleService;
 498  
         }
 499  
 
 500  
         public void setRoleService(RoleService roleService) {
 501  0
                 this.roleService = roleService;
 502  0
         }
 503  
 
 504  
         public KimPermissionDao getPermissionDao() {
 505  0
                 return this.permissionDao;
 506  
         }
 507  
 
 508  
         public void setPermissionDao(KimPermissionDao permissionDao) {
 509  0
                 this.permissionDao = permissionDao;
 510  0
         }
 511  
 
 512  
         @SuppressWarnings("unchecked")
 513  
         public List<Permission> lookupPermissions(Map<String, String> searchCriteria, boolean unbounded){
 514  0
                 Collection baseResults = null;
 515  0
                 Lookupable permissionLookupable = KNSServiceLocator.getLookupable(
 516  
                 KRADServiceLocatorWeb.getBusinessObjectDictionaryService().getLookupableID(PermissionImpl.class));
 517  0
                 permissionLookupable.setBusinessObjectClass(PermissionImpl.class);
 518  0
                 if ( unbounded ) {
 519  0
                     baseResults = permissionLookupable.getSearchResultsUnbounded( searchCriteria );
 520  
                 } else {
 521  0
                         baseResults = permissionLookupable.getSearchResults(searchCriteria);
 522  
                 }
 523  0
                 List<Permission> results = new ArrayList<Permission>( baseResults.size() );
 524  0
                 for ( PermissionBo resp : (Collection<PermissionBo>)baseResults ) {
 525  0
                         results.add( PermissionBo.to(resp) );
 526  
                 }
 527  0
                 if ( baseResults instanceof CollectionIncomplete ) {
 528  0
                         results = new CollectionIncomplete<Permission>( results, ((CollectionIncomplete<KimPermissionInfo>)baseResults).getActualSizeIfTruncated() );
 529  
                 }                
 530  0
                 return results;
 531  
         }
 532  
 
 533  
         public String getPermissionDetailLabel( String permissionId, String kimTypeId, String attributeName) {
 534  
             // get the type service for this permission
 535  0
                 KimPermissionTypeService typeService = getPermissionTypeService(null, null, null, permissionId);
 536  0
                 if ( typeService != null ) {
 537  
                         // ask the type service for the attribute definition for the given attribute name
 538  0
                         AttributeDefinitionMap attributes = typeService.getAttributeDefinitions( kimTypeId );
 539  0
                         String label = null;
 540  0
                         for ( AttributeDefinition attributeDef : attributes.values() ) {
 541  0
                                 if ( attributeDef.getName().equals(attributeName) ) {
 542  0
                                         label = attributeDef.getLabel();
 543  
                                 }
 544  
                         }
 545  
                         // return the attribute label
 546  0
                         if ( label != null ) {
 547  0
                                 return label;
 548  
                         } else {
 549  0
                                 return "Missing Def: " + attributeName;
 550  
                         }
 551  
                 } else {
 552  0
                         return "No Label: " + attributeName;
 553  
                 }
 554  
         }
 555  
         
 556  
         /**
 557  
          * @see org.kuali.rice.kim.service.PermissionService#getPermissionTemplate(java.lang.String)
 558  
          */
 559  
         public KimPermissionTemplateInfo getPermissionTemplate(String permissionTemplateId) {
 560  0
                 KimPermissionTemplateImpl impl = getBusinessObjectService().findBySinglePrimaryKey( KimPermissionTemplateImpl.class, permissionTemplateId );
 561  0
                 if ( impl != null ) {
 562  0
                         return impl.toSimpleInfo();
 563  
                 }
 564  0
                 return null;
 565  
         }
 566  
 
 567  
         /**
 568  
          * This overridden method ...
 569  
          * 
 570  
          * @see org.kuali.rice.kim.service.PermissionService#getPermissionTemplateByName(java.lang.String, java.lang.String)
 571  
          */
 572  
         public KimPermissionTemplateInfo getPermissionTemplateByName(String namespaceCode,
 573  
                         String permissionTemplateName) {
 574  0
                 Map<String,String> criteria = new HashMap<String,String>(2);
 575  0
                 criteria.put( KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode );
 576  0
                 criteria.put( KimConstants.UniqueKeyConstants.PERMISSION_TEMPLATE_NAME, permissionTemplateName );
 577  0
                 KimPermissionTemplateImpl impl = (KimPermissionTemplateImpl)getBusinessObjectService().findByPrimaryKey( KimPermissionTemplateImpl.class, criteria );
 578  0
                 if ( impl != null ) {
 579  0
                         return impl.toSimpleInfo();
 580  
                 }
 581  0
                 return null;
 582  
         }
 583  
         
 584  
         @SuppressWarnings("unchecked")
 585  
         public List<KimPermissionTemplateInfo> getAllTemplates() {
 586  0
                 if ( allTemplates == null ) {
 587  0
                         Map<String,String> criteria = new HashMap<String,String>(1);
 588  0
                         criteria.put( KRADPropertyConstants.ACTIVE, "Y" );
 589  0
                         List<KimPermissionTemplateImpl> impls = (List<KimPermissionTemplateImpl>)getBusinessObjectService().findMatching( KimPermissionTemplateImpl.class, criteria );
 590  0
                         List<KimPermissionTemplateInfo> infos = new ArrayList<KimPermissionTemplateInfo>( impls.size() );
 591  0
                         for ( KimPermissionTemplateImpl impl : impls ) {
 592  0
                                 infos.add( impl.toSimpleInfo() );
 593  
                         }
 594  0
                         Collections.sort(infos, new Comparator<KimPermissionTemplateInfo>() {
 595  
                                 public int compare(KimPermissionTemplateInfo tmpl1,
 596  
                                                 KimPermissionTemplateInfo tmpl2) {
 597  0
                                         int result = 0;
 598  0
                                         result = tmpl1.getNamespaceCode().compareTo(tmpl2.getNamespaceCode());
 599  0
                                         if ( result != 0 ) {
 600  0
                                                 return result;
 601  
                                         }
 602  0
                                         result = tmpl1.getName().compareTo(tmpl2.getName());
 603  0
                                         return result;
 604  
                                 }
 605  
                         });
 606  0
                         allTemplates = infos;
 607  
                 }
 608  0
                 return allTemplates;
 609  
         }
 610  
 
 611  
     
 612  
         
 613  
         private DataDictionaryService dataDictionaryService;
 614  
         protected DataDictionaryService getDataDictionaryService() {
 615  0
                 if(dataDictionaryService == null){
 616  0
                         dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
 617  
                 }
 618  0
                 return dataDictionaryService;
 619  
         }
 620  
         
 621  
 
 622  
     public List<String> getRoleIdsForPermissionId(String permissionId) {
 623  0
         Permission permissionInfo = getPermission(permissionId);
 624  
 
 625  0
         List<PermissionBo> applicablePermissions = new ArrayList<PermissionBo>();
 626  0
         applicablePermissions.add(PermissionBo.from(permissionInfo));
 627  
 
 628  0
         List<String> roleIds = getRolesForPermissionsFromCache(applicablePermissions);
 629  0
         if (roleIds == null) {
 630  0
             roleIds = permissionDao.getRoleIdsForPermissions(applicablePermissions);
 631  0
             addRolesForPermissionsToCache(applicablePermissions, roleIds);
 632  
         }
 633  
 
 634  0
         return roleIds;
 635  
     }
 636  
 
 637  
     public Permission getPermissionsByNameIncludingInactive(String namespaceCode, String permissionName) {
 638  0
         PermissionBo impls = getPermissionImplsByNameIncludingInactive(namespaceCode, permissionName);
 639  0
         return PermissionBo.to(impls);
 640  
     }
 641  
         
 642  
     @SuppressWarnings("unchecked")
 643  
     protected PermissionBo getPermissionImplsByNameIncludingInactive(String namespaceCode, String permissionName) {
 644  0
         String cacheKey = getPermissionImplByNameCacheKey(namespaceCode, permissionName + "inactive");
 645  0
         PermissionBo permissions = (PermissionBo) getCacheAdministrator().getFromCache(cacheKey, getRefreshPeriodInSeconds());
 646  0
         if (permissions == null) {
 647  0
             HashMap<String, Object> pk = new HashMap<String, Object>(2);
 648  0
             pk.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode);
 649  0
             pk.put(KimConstants.UniqueKeyConstants.PERMISSION_NAME, permissionName);
 650  0
             Collection<PermissionBo> c = getBusinessObjectService().findMatching( PermissionBo.class, pk );
 651  0
                     permissions = c != null && c.iterator().hasNext() ? c.iterator().next() : null;
 652  0
             getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 653  
         }
 654  0
         return permissions;
 655  
     }
 656  
     
 657  
     public int getRefreshPeriodInSeconds() {
 658  
         try {
 659  0
             return (int)(new Integer(ConfigContext.getCurrentContextConfig().getProperty(KimConstants.CacheRefreshPeriodSeconds.KIM_CACHE_PERMISSION_REFRESH_PERIOD_SECONDS)));
 660  0
         } catch (NumberFormatException e) {
 661  
                     // The cache will never expire when refreshPeriod is set to -1
 662  0
                     return -1;                        
 663  
         }
 664  
     }
 665  
 }