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 org.apache.commons.collections.CollectionUtils;
19  import org.joda.time.DateTime;
20  import org.joda.time.LocalDate;
21  import org.kuali.kpme.core.KPMENamespace;
22  import org.kuali.kpme.core.role.KPMERole;
23  import org.kuali.kpme.core.service.HrServiceLocator;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  public class HrContext {
28  	
29  	public static String getPrincipalId() {
30  		return GlobalVariables.getUserSession().getPrincipalId();
31  	}
32  
33      public static String getTargetPrincipalId() {
34          String principalId = (String) GlobalVariables.getUserSession().retrieveObject(HrConstants.TK_TARGET_USER_PERSON);
35          if (principalId == null) {
36          	principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
37          }
38          return principalId;
39      }
40      
41      public static String getTargetName() {
42      	return KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(getTargetPrincipalId()).getDefaultName().getCompositeName();
43      }
44      
45  	public static void setTargetPrincipalId(String principalId) {
46  		GlobalVariables.getUserSession().addObject(HrConstants.TK_TARGET_USER_PERSON, principalId);
47  	}
48      
49  	public static boolean isTargetInUse() {
50  		return GlobalVariables.getUserSession().retrieveObject(HrConstants.TK_TARGET_USER_PERSON) != null;
51  	}
52      
53  	public static void clearTargetUser() {
54  		GlobalVariables.getUserSession().removeObject(HrConstants.TK_TARGET_USER_PERSON);
55  	}
56  	
57  	public static boolean isSystemAdmin() {
58  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(getPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
59  	}
60  	
61  	public static boolean isTargetSystemAdmin() {
62  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(getTargetPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
63  	}
64  	
65  	public static boolean isGlobalViewOnly() {
66  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(getPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
67  	}
68  	
69  	public static boolean isTargetGlobalViewOnly() {
70  		return HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(getTargetPrincipalId(), LocalDate.now().toDateTimeAtStartOfDay());
71  	}
72  	
73  	public static boolean isAnyApprover() {
74  		return isApprover() || isApproverDelegate();
75  	}
76  
77      public static boolean isAnyPayrollProcessor() {
78          return isPayrollProcessor() || isPayrollProcessorDelegate();
79      }
80  	
81  	public static boolean isTargetAnyApprover() {
82  		return isTargetApprover() || isTargetApproverDelegate();
83  	}
84  	
85  	public static boolean isUserOrTargetAnyApprover() {
86  		return isAnyApprover() || isTargetAnyApprover();
87  	}
88  	
89  	public static boolean isUserOrTargetAnyPayrollProcessor() {
90  		return isAnyPayrollProcessor() || isTargetAnyPayrollProcessor();
91  	}
92  	
93  	public static boolean isUserOrTargetReviewer() {
94  		return isReviewer() || isTargetReviewer();
95  	}
96  	
97  	public static boolean isTargetAnyPayrollProcessor() {
98  		return isTargetPayrollProcessor() || isTargetPayrollProcessorDelegate();
99  	}
100 
101     public static boolean isAnyAdmin() {
102         String principalId = GlobalVariables.getUserSession().getPrincipalId();
103         DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
104         boolean isSysAdmin = HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(principalId, new DateTime());
105         boolean isTimeLocationAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), date);
106         boolean isTimeSysAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_SYSTEM_ADMINISTRATOR.getRoleName(), date);
107         boolean isLeaveLocationAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), date);
108         boolean isLeaveSysAdmin = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_SYSTEM_ADMINISTRATOR.getRoleName(), date);
109 
110         return isSysAdmin || isLeaveLocationAdmin || isLeaveSysAdmin || isTimeLocationAdmin || isTimeSysAdmin;
111     }
112 
113 	public static boolean isApprover() {
114 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
115 	}
116 	
117 	public static boolean isTargetApprover() {
118 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
119 	}
120 	
121 	public static boolean isApproverDelegate() {
122 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
123 	}
124 	
125 	public static boolean isTargetApproverDelegate() {
126 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
127 	}
128 	
129 	public static boolean isReviewer() {
130 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
131 	}
132 	
133 	public static boolean isTargetReviewer() {
134 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
135 	}
136 	
137 	public static boolean isActiveEmployee() {
138     	return CollectionUtils.isNotEmpty(HrServiceLocator.getAssignmentService().getAssignments(getPrincipalId(), LocalDate.now()));
139 	}
140     
141 	public static boolean isTargetActiveEmployee() {
142 		return CollectionUtils.isNotEmpty(HrServiceLocator.getAssignmentService().getAssignments(getTargetPrincipalId(), LocalDate.now()));
143 	}
144 	
145 	// KPME-2532
146 	public static boolean isPayrollProcessor() {
147 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
148 	}
149 	
150 	public static boolean isTargetPayrollProcessor() {
151 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
152 	}
153 	
154 	public static boolean isPayrollProcessorDelegate() {
155 		return HrServiceLocator.getKPMERoleService().principalHasRole(getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
156 	}
157 	
158 	public static boolean isTargetPayrollProcessorDelegate() {
159 		return HrServiceLocator.getKPMERoleService().principalHasRole(getTargetPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
160 	}
161 
162     //KPME-2699
163     public static boolean canEditInactiveRecords() {
164         return isAnyAdmin();
165     }
166 
167 }