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.hr.time.util;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  import org.kuali.hr.time.roles.TkUserRoles;
26  import org.kuali.hr.time.roles.UserRoles;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.workarea.WorkArea;
29  import org.kuali.rice.kim.api.identity.Person;
30  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  import com.google.common.collect.Multimap;
34  
35  /**
36   * This class houses the concept of a user in the Timekeeping system.  It
37   * is essentially a lightweight wrapper around multiple KIM Person objects.
38   *
39   * One for the actual ACTUAL person
40   *
41   * One for the user the ACTUAL person is backdooring as: Back Door user is like
42   * doing 'su - <username>' in unix. You "become" that person, assume all of their
43   * roles, etc.
44   *
45   * One for the user the ACTUAL person is targeting: Targeting a user is being
46   * granted read/write access to the users data.
47   *
48   * See Javadocs for:
49   *
50   * getCurrentTargetPerson(), getCurrentPerson(), getActualPerson(),
51   * getBackdoorPerson(), getTargetPerson().
52   *
53   * the getCurrent*() methods are most likely what you should be using in any
54   * end user display logic. The methods get[ABT]*() can return null.
55   *
56   */
57  public class TKUser {
58  
59  	public static void setTargetPerson(String principalId) {
60  		GlobalVariables.getUserSession().addObject(TkConstants.TK_TARGET_USER_PERSON, principalId);
61  	}
62  	
63  	public static boolean isTargetInUse() {
64  		return GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON) != null;
65  	}
66  
67  	public static void clearTargetUser() {
68  		GlobalVariables.getUserSession().removeObject(TkConstants.TK_TARGET_USER_PERSON);
69  	}
70      
71      /**
72       * Returns a principal id for the target person if present, otherwise
73       * the backdoor, and finally the actual.
74       *
75       * @return A principalId: target > backdoor > actual.
76       */
77      public static String getCurrentTargetPersonId() {
78          String p = (String) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON);
79          if (p == null) {
80              p = GlobalVariables.getUserSession().getPerson().getPrincipalId();
81          }
82          return p;
83      }
84  
85      /**
86       * Returns a Person object for the target person if present, otherwise
87       * the backdoor, and finally the actual.
88       *
89       * @return A Person object: target > backdoor > actual.
90       */
91      public static Person getCurrentTargetPerson() {
92          Person p;
93          String principalId = (String) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON);
94          if (principalId == null) {
95              p = GlobalVariables.getUserSession().getPerson();
96          } else {
97              p = KimApiServiceLocator.getPersonService().getPerson(principalId);
98          }
99          return p;
100     }
101 
102     /**
103      * Returns a UserRoles object for the target person if present, otherwise
104      * the backdoor, and finally the actual.
105      *
106      * @return A UserRoles object: target > backdoor > actual.
107      */
108     public static UserRoles getCurrentTargetRoles() {
109     	return TkUserRoles.getUserRoles(getCurrentTargetPersonId());
110     }
111     
112     //public static TKUser getUser(String targetUserId, Date asOfDate) {
113     //    TKUser.setTargetPerson(targetUserId);
114     //
115     //    return new TKUser();
116     //}
117 
118 	public static boolean isSystemAdmin() {
119 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isSystemAdmin();
120 	}
121 
122 	public static boolean isLocationAdmin() {
123 		return TKUser.getLocationAdminAreas().size() > 0;
124 	}
125 
126 	public static boolean isDepartmentAdmin() {
127 		return TKUser.getDepartmentAdminAreas().size() > 0;
128 	}
129 
130 	public static boolean isGlobalViewOnly() {
131 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isGlobalViewOnly();
132 	}
133 
134 	public static boolean isDeptViewOnly() {
135 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isDeptViewOnly();
136 	}
137 	
138 	public static boolean isActiveEmployee() {
139 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isActiveEmployee();
140 	}
141 	
142 	public static boolean isSynchronous() {
143 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isSynchronous();
144 	}
145 
146 	public static boolean isReviewer() {
147 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isReviewer();
148 	}
149 
150 	public static boolean isApprover() {
151 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isApprover();
152 	}
153 	
154 	public static boolean isTimesheetReviewer() {
155 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isTimesheetReviewer();
156 	}
157 	
158 	public static boolean isTimesheetApprover() {
159 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isTimesheetApprover();
160 	}
161 	
162 	public static boolean isAnyApproverActive() {
163 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
164 	}
165 	
166 	public static boolean isApproverForTimesheet(String docId) {
167 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isApproverForTimesheet(docId);
168 	}
169 	
170 	public static boolean isDocumentReadable(String documentId) {
171 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isDocumentReadable(documentId);
172 	}
173 	
174 	public static boolean isDocumentWritable(String documentId) {
175 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isDocumentWritable(documentId);
176 	}
177 	
178 	public static Multimap<String, Long> getReportingApprovalDepartments(){
179 		UserRoles userRoles = TkUserRoles.getUserRoles(TKContext.getTargetPrincipalId());
180         Set<Long> workAreas = new HashSet<Long>();
181         workAreas.addAll(userRoles.getApproverWorkAreas());
182         workAreas.addAll(userRoles.getReviewerWorkAreas());
183         // see the comment in the getDeptWorkAreasByWorkAreas() for the explanation of Multimap
184         Multimap<String, Long> reportingApprovalDepartments = TkServiceLocator.getTimeApproveService().getDeptWorkAreasByWorkAreas(workAreas);
185 
186         //KPME-1338
187 		/*Set<String> depts = new HashSet<String>();
188 		depts.addAll(userRoles.getDepartmentViewOnlyDepartments());
189 		depts.addAll(userRoles.getOrgAdminDepartments());
190         if (depts.size() > 0) {
191             reportingApprovalDepartments.putAll(TkServiceLocator.getTimeApproveService().getDeptWorkAreasByDepts(depts));
192         }*/
193 		
194 		return reportingApprovalDepartments;
195 	}
196 	
197 	public static Set<Long> getReportingWorkAreas(){
198 		UserRoles userRoles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
199 		Set<Long> reportingWorkAreas = new HashSet<Long>();
200 		List<String> depts = new ArrayList<String>();
201 		
202 		reportingWorkAreas.addAll(userRoles.getApproverWorkAreas());
203 		for(Long workArea : userRoles.getApproverWorkAreas()){
204 			if(!reportingWorkAreas.contains(workArea)){
205 				reportingWorkAreas.add(workArea);
206 			}
207 		}
208 		
209 		for(Long workArea : userRoles.getReviewerWorkAreas()){
210 			if(!reportingWorkAreas.contains(workArea)){
211 				reportingWorkAreas.add(workArea);
212 			}
213 		}
214 		
215 		reportingWorkAreas.addAll(userRoles.getReviewerWorkAreas());
216 		
217 		depts.addAll(userRoles.getDepartmentViewOnlyDepartments());
218 		depts.addAll(userRoles.getOrgAdminDepartments());
219 		
220 		for(String dept : depts){
221 			List<WorkArea> workAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(dept, TKUtils.getCurrentDate());
222 			for(WorkArea workArea : workAreas){
223 				if(!reportingWorkAreas.contains(workArea.getWorkArea())){
224 					reportingWorkAreas.add(workArea.getWorkArea());
225 				}
226 			}
227 		}
228 		
229 		
230 		return reportingWorkAreas;
231 	}
232 	
233 	public static Set<Long> getApproverWorkAreas() {
234 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
235 	}
236 
237 	public static Set<Long> getReviewerWorkAreas() {
238 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getReviewerWorkAreas();
239 	}
240 
241 	public static Set<String> getLocationAdminAreas() {
242 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getOrgAdminCharts();
243 	}
244 
245 	public static Set<String> getDepartmentAdminAreas() {
246 		return TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getOrgAdminDepartments();
247 	}
248 
249     public static SortedSet<Long> getWorkAreasFromUserRoles() {
250     	UserRoles userRoles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
251         SortedSet<Long> workAreas = new TreeSet<Long>();
252         workAreas.addAll(userRoles.getApproverWorkAreas());
253         workAreas.addAll(userRoles.getReviewerWorkAreas());
254         
255         if(userRoles.isDepartmentAdmin()){
256         	Set<String> deptAdminDepts = userRoles.getOrgAdminDepartments();
257         	for(String dept : deptAdminDepts){
258         		List<WorkArea> was = TkServiceLocator.getWorkAreaService().getWorkAreas(dept, TKUtils.getCurrentDate());
259         		for(WorkArea wa : was){
260         			workAreas.add(wa.getWorkArea());
261         		}
262         	}
263         }
264 
265         return workAreas;
266     }
267 
268 }