View Javadoc

1   /*
2    * Copyright 2007-2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing members and
14   * limitations under the License.
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  //import org.kuali.rice.kim.api.group.GroupServiceBase;
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   * This is a description of what this class does - shyu don't forget to fill this in. 
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	    // check for circular reference
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  }