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.web;
017    
018    import javax.servlet.http.HttpServletRequest;
019    import javax.servlet.http.HttpServletResponse;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.log4j.Logger;
023    import org.apache.struts.action.ActionForm;
024    import org.apache.struts.action.ActionForward;
025    import org.apache.struts.action.ActionMapping;
026    import org.apache.struts.action.ActionRedirect;
027    import org.joda.time.DateTime;
028    import org.kuali.hr.job.Job;
029    import org.kuali.hr.time.assignment.Assignment;
030    import org.kuali.hr.time.base.web.TkAction;
031    import org.kuali.hr.time.base.web.TkForm;
032    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
033    import org.kuali.hr.time.principal.PrincipalHRAttributes;
034    import org.kuali.hr.time.roles.TkUserRoles;
035    import org.kuali.hr.time.roles.UserRoles;
036    import org.kuali.hr.time.service.base.TkServiceLocator;
037    import org.kuali.hr.time.util.TKContext;
038    import org.kuali.hr.time.util.TKUser;
039    import org.kuali.hr.time.util.TkConstants;
040    import org.kuali.hr.time.workarea.WorkArea;
041    import org.kuali.rice.krad.exception.AuthorizationException;
042    import org.kuali.rice.krad.util.GlobalVariables;
043    
044    import java.sql.Date;
045    import java.util.List;
046    import java.util.Set;
047    
048    public class TimeAction extends TkAction {
049    
050            private static final Logger LOG = Logger.getLogger(TimeAction.class);
051    
052        @Override
053        protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
054            TkForm tkForm = (TkForm) form;
055    
056            if (StringUtils.equals(methodToCall, "targetEmployee") || StringUtils.equals(methodToCall, "changeEmployee") || StringUtils.equals(methodToCall, "clearBackdoor") || StringUtils.equals(methodToCall, "clearChangeUser")) {
057                // Handle security validation in targetEmployee action, we may need
058                // to check the document for validity, since the user may not
059                // necessarily be a system administrator.
060            } else {
061                if (!TKUser.isSystemAdmin()
062                                    && !TKUser.isLocationAdmin()
063                                    && !TKUser.isDepartmentAdmin()
064                                    && !TKUser.isGlobalViewOnly()
065                                    && !TKUser.isDeptViewOnly()
066                                    && (tkForm.getDocumentId() != null && !TKUser.isApproverForTimesheet(tkForm.getDocumentId()))
067                                    && (tkForm.getDocumentId() != null && !TKUser.isDocumentReadable(tkForm.getDocumentId())))  {
068                    throw new AuthorizationException("", "TimeAction", "");
069                }
070            }
071        }
072    
073        
074        @Override
075            public ActionForward execute(ActionMapping mapping, ActionForm form,
076                            HttpServletRequest request, HttpServletResponse response)
077                            throws Exception {
078            //boolean synch = TKUser.isSynchronous();
079            DateTime now = new DateTime();
080            String principalId = TKContext.getTargetPrincipalId();
081            if (TKUser.isSystemAdmin()) {
082                return new ActionRedirect("/portal.do");
083            }
084            PrincipalHRAttributes phra = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, now.toDate());
085            if (phra == null) {
086                return new ActionRedirect("/PersonInfo.do");
087            }
088            Job job = TkServiceLocator.getJobService().getPrimaryJob(principalId, now.toDate());
089            boolean activeAssignments = false;
090            if (job != null) {
091                String flsa = job.getFlsaStatus();
092                List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForJob(principalId, job.getJobNumber(), new Date(now.getMillis()));
093                for (Assignment asmnt : assignments) {
094                    if (asmnt.isActive()) {
095                        if (job.getFlsaStatus().equals(TkConstants.FLSA_STATUS_NON_EXEMPT)) {
096                            TimeCollectionRule tcr = asmnt.getTimeCollectionRule();
097                            if (tcr.isClockUserFl()) {
098                                return new ActionRedirect("/Clock.do");
099                            } else {
100                                return new ActionRedirect("/TimeDetail.do");
101                            }
102                        } else {
103                            if (job.isEligibleForLeave()) {
104                                return new ActionRedirect("/LeaveCalendar.do");
105                            }
106                        }
107                    }
108                }
109            }
110    
111            return new ActionRedirect("/PersonInfo.do");
112    
113                //if (assignment != null) {
114                //    assignment.get
115                //}
116            /*if (principalId != null) {
117                if (TKUser.isSystemAdmin()) {
118                    return new ActionRedirect("/portal.do");
119                } else if (TKUser.isDepartmentAdmin()
120                        && !synch) {
121                    return new ActionRedirect("/portal.do");
122                } else if (TKUser.isApprover()
123                        && !synch) {
124                    return new ActionRedirect("/TimeApproval.do");
125                } else if (TKUser.isReviewer()
126                        && !synch) {
127                    return new ActionRedirect("/TimeApproval.do");
128                } else if (TKUser.isActiveEmployee()
129                        && !synch) {
130                    return new ActionRedirect("/TimeDetail.do");
131                } else if (synch) {
132                    return new ActionRedirect("/Clock.do");
133                } else {
134                    return new ActionRedirect("/PersonInfo.do");
135                }
136            }
137            return super.execute(mapping, form, request, response);*/
138    }
139        
140    }