View Javadoc

1   /**
2    * Copyright 2004-2012 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.admin.web;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.apache.struts.action.ActionForm;
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  import org.apache.struts.action.ActionRedirect;
27  import org.kuali.hr.time.base.web.TkAction;
28  import org.kuali.hr.time.roles.TkUserRoles;
29  import org.kuali.hr.time.roles.UserRoles;
30  import org.kuali.hr.time.util.TKContext;
31  import org.kuali.hr.time.util.TKUser;
32  import org.kuali.hr.time.util.TkConstants;
33  import org.kuali.rice.kim.api.identity.Person;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  public class ChangeTargetPersonAction extends TkAction {
38  	
39  	private static final Logger LOG = Logger.getLogger(ChangeTargetPersonAction.class);
40  	
41      public ActionForward changeTargetPerson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
42  		ActionForward forward = mapping.findForward("basic");
43      	
44      	ChangeTargetPersonForm changeTargetPersonForm = (ChangeTargetPersonForm) form;
45  
46          if (StringUtils.isNotBlank(changeTargetPersonForm.getPrincipalName())) {
47          	Person targetPerson = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(changeTargetPersonForm.getPrincipalName());
48          	
49  	        if (targetPerson != null) {
50  	        	UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
51  	            if (roles.isSystemAdmin()
52  	                	|| roles.isGlobalViewOnly()
53  	                	|| roles.isDepartmentAdminForPerson(targetPerson.getPrincipalId())
54  	                	|| roles.isDeptViewOnlyForPerson(targetPerson.getPrincipalId())
55  	                	|| roles.isLocationAdminForPerson(targetPerson.getPrincipalId())
56  	                	|| roles.isTimesheetReviewerForPerson(targetPerson.getPrincipalId())
57  	                	|| roles.isApproverForPerson(targetPerson.getPrincipalId())) {
58  		                	
59  	            	TKUser.setTargetPerson(targetPerson);
60  	
61  		            if (StringUtils.isNotEmpty(changeTargetPersonForm.getReturnUrl())) {
62  		            	GlobalVariables.getUserSession().addObject(TkConstants.TK_TARGET_USER_RETURN, changeTargetPersonForm.getReturnUrl());
63  		            }
64  		            
65  		            String returnAction = "PersonInfo.do";
66  		            if (StringUtils.isNotEmpty(changeTargetPersonForm.getTargetUrl())) {
67  		                returnAction = changeTargetPersonForm.getTargetUrl();
68  		            }
69  		            forward = new ActionRedirect(returnAction);
70  		
71  		            LOG.debug(GlobalVariables.getUserSession().getActualPerson().getPrincipalName() + " changed target person to " + targetPerson.getPrincipalName());
72  	            } else {
73  	                LOG.warn("Non-Admin user attempting to change target person.");
74  	                return mapping.findForward("unauthorized");
75  	            }
76  	        }
77          }
78  
79          return forward;
80      }
81      
82      public ActionForward clearTargetPerson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
83      	TKUser.clearTargetUser();
84          
85          String returnAction = "PersonInfo.do";
86          if (StringUtils.isNotBlank((String) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_RETURN))) {
87          	returnAction = (String) GlobalVariables.getUserSession().retrieveObject(TkConstants.TK_TARGET_USER_RETURN);
88          }
89          
90          LOG.debug(GlobalVariables.getUserSession().getActualPerson().getPrincipalName() + " cleared target person");
91  
92          return new ActionRedirect(returnAction);
93      }
94  
95  }