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.kpme.tklm.time.rules.clocklocation.service;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.joda.time.LocalDate;
25  import org.kuali.kpme.core.KPMENamespace;
26  import org.kuali.kpme.core.department.Department;
27  import org.kuali.kpme.core.permission.KPMEPermissionTemplate;
28  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.kpme.tklm.time.clocklog.ClockLog;
31  import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRule;
32  import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRuleIpAddress;
33  import org.kuali.kpme.tklm.time.rules.clocklocation.dao.ClockLocationDao;
34  import org.kuali.rice.kim.api.KimConstants;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  
38  public class ClockLocationRuleServiceImpl implements ClockLocationRuleService {
39  	private ClockLocationDao clockLocationDao;
40  
41  	public ClockLocationDao getClockLocationDao() {
42  		return clockLocationDao;
43  	}
44  
45  	public void setClockLocationDao(ClockLocationDao clockLocationDao) {
46  		this.clockLocationDao = clockLocationDao;
47  	}
48  
49  	public void processClockLocationRule(ClockLog clockLog, LocalDate asOfDate){
50  		List<ClockLocationRule> lstClockLocationRules = getClockLocationRule(clockLog.getJob().getDept(),
51  										clockLog.getWorkArea(), clockLog.getPrincipalId(), clockLog.getJobNumber(), asOfDate);
52  		if(lstClockLocationRules.isEmpty()){
53  			return;
54  		}
55  		for(ClockLocationRule clockLocationRule : lstClockLocationRules){
56  			List<ClockLocationRuleIpAddress> ruleIpAddresses = clockLocationRule.getIpAddresses();
57  			String ipAddressClock = clockLog.getIpAddress();
58  			for(ClockLocationRuleIpAddress ruleIp : ruleIpAddresses) {
59  				if(compareIpAddresses(ruleIp.getIpAddress(), ipAddressClock)){
60  					clockLog.setUnapprovedIP(false);
61  					return;
62  				}
63  			}
64  		}
65  		clockLog.setUnapprovedIP(true);
66  		GlobalVariables.getMessageMap().putWarning("property", "ipaddress.invalid.format", clockLog.getIpAddress());
67  
68  	}
69  
70  	private boolean compareIpAddresses(String ipAddressRule, String ipAddress){
71  		String[] rulePieces = StringUtils.split(ipAddressRule, ".");
72          int ruleMax = rulePieces.length-1;
73  
74  		String[] ipAddPieces = StringUtils.split(ipAddress,".");
75  		boolean match = true;
76  		for(int i=0; i<ipAddPieces.length; i++){
77  			if( ((i > ruleMax) && StringUtils.equals("%", rulePieces[ruleMax])) ||
78                    ((i <= ruleMax) && ( StringUtils.equals(ipAddPieces[i], rulePieces[i]) || StringUtils.equals("%", rulePieces[i]) ))
79                  )
80              {
81  				// we don't need to do anything.
82  			} else {
83  			    return false;
84  			}
85  		}
86  		return match;
87  	}
88  
89  	@Override
90  	public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea,String principalId, Long jobNumber, LocalDate asOfDate) {
91  
92          // 1 : dept, wa, principal, job
93  		List<ClockLocationRule> clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea,principalId,jobNumber,asOfDate);
94  		if(!clockLocationRule.isEmpty()){
95  			return clockLocationRule;
96  		}
97  
98          // 2 : dept, wa, principal, -1
99  		clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, principalId, -1L, asOfDate);
100 		if(!clockLocationRule.isEmpty()){
101 			return clockLocationRule;
102 		}
103 
104         // 3 : dept, wa, %        , job
105         clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", jobNumber, asOfDate);
106         if(!clockLocationRule.isEmpty()){
107             return clockLocationRule;
108         }
109 
110         // 4 : dept, -1, principal, job
111         clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, jobNumber, asOfDate);
112         if(!clockLocationRule.isEmpty()){
113             return clockLocationRule;
114         }
115 
116         // 5 : dept, wa, %        , -1
117 		clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", -1L, asOfDate);
118 		if(!clockLocationRule.isEmpty()){
119 			return clockLocationRule;
120 		}
121 
122         // 6 : dept, -1, principal, -1
123         clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, -1L, asOfDate);
124         if(!clockLocationRule.isEmpty()){
125             return clockLocationRule;
126         }
127 
128         // 7 : dept, -1, %        , job
129         clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", jobNumber, asOfDate);
130         if(!clockLocationRule.isEmpty()){
131             return clockLocationRule;
132         }
133 
134         // 8 : dept, -1, %        , job
135 		clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", -1L, asOfDate);
136 		return clockLocationRule;
137 	}
138 
139 	@Override
140 	public List<ClockLocationRule> getNewerVersionClockLocationRule(
141 			String dept, Long workArea, String principalId, Long jobNumber,
142 			LocalDate asOfDate) {
143 		 
144 		return clockLocationDao.getNewerVersionClockLocationRule(dept, workArea, principalId, jobNumber, asOfDate);
145 	}
146 
147 	@Override
148 	public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId) {
149 		return clockLocationDao.getClockLocationRule(tkClockLocationRuleId);
150 	}
151 	
152 	public void populateIPAddressesForCLR(ClockLocationRule clr){
153 		clockLocationDao.populateIPAddressesForCLR(clr);
154 	}
155 
156     public List<ClockLocationRule> getClockLocationRules(String userPrincipalId, LocalDate fromEffdt, LocalDate toEffdt, String principalId, String jobNumber,
157                                                          String dept, String workArea, String active, String showHistory){
158     	List<ClockLocationRule> results = new ArrayList<ClockLocationRule>();
159     	
160     	List<ClockLocationRule> clockLocationRuleObjs = clockLocationDao.getClockLocationRules(fromEffdt, toEffdt, principalId, jobNumber, dept, workArea, active, showHistory);
161     
162     	for (ClockLocationRule clockLocationRuleObj : clockLocationRuleObjs) {
163         	String department = clockLocationRuleObj.getDept();
164         	Department departmentObj = HrServiceLocator.getDepartmentService().getDepartmentWithoutRoles(department, clockLocationRuleObj.getEffectiveLocalDate());
165         	String location = departmentObj != null ? departmentObj.getLocation() : null;
166         	
167         	Map<String, String> roleQualification = new HashMap<String, String>();
168         	roleQualification.put(KimConstants.AttributeConstants.PRINCIPAL_ID, userPrincipalId);
169         	roleQualification.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), department);
170         	roleQualification.put(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), location);
171         	
172         	if (!KimApiServiceLocator.getPermissionService().isPermissionDefinedByTemplate(KPMENamespace.KPME_WKFLW.getNamespaceCode(),
173     				KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>())
174     		  || KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(userPrincipalId, KPMENamespace.KPME_WKFLW.getNamespaceCode(),
175     				  KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>(), roleQualification)) {
176         		results.add(clockLocationRuleObj);
177         	}
178     	}
179     	
180     	return results;
181     }
182 }