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.services.KimApiServiceLocator; |
24 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentMember; |
25 | |
import org.kuali.rice.kim.document.IdentityManagementGroupDocument; |
26 | |
import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent; |
27 | |
import org.kuali.rice.kim.rule.ui.AddGroupMemberRule; |
28 | |
import org.kuali.rice.kim.api.group.GroupService; |
29 | |
|
30 | |
import org.kuali.rice.kim.util.KimConstants; |
31 | |
import org.kuali.rice.krad.rules.DocumentRuleBase; |
32 | |
import org.kuali.rice.krad.util.GlobalVariables; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class GroupDocumentMemberRule extends DocumentRuleBase implements AddGroupMemberRule { |
41 | |
|
42 | |
private static final String ERROR_PATH = "document.member.memberId"; |
43 | |
|
44 | |
public boolean processAddGroupMember(AddGroupMemberEvent addGroupMemberEvent){ |
45 | 0 | GroupDocumentMember newMember = addGroupMemberEvent.getMember(); |
46 | 0 | IdentityManagementGroupDocument document = (IdentityManagementGroupDocument)addGroupMemberEvent.getDocument(); |
47 | 0 | boolean rulePassed = true; |
48 | |
|
49 | 0 | if (newMember == null || StringUtils.isBlank(newMember.getMemberId())){ |
50 | 0 | GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Member"}); |
51 | 0 | return false; |
52 | |
} |
53 | 0 | if(!validAssignGroup(newMember, document)) |
54 | 0 | return false; |
55 | |
|
56 | 0 | int i = 0; |
57 | 0 | for (GroupDocumentMember member: document.getMembers()){ |
58 | 0 | if (member.getMemberId().equals(newMember.getMemberId()) && member.getMemberTypeCode().equals(newMember.getMemberTypeCode())){ |
59 | 0 | rulePassed = false; |
60 | 0 | GlobalVariables.getMessageMap().putError("document.members["+i+"].memberId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Member"}); |
61 | |
} |
62 | 0 | i++; |
63 | |
} |
64 | |
|
65 | |
|
66 | 0 | GroupService groupService = KimApiServiceLocator.getGroupService(); |
67 | 0 | if (groupService.isGroupMemberOfGroup(document.getGroupId(),newMember.getMemberId())){ |
68 | 0 | GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_GROUP_MEMBER_CIRCULAR, new String[] {newMember.getMemberId()}); |
69 | 0 | return false; |
70 | |
} |
71 | |
|
72 | 0 | return rulePassed; |
73 | |
} |
74 | |
|
75 | |
protected boolean validAssignGroup(GroupDocumentMember groupMember, IdentityManagementGroupDocument document){ |
76 | 0 | boolean rulePassed = true; |
77 | 0 | if(StringUtils.isNotEmpty(document.getGroupNamespace())){ |
78 | 0 | Map<String,String> roleDetails = new HashMap<String,String>(); |
79 | 0 | roleDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace()); |
80 | 0 | roleDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName()); |
81 | 0 | if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate( |
82 | |
document, |
83 | |
KimConstants.NAMESPACE_CODE, |
84 | |
KimConstants.PermissionTemplateNames.POPULATE_GROUP, |
85 | |
GlobalVariables.getUserSession().getPerson().getPrincipalId(), |
86 | |
roleDetails, null)){ |
87 | 0 | GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_GROUP, |
88 | |
new String[] {document.getGroupNamespace(), document.getGroupName()}); |
89 | 0 | rulePassed = false; |
90 | |
} |
91 | |
} |
92 | 0 | return rulePassed; |
93 | |
} |
94 | |
|
95 | |
} |