1 19 20 package com.sslexplorer.policyframework.actions; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.struts.action.ActionForm; 26 import org.apache.struts.action.ActionForward; 27 import org.apache.struts.action.ActionMapping; 28 29 import com.sslexplorer.core.CoreUtil; 30 import com.sslexplorer.policyframework.NoPermissionException; 31 import com.sslexplorer.policyframework.Permission; 32 import com.sslexplorer.policyframework.PolicyConstants; 33 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 34 import com.sslexplorer.policyframework.PolicyUtil; 35 import com.sslexplorer.policyframework.Resource; 36 import com.sslexplorer.policyframework.ResourceStack; 37 import com.sslexplorer.policyframework.ResourceType; 38 import com.sslexplorer.policyframework.ResourceUtil; 39 import com.sslexplorer.policyframework.forms.AbstractResourcesForm; 40 import com.sslexplorer.properties.Property; 41 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey; 42 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 43 import com.sslexplorer.security.Constants; 44 import com.sslexplorer.security.SessionInfo; 45 import com.sslexplorer.table.actions.AbstractPagerAction; 46 47 54 public abstract class AbstractResourcesDispatchAction extends AbstractPagerAction { 55 protected Permission editPermission; 56 protected Permission createPermission; 57 protected Permission removePermission; 58 protected Permission assignPermission; 59 60 63 public AbstractResourcesDispatchAction() { 64 super(); 65 } 66 67 77 public AbstractResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions, 78 Permission editPermission, Permission createPermission, 79 Permission removePermission, Permission assignPermission) { 80 this(resourceType, requiredPermissions, editPermission, createPermission, removePermission, assignPermission, null); 81 } 82 83 90 public AbstractResourcesDispatchAction(ResourceType resourceType, ResourceType requiresResources) { 91 this(resourceType, new Permission[] { 92 PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, 93 PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN }, 94 PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, 95 PolicyConstants.PERM_ASSIGN, requiresResources); 96 } 97 98 110 public AbstractResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions, 111 Permission editPermission, Permission createPermission, 112 Permission removePermission, Permission assignPermission, 113 ResourceType requiresResources) { 114 super(resourceType, requiredPermissions, requiresResources); 115 this.editPermission = editPermission; 116 this.createPermission = createPermission; 117 this.removePermission = removePermission; 118 this.assignPermission = assignPermission; 119 } 120 121 124 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 125 HttpServletResponse response) throws Exception { 126 return list(mapping, form, request, response); 127 } 128 129 138 public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, 139 HttpServletResponse response) throws Exception { 140 if (getRemovePermission() != null) { 141 if (getResourceType() == null) { 142 throw new Exception ( 143 "Concrete implementation of AbstractResourcesDispatchAction does not provide the ResourceType that it is maintaining."); 144 } 145 PolicyUtil.checkPermission(getResourceType(), getRemovePermission(), request); 146 } 147 return mapping.findForward("confirmRemove"); 148 } 149 150 159 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 160 throws Exception { 161 if (getCreatePermission() != null) { 162 if (getResourceType() == null) { 163 throw new Exception ( 164 "Concrete implementation of AbstractResourcesDispatchAction does not provide the ResourceType that it is maintaining."); 165 } 166 PolicyUtil.checkPermission(getResourceType(), getCreatePermission(), request); 167 } 168 return mapping.findForward("create"); 169 } 170 171 180 public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 181 throws Exception { 182 Resource r= getResourceById(((AbstractResourcesForm)form).getSelectedResource()); 183 checkValid(r, new Permission[] { getRemovePermission() }, mapping, (AbstractResourcesForm) form, request); 184 doRemove(mapping, form, request, response); 185 PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(r, getSessionInfo(request)); 186 return getRedirectWithMessages(mapping, request); 187 } 188 189 197 protected void doRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 198 throws Exception { 199 getResourceType().removeResource(((AbstractResourcesForm)form).getSelectedResource(), getSessionInfo(request)); 200 } 201 202 212 public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 213 throws Exception { 214 edit(mapping, form, request, response); 215 return mapping.findForward("view"); 216 } 217 218 228 public ActionForward information(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 229 throws Exception { 230 int id = ((AbstractResourcesForm) form).getSelectedResource(); 231 Resource r = getResourceType().getResourceById(id); 232 try { 233 ResourceUtil.checkResourceManagementRights(r, getSessionInfo(request), new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE }); 234 } 235 catch(NoPermissionException npe) { 236 ResourceUtil.checkResourceAccessRights(r, getSessionInfo(request)); 237 } 238 request.setAttribute(Constants.REQ_ATTR_INFO_RESOURCE, r); 239 return mapping.findForward("resourceInformation"); 240 } 241 242 252 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 253 throws Exception { 254 255 int id = ((AbstractResourcesForm) form).getSelectedResource(); 258 Resource r = getResourceById(id); 259 if (r == null) { 260 throw new Exception ("No resource with ID " + id); 261 } 262 263 checkValid(r, new Permission[] { getEditPermission(), getCreatePermission(), getAssignPermission() }, mapping, (AbstractResourcesForm) form, request); 265 ResourceStack.pushToEditingStack(request.getSession(), r); 266 return mapping.findForward("edit"); 267 } 268 269 279 public ActionForward clone(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 280 throws Exception { 281 int id = ((AbstractResourcesForm) form).getSelectedResource(); 284 Resource r = getResourceById(id); 285 if (r == null) { 286 throw new Exception ("No resource with ID " + id); 287 } 288 289 checkValid(r, new Permission[] { getCreatePermission() }, mapping, (AbstractResourcesForm) form, request); 291 ResourceStack.pushToEditingStack(request.getSession(), r); 292 return mapping.findForward("clone"); 293 } 294 295 305 public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 306 throws Exception { 307 return mapping.findForward("display"); 308 } 309 310 321 public ActionForward viewIcons(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 322 throws Exception { 323 ((AbstractResourcesForm) form).setSelectedView(AbstractResourcesForm.ICONS_VIEW); 324 CoreUtil.storeUIState("ui_view_" + ((AbstractResourcesForm)form).getModel().getId() + "_" + getSessionInfo(request).getNavigationContext(), AbstractResourcesForm.ICONS_VIEW, request, response); 325 return mapping.findForward("display"); 326 } 327 328 339 public ActionForward viewList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 340 throws Exception { 341 ((AbstractResourcesForm) form).setSelectedView(AbstractResourcesForm.LIST_VIEW); 342 CoreUtil.storeUIState("ui_view_" + ((AbstractResourcesForm)form).getModel().getId() + "_" + getSessionInfo(request).getNavigationContext(), AbstractResourcesForm.LIST_VIEW, request, response); 343 return mapping.findForward("display"); 344 } 345 346 351 public Resource getResourceById(int id) throws Exception { 352 return getResourceType().getResourceById(id); 353 } 354 355 363 protected void checkValid(Resource r, Permission[] permission, ActionMapping mapping, AbstractResourcesForm form, HttpServletRequest request) 364 throws NoPermissionException { 365 ResourceUtil.checkResourceManagementRights(r, this.getSessionInfo(request), permission); 366 } 367 368 373 public Permission getEditPermission() { 374 return editPermission; 375 } 376 377 382 public Permission getAssignPermission() { 383 return assignPermission; 384 } 385 386 391 public Permission getCreatePermission() { 392 return createPermission; 393 } 394 395 400 public Permission getRemovePermission() { 401 return removePermission; 402 } 403 404 protected void saveError(HttpServletRequest request, String message, Resource resource) { 405 saveError(request, message, resource.getResourceDisplayName()); 406 } 407 408 protected void saveMessage(HttpServletRequest request, String message, Resource resource) { 409 saveMessage(request, message, resource.getResourceDisplayName()); 410 } 411 } | Popular Tags |