1 package com.sslexplorer.policyframework.actions; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 9 10 import org.apache.struts.Globals; 11 import org.apache.struts.action.ActionForm; 12 import org.apache.struts.action.ActionForward; 13 import org.apache.struts.action.ActionMapping; 14 import org.apache.struts.action.ActionMessages; 15 16 import com.sslexplorer.core.BundleActionMessage; 17 import com.sslexplorer.navigation.Favorite; 18 import com.sslexplorer.navigation.FavoriteResourceType; 19 import com.sslexplorer.policyframework.Permission; 20 import com.sslexplorer.policyframework.PolicyConstants; 21 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 22 import com.sslexplorer.policyframework.PolicyUtil; 23 import com.sslexplorer.policyframework.Resource; 24 import com.sslexplorer.policyframework.ResourceType; 25 import com.sslexplorer.policyframework.ResourceUtil; 26 import com.sslexplorer.policyframework.forms.AbstractResourcesForm; 27 import com.sslexplorer.security.LogonControllerFactory; 28 import com.sslexplorer.security.SessionInfo; 29 import com.sslexplorer.security.SystemDatabase; 30 import com.sslexplorer.security.SystemDatabaseFactory; 31 import com.sslexplorer.security.User; 32 33 40 public abstract class AbstractFavoriteResourcesDispatchAction extends AbstractResourcesDispatchAction { 41 42 43 46 public AbstractFavoriteResourcesDispatchAction() { 47 super(); 48 } 49 50 57 public AbstractFavoriteResourcesDispatchAction(ResourceType resourceType, ResourceType requiresResources) { 58 this(resourceType, new Permission[] { PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN }, 59 PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, 60 PolicyConstants.PERM_ASSIGN, requiresResources); 61 } 62 63 71 public AbstractFavoriteResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions, 72 Permission editPermission, Permission createPermission, 73 Permission removePermission, Permission assignPermission) { 74 super(resourceType, requiredPermissions, editPermission, createPermission, removePermission, assignPermission); 75 } 76 77 86 public AbstractFavoriteResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions, 87 Permission editPermission, Permission createPermission, 88 Permission removePermission, 89 Permission assignPermission, ResourceType requiresResources) { 90 super(resourceType, requiredPermissions, editPermission, createPermission, removePermission, assignPermission, requiresResources); 91 } 92 93 103 public ActionForward favorite(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 104 throws Exception { 105 AbstractResourcesForm f = (AbstractResourcesForm) form; 106 ActionMessages msgs = new ActionMessages(); 107 User user = LogonControllerFactory.getInstance().getUser(request); 108 SystemDatabase sdb = SystemDatabaseFactory.getInstance(); 109 if (getSessionInfo(request).getNavigationContext() != SessionInfo.USER_CONSOLE_CONTEXT) { 110 throw new Exception ("Favorites may only be set in the user console."); 111 } 112 PolicyUtil.checkPermission(PolicyConstants.FAVORITES_RESOURCE_TYPE, PolicyConstants.PERM_USE, request); 113 Resource resource = getResourceById(f.getSelectedResource()); 114 if(ResourceUtil.filterResourceIdsForGlobalFavorites(PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(getSessionInfo(request).getUser(), 115 resource.getResourceType()), resource.getResourceType()).contains(new Integer (resource.getResourceId()))) { 116 throw new Exception ("Cannot set a user favorite for items that have a policy favorite."); 117 } 118 if (sdb.getFavorite(getResourceType().getResourceTypeId(), user, f.getSelectedResource()) != null) { 119 msgs.add(Globals.ERROR_KEY, new BundleActionMessage("navigation", "addToFavorites.error.alreadyFavorite", resource 120 .getResourceName())); 121 saveErrors(request, msgs); 122 return mapping.findForward("refresh"); 123 } 124 sdb.addFavorite(getResourceType().getResourceTypeId(), f.getSelectedResource(), user.getPrincipalName()); 125 msgs.add(Globals.MESSAGES_KEY, new BundleActionMessage("navigation", "addToFavorites.message.favoriteAdded", resource 126 .getResourceName())); 127 saveMessages(request, msgs); 128 return mapping.findForward("refresh"); 129 } 130 131 141 public ActionForward removeFavorite(ActionMapping mapping, ActionForm form, HttpServletRequest request, 142 HttpServletResponse response) throws Exception { 143 AbstractResourcesForm f = (AbstractResourcesForm) form; 144 ActionMessages msgs = new ActionMessages(); 145 User user = LogonControllerFactory.getInstance().getUser(request); 146 SystemDatabase sdb = SystemDatabaseFactory.getInstance(); 147 if (getSessionInfo(request).getNavigationContext() != SessionInfo.USER_CONSOLE_CONTEXT) { 148 throw new Exception ("Favorites may only be set in the user console."); 149 } 150 PolicyUtil.checkPermission(PolicyConstants.FAVORITES_RESOURCE_TYPE, PolicyConstants.PERM_USE, request); 151 Resource resource = getResourceById(f.getSelectedResource()); 152 if(ResourceUtil.filterResourceIdsForGlobalFavorites(PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(getSessionInfo(request).getUser(), 153 resource.getResourceType()), resource.getResourceType()).contains(new Integer (resource.getResourceId()))) { 154 throw new Exception ("Cannot remove a user favorite from items that have a policy favorite."); 155 } 156 if (sdb.getFavorite(getResourceType().getResourceTypeId(), user, f.getSelectedResource()) == null) { 157 msgs.add(Globals.ERROR_KEY, new BundleActionMessage("navigation", "removeFromFavorites.error.notFavorite", resource 158 .getResourceName())); 159 saveErrors(request, msgs); 160 return mapping.findForward("refresh"); 161 } 162 sdb.removeFavorite(getResourceType().getResourceTypeId(), f.getSelectedResource(), user.getPrincipalName()); 163 msgs.add(Globals.MESSAGES_KEY, new BundleActionMessage("navigation", "removeFromFavorites.message.favoriteAdded", resource 164 .getResourceName())); 165 saveMessages(request, msgs); 166 return mapping.findForward("refresh"); 167 } 168 169 177 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 178 HttpServletResponse response) throws Exception { 179 List <Integer > l = new ArrayList <Integer >(); 182 List <Favorite> favorites = SystemDatabaseFactory.getInstance().getFavorites(getResourceType().getResourceTypeId(), 183 getSessionInfo(request).getUser()); 184 for (Iterator i = favorites.iterator(); i.hasNext();) { 185 Favorite f = (Favorite) i.next(); 186 l.add(new Integer (f.getFavoriteKey())); 187 } 188 ((AbstractResourcesForm) form).setUserFavorites(l); 189 ((AbstractResourcesForm) form).setGlobalFavorites(ResourceUtil.filterResourceIdsForGlobalFavorites(PolicyDatabaseFactory.getInstance() 190 .getGrantedResourcesOfType(getSessionInfo(request).getUser(), getResourceType()), getResourceType())); 191 return super.unspecified(mapping, form, request, response); 192 } 193 194 197 public Resource getResourceById(int id) throws Exception { 198 return ((FavoriteResourceType)getResourceType()).getResourceById(id); 199 } 200 201 } 202 | Popular Tags |