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