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 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 GroupDocumentMember newMember = addGroupMemberEvent.getMember();
46 IdentityManagementGroupDocument document = (IdentityManagementGroupDocument)addGroupMemberEvent.getDocument();
47 boolean rulePassed = true;
48
49 if (newMember == null || StringUtils.isBlank(newMember.getMemberId())){
50 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Member"});
51 return false;
52 }
53 if(!validAssignGroup(newMember, document))
54 return false;
55
56 int i = 0;
57 for (GroupDocumentMember member: document.getMembers()){
58 if (member.getMemberId().equals(newMember.getMemberId()) && member.getMemberTypeCode().equals(newMember.getMemberTypeCode())){
59 rulePassed = false;
60 GlobalVariables.getMessageMap().putError("document.members["+i+"].memberId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Member"});
61 }
62 i++;
63 }
64
65
66 GroupService groupService = KimApiServiceLocator.getGroupService();
67 if (groupService.isGroupMemberOfGroup(document.getGroupId(),newMember.getMemberId())){
68 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_GROUP_MEMBER_CIRCULAR, new String[] {newMember.getMemberId()});
69 return false;
70 }
71
72 return rulePassed;
73 }
74
75 protected boolean validAssignGroup(GroupDocumentMember groupMember, IdentityManagementGroupDocument document){
76 boolean rulePassed = true;
77 if(StringUtils.isNotEmpty(document.getGroupNamespace())){
78 Map<String,String> roleDetails = new HashMap<String,String>();
79 roleDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace());
80 roleDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName());
81 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 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_GROUP,
88 new String[] {document.getGroupNamespace(), document.getGroupName()});
89 rulePassed = false;
90 }
91 }
92 return rulePassed;
93 }
94
95 }