View Javadoc

1   /**
2    * Copyright 2004-2013 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 permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.core.service.group;
17  
18  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
19  import static org.kuali.rice.core.api.criteria.PredicateFactory.greaterThan;
20  import static org.kuali.rice.core.api.criteria.PredicateFactory.isNull;
21  import static org.kuali.rice.core.api.criteria.PredicateFactory.lessThanOrEqual;
22  import static org.kuali.rice.core.api.criteria.PredicateFactory.or;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.joda.time.DateTime;
29  import org.kuali.kpme.core.KPMENamespace;
30  import org.kuali.kpme.core.group.KPMEGroup;
31  import org.kuali.rice.core.api.criteria.Predicate;
32  import org.kuali.rice.core.api.criteria.QueryByCriteria;
33  import org.kuali.rice.core.api.membership.MemberType;
34  import org.kuali.rice.kim.api.group.Group;
35  import org.kuali.rice.kim.api.group.GroupMember;
36  import org.kuali.rice.kim.api.group.GroupService;
37  
38  public class KPMEGroupServiceImpl implements KPMEGroupService {
39  	
40  	private GroupService groupService;
41  
42  	@Override
43  	public boolean isMemberOfGroup(String principalId, String groupName, DateTime asOfDate) {
44  		boolean isMemberOfGroup = false;
45  		
46  		Group group = getGroupService().getGroupByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), groupName);
47  		
48  		if (group != null) {
49  			List<Predicate> predicates = new ArrayList<Predicate>();
50  			predicates.add(equal("groupId", group.getId()));
51  			predicates.add(or(isNull("activeFromDateValue"), lessThanOrEqual("activeFromDateValue", asOfDate)));
52  			predicates.add(or(isNull("activeToDateValue"), greaterThan("activeToDateValue", asOfDate)));
53  			
54  			List<GroupMember> groupMembers = getGroupService().findGroupMembers(QueryByCriteria.Builder.fromPredicates(predicates.toArray(new Predicate[] {}))).getResults();
55  	
56  			for (GroupMember groupMember : groupMembers) {
57  				if (MemberType.PRINCIPAL.equals(groupMember.getType())) {
58  					if (StringUtils.equals(groupMember.getMemberId(), principalId)) {
59  						isMemberOfGroup = true;
60  						break;
61  					}
62  				} else if (MemberType.GROUP.equals(groupMember.getType())) {
63  					Group nestedGroup = getGroupService().getGroupByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), groupName);
64  					
65  					if (nestedGroup != null) {
66  						if (isMemberOfGroup(principalId, nestedGroup.getName(), asOfDate)) {
67  							isMemberOfGroup = true;
68  							break;
69  						}
70  					}
71  				}
72  			}
73  		}
74  		
75  		return isMemberOfGroup;
76  	}
77  	
78  	@Override
79  	public boolean isMemberOfSystemAdministratorGroup(String principalId, DateTime asOfDate) {
80      	return isMemberOfGroup(principalId, KPMEGroup.SYSTEM_ADMINISTRATOR.getGroupName(), asOfDate);
81  	}
82  	
83  	@Override
84  	public boolean isMemberOfSystemViewOnlyGroup(String principalId, DateTime asOfDate) {
85  		return isMemberOfGroup(principalId, KPMEGroup.SYSTEM_VIEW_ONLY.getGroupName(), asOfDate);
86  	}
87  
88  	public GroupService getGroupService() {
89  		return groupService;
90  	}
91  
92  	public void setGroupService(GroupService groupService) {
93  		this.groupService = groupService;
94  	}
95  
96  }