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