View Javadoc

1   /**
2    * Copyright 2004-2014 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.util;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.joda.time.DateTime;
23  import org.joda.time.LocalDate;
24  import org.kuali.kpme.core.KPMENamespace;
25  import org.kuali.kpme.core.api.job.JobContract;
26  import org.kuali.kpme.core.role.KPMERole;
27  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  public class HrContext {
33  	
34  	public static String getPrincipalId() {
35  		return GlobalVariables.getUserSession().getPrincipalId();
36  	}
37  
38      public static String getTargetPrincipalId() {
39          String principalId = (String) GlobalVariables.getUserSession().retrieveObject(HrConstants.TK_TARGET_USER_PERSON);
40          if (principalId == null) {
41          	principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
42          }
43          return principalId;
44      }
45      
46      public static String getTargetName() {
47      	return KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(getTargetPrincipalId()).getDefaultName().getCompositeName();
48      }
49      
50  	public static void setTargetPrincipalId(String principalId) {
51  		GlobalVariables.getUserSession().addObject(HrConstants.TK_TARGET_USER_PERSON, principalId);
52  	}
53      
54  	public static boolean isTargetInUse() {
55  		return GlobalVariables.getUserSession().retrieveObject(HrConstants.TK_TARGET_USER_PERSON) != null;
56  	}
57      
58  	public static void clearTargetUser() {
59  		GlobalVariables.getUserSession().removeObject(HrConstants.TK_TARGET_USER_PERSON);
60  	}
61  	
62  	public static boolean isSystemAdmin() {
63  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(getPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
64  	}
65  	
66  	public static boolean isTargetSystemAdmin() {
67  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(getTargetPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
68  	}
69  	
70  	public static boolean isGlobalViewOnly() {
71  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(getPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
72  	}
73  	
74  	public static boolean isTargetGlobalViewOnly() {
75  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(getTargetPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
76  	}
77  	
78  	public static boolean isAnyApprover() {
79  		return isApprover() || isApproverDelegate() || isAnyPositionApprover() || isAnyPositionApproverDelegate();
80  	}
81  
82  	public static boolean isAnyPositionApprover() {
83  		return isAnyPositionAuthorizedUser(getPrincipalId(), KPMERole.APPROVER.getRoleName());
84  	}
85  	
86  	public static boolean isTargetAnyPositionApprover() {
87  		return isAnyPositionAuthorizedUser(getTargetPrincipalId(), KPMERole.APPROVER.getRoleName());
88  	}
89  	
90  	public static boolean isAnyPositionApproverDelegate() {
91  		return isAnyPositionAuthorizedUser(getPrincipalId(), KPMERole.APPROVER_DELEGATE.getRoleName());
92  	}
93  	
94  	public static boolean isTargetAnyPositionApproverDelegate() {
95  		return isAnyPositionAuthorizedUser(getTargetPrincipalId(), KPMERole.APPROVER_DELEGATE.getRoleName());
96  	}
97  	
98  	public static boolean isAnyPositionReviewer() {
99  		return isAnyPositionAuthorizedUser(getPrincipalId(), KPMERole.REVIEWER.getRoleName());
100 	}
101 	
102 	public static boolean isTargetAnyPositionReviewer() {
103 		return isAnyPositionAuthorizedUser(getTargetPrincipalId(), KPMERole.REVIEWER.getRoleName());
104 	}
105 	
106 	public static boolean isAnyPositionAuthorizedUser(String principalId, String roleName){
107 		DateTime asOfDate = LocalDate.now().toDateTimeAtStartOfDay();
108 		JobContract jobObj = HrServiceLocator.getJobService().getPrimaryJob(principalId, LocalDate.now());
109 		Map<String, String> qualification = new HashMap<String, String>();
110 		if(jobObj != null) {
111 			qualification.put(KPMERoleMemberAttribute.POSITION.getRoleMemberAttributeName(), jobObj.getPositionNumber());
112 			qualification.put(KPMERoleMemberAttribute.WORK_AREA.getRoleMemberAttributeName(), "%");
113 		}
114 		return HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), roleName, qualification, asOfDate);
115 	}
116 	
117     public static boolean isAnyPayrollProcessor() {
118         return isPayrollProcessor() || isPayrollProcessorDelegate();
119     }
120 	
121 	public static boolean isTargetAnyApprover() {
122 		return isTargetApprover() || isTargetApproverDelegate() || isTargetAnyPositionApprover() || isTargetAnyPositionApproverDelegate();
123 	}
124 	
125 	public static boolean isUserOrTargetAnyApprover() {
126 		return isAnyApprover() || isTargetAnyApprover() ;
127 	}
128 	
129 	public static boolean isUserOrTargetAnyPayrollProcessor() {
130 		return isAnyPayrollProcessor() || isTargetAnyPayrollProcessor();
131 	}
132 	
133 	public static boolean isUserOrTargetReviewer() {
134 		return isReviewer() || isTargetReviewer() || isAnyPositionReviewer() || isTargetAnyPositionReviewer();
135 	}
136 	
137 	public static boolean isTargetAnyPayrollProcessor() {
138 		return isTargetPayrollProcessor() || isTargetPayrollProcessorDelegate();
139 	}
140 
141     public static boolean isAnyAdmin() {
142         String principalId = GlobalVariables.getUserSession().getPrincipalId();
143         DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
144         boolean isSysAdmin = HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(principalId, new DateTime());
145         boolean isTimeLocationAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), date);
146         boolean isTimeSysAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_SYSTEM_ADMINISTRATOR.getRoleName(), date);
147         boolean isLeaveLocationAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), date);
148         boolean isLeaveSysAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_SYSTEM_ADMINISTRATOR.getRoleName(), date);
149 
150         return isSysAdmin || isLeaveLocationAdmin || isLeaveSysAdmin || isTimeLocationAdmin || isTimeSysAdmin;
151     }
152 
153 	public static boolean isApprover() {
154 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
155 	}
156 	
157 	public static boolean isTargetApprover() {
158 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
159 	}
160 	
161 	public static boolean isApproverDelegate() {
162 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
163 	}
164 	
165 	public static boolean isTargetApproverDelegate() {
166 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
167 	}
168 	
169 	public static boolean isReviewer() {
170 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
171 	}
172 	
173 	public static boolean isTargetReviewer() {
174 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
175 	}
176 	
177 	public static boolean isActiveEmployee() {
178     	return CollectionUtils.isNotEmpty(HrServiceLocator.getAssignmentService().getAssignments(getPrincipalId(), LocalDate.now()));
179 	}
180     
181 	public static boolean isTargetActiveEmployee() {
182 		return CollectionUtils.isNotEmpty(HrServiceLocator.getAssignmentService().getAssignments(getTargetPrincipalId(), LocalDate.now()));
183 	}
184 	
185 	// KPME-2532
186 	public static boolean isPayrollProcessor() {
187 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
188 	}
189 	
190 	public static boolean isTargetPayrollProcessor() {
191 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
192 	}
193 	
194 	public static boolean isPayrollProcessorDelegate() {
195 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
196 	}
197 	
198 	public static boolean isTargetPayrollProcessorDelegate() {
199 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
200 	}
201 
202     //KPME-2699
203     public static boolean canEditInactiveRecords() {
204         return isAnyAdmin();
205     }
206 
207 }