1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.impl.responsibility;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.util.RiceKeyConstants;
20 import org.kuali.rice.kim.api.KimConstants;
21 import org.kuali.rice.kim.api.responsibility.Responsibility;
22 import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
23 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
24 import org.kuali.rice.kns.rules.DocumentRuleBase;
25 import org.kuali.rice.krad.util.GlobalVariables;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30
31
32
33
34
35
36 public class KimDocumentResponsibilityRule extends DocumentRuleBase implements AddResponsibilityRule {
37
38 public static final String ERROR_PATH = "document.responsibility.responsibilityId";
39
40 public boolean processAddResponsibility(AddResponsibilityEvent addResponsibilityEvent) {
41 KimDocumentRoleResponsibility newResponsibility = addResponsibilityEvent.getResponsibility();
42 if(newResponsibility==null){
43 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
44 return false;
45 }
46
47 ResponsibilityBo kimResponsibilityImpl = newResponsibility.getKimResponsibility();
48 if(kimResponsibilityImpl==null){
49 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
50 return false;
51 }
52
53 IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addResponsibilityEvent.getDocument();
54 boolean rulePassed = true;
55 if (!hasPermissionToGrantResponsibility(ResponsibilityBo.to(kimResponsibilityImpl), document)) {
56 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_RESPONSIBILITY,
57 new String[] {kimResponsibilityImpl.getNamespaceCode(), kimResponsibilityImpl.getTemplate().getName()});
58 return false;
59 }
60
61 if (newResponsibility == null || StringUtils.isBlank(newResponsibility.getResponsibilityId())) {
62 rulePassed = false;
63 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
64 } else {
65 int i = 0;
66 for (KimDocumentRoleResponsibility responsibility: document.getResponsibilities()) {
67 if (responsibility.getResponsibilityId().equals(newResponsibility.getResponsibilityId())) {
68 rulePassed = false;
69 GlobalVariables.getMessageMap().putError("document.responsibilities["+i+"].responsibilityId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Responsibility"});
70 }
71 i++;
72 }
73 }
74 return rulePassed;
75 }
76
77 public boolean hasPermissionToGrantResponsibility(Responsibility kimResponsibilityInfo, IdentityManagementRoleDocument document){
78 Map<String,String> responsibilityDetails = new HashMap<String,String>();
79 responsibilityDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, kimResponsibilityInfo.getNamespaceCode());
80 responsibilityDetails.put(KimConstants.AttributeConstants.RESPONSIBILITY_NAME, kimResponsibilityInfo.getName());
81 if (!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
82 document,
83 KimConstants.NAMESPACE_CODE,
84 KimConstants.PermissionTemplateNames.GRANT_RESPONSIBILITY,
85 GlobalVariables.getUserSession().getPerson().getPrincipalId(),
86 responsibilityDetails, null)) {
87 return false;
88 }
89 return true;
90 }
91 }