Coverage Report - org.kuali.rice.kim.rules.ui.KimDocumentPermissionRule
 
Classes in this File Line Coverage Branch Coverage Complexity
KimDocumentPermissionRule
0%
0/30
0%
0/18
7.5
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.rules.ui;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.kuali.rice.core.util.RiceKeyConstants;
 23  
 import org.kuali.rice.kim.api.permission.Permission;
 24  
 import org.kuali.rice.kim.bo.ui.KimDocumentRolePermission;
 25  
 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
 26  
 import org.kuali.rice.kim.rule.event.ui.AddPermissionEvent;
 27  
 import org.kuali.rice.kim.rule.ui.AddPermissionRule;
 28  
 import org.kuali.rice.kim.util.KimConstants;
 29  
 import org.kuali.rice.krad.rules.DocumentRuleBase;
 30  
 import org.kuali.rice.krad.util.GlobalVariables;
 31  
 
 32  
 /**
 33  
  * This is a description of what this class does - shyu don't forget to fill this in. 
 34  
  * 
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  *
 37  
  */
 38  0
 public class KimDocumentPermissionRule extends DocumentRuleBase implements AddPermissionRule {
 39  
 
 40  
         public static final String ERROR_PATH = "document.permission.permissionId";
 41  
         
 42  
         public boolean processAddPermission(AddPermissionEvent addPermissionEvent) {
 43  0
                 KimDocumentRolePermission newPermission = addPermissionEvent.getPermission();
 44  0
                 if(newPermission==null || StringUtils.isEmpty(newPermission.getPermissionId())){
 45  0
                         GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Permission"});
 46  0
                         return false;
 47  
                 }
 48  
 
 49  0
                 Permission kimPermissionInfo = newPermission.getPermission();
 50  0
                 if(kimPermissionInfo==null){
 51  0
                         GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Permission"});
 52  0
                         return false;
 53  
                 }
 54  0
             boolean rulePassed = true;
 55  0
                 IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addPermissionEvent.getDocument();
 56  0
                 if(!hasPermissionToGrantPermission(kimPermissionInfo, document)){
 57  0
                 GlobalVariables.getMessageMap().putError(KimDocumentPermissionRule.ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_PERMISSION, 
 58  
                                 new String[] {kimPermissionInfo.getNamespaceCode(), kimPermissionInfo.getTemplate().getName()});
 59  0
                 return false;
 60  
                 }
 61  
 
 62  0
                 if (newPermission == null || StringUtils.isBlank(newPermission.getPermissionId())) {
 63  0
             rulePassed = false;
 64  0
             GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Permission"});
 65  
         } else {
 66  0
                     int i = 0;
 67  0
                 for (KimDocumentRolePermission permission: document.getPermissions()) {
 68  0
                             if (permission.getPermissionId().equals(newPermission.getPermissionId())) {
 69  0
                             rulePassed = false;
 70  0
                             GlobalVariables.getMessageMap().putError("document.permissions["+i+"].permissionId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Permission"});
 71  
                             }
 72  0
                             i++;
 73  
                     }
 74  
         }
 75  0
                 return rulePassed;
 76  
         } 
 77  
 
 78  
         public boolean hasPermissionToGrantPermission(Permission kimPermissionInfo , IdentityManagementRoleDocument document){
 79  0
                 Map<String,String> permissionDetails = new HashMap<String,String>();
 80  0
                 permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, kimPermissionInfo.getNamespaceCode());
 81  0
                 permissionDetails.put(KimConstants.AttributeConstants.PERMISSION_NAME, kimPermissionInfo.getTemplate().getName());
 82  0
                 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 83  
                                 document, 
 84  
                                 KimConstants.NAMESPACE_CODE, 
 85  
                                 KimConstants.PermissionTemplateNames.GRANT_PERMISSION, 
 86  
                                 GlobalVariables.getUserSession().getPerson().getPrincipalId(), 
 87  
                                 permissionDetails, null)) {
 88  0
                 return false;
 89  
                 }
 90  0
                 return true;
 91  
         }
 92  
         
 93  
 }