1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
34 | |
|
35 | |
|
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 | |
} |