1 19 20 package com.sslexplorer.policyframework.actions; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 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.boot.PropertyList; 35 import com.sslexplorer.core.CoreUtil; 36 import com.sslexplorer.core.UserDatabaseManager; 37 import com.sslexplorer.core.actions.AuthenticatedDispatchAction; 38 import com.sslexplorer.core.forms.CoreForm; 39 import com.sslexplorer.input.MultiSelectDataSource; 40 import com.sslexplorer.input.MultiSelectSelectionModel; 41 import com.sslexplorer.navigation.FavoriteResourceType; 42 import com.sslexplorer.policyframework.AccessRightsGrantedPoliciesDatasource; 43 import com.sslexplorer.policyframework.NoPermissionException; 44 import com.sslexplorer.policyframework.OwnedResource; 45 import com.sslexplorer.policyframework.Permission; 46 import com.sslexplorer.policyframework.Policy; 47 import com.sslexplorer.policyframework.PolicyConstants; 48 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 49 import com.sslexplorer.policyframework.PolicyUtil; 50 import com.sslexplorer.policyframework.Resource; 51 import com.sslexplorer.policyframework.ResourceStack; 52 import com.sslexplorer.policyframework.ResourceType; 53 import com.sslexplorer.policyframework.ResourceUtil; 54 import com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm; 55 import com.sslexplorer.policyframework.forms.AbstractResourceForm; 56 import com.sslexplorer.security.Constants; 57 import com.sslexplorer.security.LogonControllerFactory; 58 import com.sslexplorer.security.SessionInfo; 59 import com.sslexplorer.security.User; 60 61 69 public abstract class AbstractResourceDispatchAction extends AuthenticatedDispatchAction { 70 final static Log log = LogFactory.getLog(AbstractResourceDispatchAction.class); 71 72 protected Permission editPermission; 73 protected Permission createPermission; 74 protected Permission removePermission; 75 protected Permission assignPermission; 76 77 81 public AbstractResourceDispatchAction() { 82 super(); 83 } 84 85 91 public AbstractResourceDispatchAction(ResourceType resourceType) { 92 this(resourceType, new Permission[] { PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN }, 93 PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, 94 PolicyConstants.PERM_ASSIGN); 95 } 96 97 107 public AbstractResourceDispatchAction(ResourceType resourceType, Permission[] requiredPermissions, Permission editPermission, 108 Permission createPermission, Permission removePermission, Permission assignPermission) { 109 super(resourceType, requiredPermissions); 110 this.editPermission = editPermission; 111 this.createPermission = createPermission; 112 this.removePermission = removePermission; 113 this.assignPermission = assignPermission; 114 } 115 116 124 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 125 HttpServletResponse response) throws Exception { 126 return edit(mapping, form, request, response); 127 } 128 129 139 public ActionForward clone(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 140 throws Exception { 141 if (getCreateResourcePermission() != null) { 142 if (getResourceType() == null) { 143 throw new Exception ( 144 "Concrete implementation of AbstractResourceDispatchAction does not provide the ResourceType that it is maintaining."); 145 } 146 PolicyUtil.checkPermission(getResourceType(), getCreateResourcePermission(), request); 147 } 148 Resource sourceResource = ResourceStack.popFromEditingStack(request.getSession()); 149 ResourceStack.pushToEditingStack(request.getSession(), sourceResource.getResourceType().cloneResource(sourceResource, getSessionInfo(request))); 150 ActionForward fwd = edit(mapping, form, request, response); 151 ((AbstractResourceForm) form).setCreating(); 152 return fwd; 153 } 154 155 166 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 167 throws Exception { 168 if (getCreateResourcePermission() != null) { 169 if (getResourceType() == null) { 170 throw new Exception ( 171 "Concrete implementation of AbstractResourceDispatchAction does not provide the ResourceType that it is maintaining."); 172 } 173 PolicyUtil.checkPermission(getResourceType(), getCreateResourcePermission(), request); 174 } 175 ResourceStack.pushToEditingStack(request.getSession(), createResource(mapping, form, request, response)); 176 ActionForward fwd = edit(mapping, form, request, response); 177 ((AbstractResourceForm) form).setCreating(); 178 return fwd; 179 } 180 181 194 public abstract Resource createResource(ActionMapping mapping, ActionForm form, HttpServletRequest request, 195 HttpServletResponse response) throws Exception ; 196 197 207 public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 208 throws Exception { 209 AbstractResourceForm resourceForm = (AbstractResourceForm) form; 210 resourceForm.apply(); 211 Resource resource = resourceForm.getResource(); 212 if(resourceForm.getEditing()) { 213 resource.getResourceType().updateResource(resource, getSessionInfo(request)); 214 } 215 else { 216 resource = commitCreatedResource(mapping, resourceForm, request, response); 217 } 218 doUpdate(mapping, form, request, response); 219 220 if(resource.getResourceType() != PolicyConstants.PROFILE_RESOURCE_TYPE || !resource.getResourceName().equals("Default")) { 222 PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, resourceForm.getSelectedPoliciesList(), getSessionInfo(request)); 223 } 224 if (resource.getResourceType() instanceof FavoriteResourceType) { 225 ResourceUtil.setResourceGlobalFavorite(resource, ((AbstractFavoriteResourceForm) resourceForm).isFavorite()); 226 } 227 return cleanUpAndReturnToReferer(mapping, form, request, response); 228 } 229 230 239 protected void doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 240 } 241 242 253 public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 254 throws Exception { 255 ((AbstractResourceForm) form).setReadOnly(); 256 return edit(mapping, form, request, response); 257 } 258 259 272 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 273 throws Exception { 274 ((AbstractResourceForm) form).setEditing(); 275 Resource resource = ResourceStack.peekEditingStack(request.getSession()); 276 SessionInfo session = this.getSessionInfo(request); 277 checkValid(resource, new Permission[] { getEditResourcePermission(), getCreateResourcePermission(), getAssignPermission() }, mapping, (AbstractResourceForm) form, request); 278 User ownerUser = null; 279 if (resource instanceof OwnedResource) { 280 String owner = ((OwnedResource) resource).getOwnerUsername(); 281 if (owner != null && !owner.equals("")) { 282 ownerUser = UserDatabaseManager.getInstance().getUserDatabase(session.getUser().getRealm()).getAccount(owner); 283 } 284 } 285 PropertyList selectedPolicies = new PropertyList(); 286 MultiSelectDataSource policies = createAvailablePoliciesDataSource(resource, mapping, form, request, response); 287 List l = PolicyDatabaseFactory.getInstance().getPoliciesAttachedToResource(resource, session.getUser().getRealm()); 288 for (Iterator i = l.iterator(); i.hasNext();) { 289 selectedPolicies.add(String.valueOf(((Policy) i.next()).getResourceId())); 290 } 291 String referer = CoreUtil.getReferer(request); 292 ((CoreForm) form).setReferer(referer); 293 MultiSelectSelectionModel policyModel = new MultiSelectSelectionModel(session, policies, selectedPolicies); 294 ((AbstractResourceForm) form).initialise(session.getUser(), resource, true, policyModel, selectedPolicies, ownerUser, isAssignOnly(resource, session)); 295 return display(mapping, form, request, response); 296 } 297 298 306 private boolean isAssignOnly(Resource resource, SessionInfo session) throws Exception { 307 if (!LogonControllerFactory.getInstance().isAdministrator(session.getUser())){ 308 boolean canAssign = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {PolicyConstants.PERM_ASSIGN}, session.getUser(), false); 309 boolean canEditAssign = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {PolicyConstants.PERM_EDIT_AND_ASSIGN}, session.getUser(), false); 310 boolean canCreateEditAssign = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN}, session.getUser(), false); 311 if (canAssign & !canEditAssign & !canCreateEditAssign) 312 return true; 313 } 314 return false; 315 } 316 317 329 public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, 330 HttpServletResponse response) throws Exception { 331 return mapping.findForward("confirmRemove"); 332 } 333 334 345 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 346 throws Exception { 347 return mapping.findForward("refresh"); 348 } 349 350 361 public ActionForward display(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 362 throws Exception { 363 CoreUtil.addRequiredFieldMessage(this, request); 364 return mapping.findForward("display"); 365 } 366 367 373 public Permission getEditResourcePermission() { 374 return editPermission; 375 } 376 377 383 public Permission getAssignPermission() { 384 return assignPermission; 385 } 386 387 393 394 public Permission getCreateResourcePermission() { 395 return createPermission; 396 } 397 398 404 public Permission getRemoveResourcePermission() { 405 return removePermission; 406 } 407 408 416 public ActionForward cleanUpAndReturnToReferer(ActionMapping mapping, ActionForm form, HttpServletRequest request, 417 HttpServletResponse response) throws Exception { 418 ResourceStack.popFromEditingStack(request.getSession()); 419 return super.cleanUpAndReturnToReferer(mapping, form, request, response); 420 } 421 422 430 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 431 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT; 432 } 433 434 444 protected void checkValid(Resource r, Permission[] permissions, ActionMapping mapping, AbstractResourceForm form, 445 HttpServletRequest request) throws NoPermissionException { 446 ResourceUtil.checkResourceManagementRights(r, this.getSessionInfo(request), permissions); 447 } 448 449 459 protected Resource commitCreatedResource(ActionMapping mapping, AbstractResourceForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 460 throw new Exception ("commitCreatedResource() must be overidden to actually create the resource."); 461 } 462 463 protected MultiSelectDataSource createAvailablePoliciesDataSource(Resource resource, ActionMapping mapping, ActionForm form, 464 HttpServletRequest request, HttpServletResponse response) throws Exception { 465 return new AccessRightsGrantedPoliciesDatasource(null); 466 } 467 } | Popular Tags |