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