001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.util;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.HashSet;
021    import java.util.List;
022    import java.util.Set;
023    import java.util.SortedSet;
024    import java.util.TreeSet;
025    
026    import org.kuali.hr.time.roles.TkUserRoles;
027    import org.kuali.hr.time.roles.UserRoles;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.timesheet.TimesheetDocument;
030    import org.kuali.hr.time.workarea.WorkArea;
031    import org.kuali.rice.kim.api.identity.Person;
032    import org.kuali.rice.krad.util.GlobalVariables;
033    
034    import com.google.common.collect.Multimap;
035    
036    /**
037     * This class houses the concept of a user in the Timekeeping system.  It
038     * is essentially a lightweight wrapper around multiple KIM Person objects.
039     *
040     * One for the actual ACTUAL person
041     *
042     * One for the user the ACTUAL person is backdooring as: Back Door user is like
043     * doing 'su - <username>' in unix. You "become" that person, assume all of their
044     * roles, etc.
045     *
046     * One for the user the ACTUAL person is targeting: Targeting a user is being
047     * granted read/write access to the users data.
048     *
049     * See Javadocs for:
050     *
051     * getCurrentTargetPerson(), getCurrentPerson(), getActualPerson(),
052     * getBackdoorPerson(), getTargetPerson().
053     *
054     * the getCurrent*() methods are most likely what you should be using in any
055     * end user display logic. The methods get[ABT]*() can return null.
056     *
057     */
058    public class TKUser {
059    
060            public static void setTargetPerson(Person targetPerson) {
061                    GlobalVariables.getUserSession().addObject(TkConstants.TK_TARGET_USER_PERSON, targetPerson);
062            }
063            
064            public static boolean isTargetInUse() {
065                    return (Person) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON) != null;
066            }
067    
068            public static void clearTargetUser() {
069                    GlobalVariables.getUserSession().removeObject(TkConstants.TK_TARGET_USER_PERSON);
070            }
071        
072        /**
073         * Returns a Person object for the target person if present, otherwise
074         * the backdoor, and finally the actual.
075         *
076         * @return A Person object: target > backdoor > actual.
077         */
078        public static Person getCurrentTargetPerson() {
079            Person p = (Person) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON);
080            if (p == null) {
081                p = GlobalVariables.getUserSession().getPerson();
082            }
083            return p;
084        }
085    
086        /**
087         * Returns a UserRoles object for the target person if present, otherwise
088         * the backdoor, and finally the actual.
089         *
090         * @return A UserRoles object: target > backdoor > actual.
091         */
092        public static UserRoles getCurrentTargetRoles() {
093            return TkUserRoles.getUserRoles(getCurrentTargetPerson().getPrincipalId());
094        }
095        
096        public static TKUser getUser(Person target, Date asOfDate) {
097            TKUser.setTargetPerson(target);
098    
099            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    }