1 19 20 package com.sslexplorer.security.actions; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.List ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 34 import com.sslexplorer.core.CoreAttributeConstants; 35 import com.sslexplorer.core.CoreEvent; 36 import com.sslexplorer.core.CoreEventConstants; 37 import com.sslexplorer.core.CoreServlet; 38 import com.sslexplorer.core.CoreUtil; 39 import com.sslexplorer.policyframework.Permission; 40 import com.sslexplorer.policyframework.PolicyConstants; 41 import com.sslexplorer.policyframework.PolicyUtil; 42 import com.sslexplorer.security.Constants; 43 import com.sslexplorer.security.IpRestriction; 44 import com.sslexplorer.security.SessionInfo; 45 import com.sslexplorer.security.SystemDatabase; 46 import com.sslexplorer.security.SystemDatabaseFactory; 47 import com.sslexplorer.security.forms.ShowAvailableIpRestrictionsForm; 48 import com.sslexplorer.table.actions.AbstractPagerAction; 49 import com.sslexplorer.table.forms.AbstractPagerForm; 50 51 59 public class ShowAvailableIpRestrictionsDispatchAction extends AbstractPagerAction { 60 61 64 public ShowAvailableIpRestrictionsDispatchAction() { 65 super(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CREATE, PolicyConstants.PERM_DELETE }); 66 } 67 68 75 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 76 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 77 } 78 79 89 public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 90 throws Exception { 91 CoreUtil.clearFlow(request); 92 IpRestriction[] restrictions = null; 93 94 SystemDatabase sdb = SystemDatabaseFactory.getInstance(); 95 restrictions = sdb.getIpRestrictions(); 96 97 ((ShowAvailableIpRestrictionsForm) form).initialize(restrictions, request.getSession()); 98 return mapping.findForward("success"); 99 } 100 101 109 public ActionForward confirmDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 110 throws Exception { 111 112 String [] id = request.getParameterValues("id"); 113 if (id != null) { 114 IpRestriction[] ipRestrictions = findRemainingIpRestrictions(id); 115 SystemDatabase database = SystemDatabaseFactory.getInstance(); 116 String remoteAddr = request.getRemoteAddr(); 117 boolean isValid = database.verifyIPAddress(remoteAddr, ipRestrictions); 118 return mapping.findForward(isValid ? "confirmDelete" : "confirmDeleteWithLockout"); 119 } 120 return mapping.findForward("refresh"); 121 } 122 123 private IpRestriction[] findRemainingIpRestrictions(String [] restrictionIds) throws Exception { 124 SystemDatabase database = SystemDatabaseFactory.getInstance(); 125 IpRestriction[] restrictions = database.getIpRestrictions(); 126 127 Collection <IpRestriction> differences = new HashSet <IpRestriction>(Arrays.asList(restrictions)); 128 for (String restrictionId : restrictionIds) { 129 IpRestriction ipRestriction = findIpRestriction(restrictions, Integer.valueOf(restrictionId)); 130 if (ipRestriction !=null) { 131 differences.remove(ipRestriction); 132 } 133 } 134 return differences.toArray(new IpRestriction[differences.size()]); 135 } 136 137 147 public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 148 throws Exception { 149 PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request); 150 String [] id = request.getParameterValues("id"); 151 if (id != null) { 152 deleteIpRestrictions(request, id); 153 } 154 return mapping.findForward("refresh"); 155 } 156 157 168 public ActionForward moveDown(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 169 PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_EDIT, request); 170 int id = Integer.parseInt(request.getParameter("id")); 171 SystemDatabase database = SystemDatabaseFactory.getInstance(); 172 IpRestriction restriction1 = database.getIpRestriction(id); 173 String ipAddress = restriction1.getAddress(); 174 String ipPermission = restriction1.getAllowed() ? "Allowed" : "Denied"; 175 try { 176 List <IpRestriction> restrictions = Arrays.asList(database.getIpRestrictions()); 177 database.swapIpRestrictions(restriction1, restrictions.get(restrictions.indexOf(restriction1) + 1)); 178 fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_DOWN, ipAddress, ipPermission, CoreEvent.STATE_SUCCESSFUL); 179 } catch (Exception e) { 180 fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_DOWN, ipAddress, ipPermission, CoreEvent.STATE_UNSUCCESSFUL); 181 throw e; 182 } 183 return mapping.findForward("refresh"); 184 } 185 186 197 public ActionForward moveUp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 198 PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_EDIT, request); 199 int id = Integer.parseInt(request.getParameter("id")); 200 SystemDatabase database = SystemDatabaseFactory.getInstance(); 201 IpRestriction restriction1 = database.getIpRestriction(id); 202 String ipAddress = restriction1.getAddress(); 203 String ipPermission = restriction1.getAllowed() ? "Allowed" : "Denied"; 204 try { 205 List <IpRestriction> restrictions = Arrays.asList(database.getIpRestrictions()); 206 database.swapIpRestrictions(restriction1, restrictions.get(restrictions.indexOf(restriction1) - 1)); 207 fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_UP, ipAddress, ipPermission, CoreEvent.STATE_SUCCESSFUL); 208 } catch (Exception e) { 209 fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_UP, ipAddress, ipPermission, CoreEvent.STATE_UNSUCCESSFUL); 210 throw e; 211 } 212 return mapping.findForward("refresh"); 213 } 214 215 private void deleteIpRestrictions(HttpServletRequest request, String [] restrictionIds) throws Exception { 216 SystemDatabase database = SystemDatabaseFactory.getInstance(); 217 IpRestriction[] restrictions = database.getIpRestrictions(); 218 219 for (String restrictionId : restrictionIds) { 220 IpRestriction ipRestriction = findIpRestriction(restrictions, Integer.valueOf(restrictionId)); 221 if (ipRestriction != null) { 222 deleteIpRestriction(request, ipRestriction); 223 } 224 } 225 } 226 227 private void deleteIpRestriction(HttpServletRequest request, IpRestriction restriction) throws Exception { 228 String ipAddress = restriction.getAddress(); 229 String ipPermission = restriction.getAllowed() ? "Allowed" : "Denied"; 230 231 try { 232 SystemDatabase database = SystemDatabaseFactory.getInstance(); 233 database.removeIpRestriction(restriction.getID()); 234 fireCoreEvent(request, CoreEventConstants.DELETE_IP_RESTRICTION, ipAddress, ipPermission, CoreEvent.STATE_SUCCESSFUL); 235 } catch (Exception e) { 236 fireCoreEvent(request, CoreEventConstants.DELETE_IP_RESTRICTION, ipAddress, ipPermission, CoreEvent.STATE_UNSUCCESSFUL); 237 throw e; 238 } 239 } 240 241 private static IpRestriction findIpRestriction(IpRestriction[] restrictions, int id) { 242 for (IpRestriction restriction : restrictions) { 243 if (restriction.getID() == id) 244 return restriction; 245 } 246 return null; 247 } 248 249 private void fireCoreEvent(HttpServletRequest request, int eventID, String ipAddress, String ipPermission, int state) { 250 CoreEvent coreEvent = new CoreEvent(this, eventID, null, getSessionInfo(request), state); 251 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_ADDRESS, ipAddress); 252 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_IS_AUTHORIZED, ipPermission); 253 CoreServlet.getServlet().fireCoreEvent(coreEvent); 254 } 255 256 265 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 266 throws Exception { 267 return mapping.findForward("create"); 268 } 269 270 279 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 280 throws Exception { 281 int id = Integer.parseInt(request.getParameter("id")); 282 IpRestriction restriction = SystemDatabaseFactory.getInstance().getIpRestriction(id); 283 request.setAttribute(Constants.EDITING_ITEM, restriction); 284 return mapping.findForward("edit"); 285 } 286 287 294 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 295 return list(mapping, form, request, response); 296 } 297 298 306 public ActionForward filter(ActionMapping mapping, ActionForm form, HttpServletRequest request, 307 HttpServletResponse response) throws Exception { 308 return unspecified(mapping, form, request, response); 309 } 310 } | Popular Tags |