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.bo.ui.PersonDocumentGroup;
22 import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
23 import org.kuali.rice.kim.rule.event.ui.AddGroupEvent;
24 import org.kuali.rice.kim.rule.ui.AddGroupRule;
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 public class PersonDocumentGroupRule extends DocumentRuleBase implements AddGroupRule {
38 protected static final String NEW_GROUP = "newGroup";
39 protected static final String GROUP_ID_ERROR_PATH = NEW_GROUP+".groupId";
40
41 public boolean processAddGroup(AddGroupEvent addGroupEvent) {
42 IdentityManagementPersonDocument document = (IdentityManagementPersonDocument)addGroupEvent.getDocument();
43 PersonDocumentGroup newGroup = addGroupEvent.getGroup();
44 boolean rulePassed = true;
45 rulePassed = validAssignGroup(document, newGroup);
46
47
48 if (newGroup == null || StringUtils.isBlank(newGroup.getGroupId())) {
49 rulePassed = false;
50 GlobalVariables.getMessageMap().putError(GROUP_ID_ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Group"});
51
52 } else {
53 for (PersonDocumentGroup group : document.getGroups()) {
54 if (group.getGroupId().equals(newGroup.getGroupId())) {
55 rulePassed = false;
56 GlobalVariables.getMessageMap().putError(GROUP_ID_ERROR_PATH, RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group"});
57
58 }
59 }
60 }
61
62
63
64
65
66
67
68
69
70 return rulePassed;
71 }
72
73 protected boolean validAssignGroup(IdentityManagementPersonDocument document, PersonDocumentGroup newGroup){
74 boolean rulePassed = true;
75 Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
76 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, newGroup.getNamespaceCode());
77 additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, newGroup.getGroupName());
78 if(!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
79 document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP,
80 GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){
81 GlobalVariables.getMessageMap().putError(GROUP_ID_ERROR_PATH,
82 RiceKeyConstants.ERROR_ASSIGN_GROUP,
83 new String[] {newGroup.getNamespaceCode(), newGroup.getGroupName()});
84 rulePassed = false;
85 }
86 return rulePassed;
87 }
88
89 }