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.util.ArrayList;
019    import java.util.HashSet;
020    import java.util.List;
021    import java.util.Set;
022    import java.util.SortedSet;
023    import java.util.TreeSet;
024    
025    import org.kuali.hr.time.roles.TkUserRoles;
026    import org.kuali.hr.time.roles.UserRoles;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.workarea.WorkArea;
029    import org.kuali.rice.kim.api.identity.Person;
030    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
031    import org.kuali.rice.krad.util.GlobalVariables;
032    
033    import com.google.common.collect.Multimap;
034    
035    /**
036     * This class houses the concept of a user in the Timekeeping system.  It
037     * is essentially a lightweight wrapper around multiple KIM Person objects.
038     *
039     * One for the actual ACTUAL person
040     *
041     * One for the user the ACTUAL person is backdooring as: Back Door user is like
042     * doing 'su - <username>' in unix. You "become" that person, assume all of their
043     * roles, etc.
044     *
045     * One for the user the ACTUAL person is targeting: Targeting a user is being
046     * granted read/write access to the users data.
047     *
048     * See Javadocs for:
049     *
050     * getCurrentTargetPerson(), getCurrentPerson(), getActualPerson(),
051     * getBackdoorPerson(), getTargetPerson().
052     *
053     * the getCurrent*() methods are most likely what you should be using in any
054     * end user display logic. The methods get[ABT]*() can return null.
055     *
056     */
057    public class TKUser {
058    
059            public static void setTargetPerson(String principalId) {
060                    GlobalVariables.getUserSession().addObject(TkConstants.TK_TARGET_USER_PERSON, principalId);
061            }
062            
063            public static boolean isTargetInUse() {
064                    return GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON) != null;
065            }
066    
067            public static void clearTargetUser() {
068                    GlobalVariables.getUserSession().removeObject(TkConstants.TK_TARGET_USER_PERSON);
069            }
070        
071        /**
072         * Returns a principal id for the target person if present, otherwise
073         * the backdoor, and finally the actual.
074         *
075         * @return A principalId: target > backdoor > actual.
076         */
077        public static String getCurrentTargetPersonId() {
078            String p = (String) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON);
079            if (p == null) {
080                p = GlobalVariables.getUserSession().getPerson().getPrincipalId();
081            }
082            return p;
083        }
084    
085        /**
086         * Returns a Person object for the target person if present, otherwise
087         * the backdoor, and finally the actual.
088         *
089         * @return A Person object: target > backdoor > actual.
090         */
091        public static Person getCurrentTargetPerson() {
092            Person p;
093            String principalId = (String) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_PERSON);
094            if (principalId == null) {
095                p = GlobalVariables.getUserSession().getPerson();
096            } else {
097                p = KimApiServiceLocator.getPersonService().getPerson(principalId);
098            }
099            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    }