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.Globals; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 import org.apache.struts.action.ActionMessages; 36 37 import com.sslexplorer.core.forms.CoreForm; 38 import com.sslexplorer.input.MultiSelectDataSource; 39 import com.sslexplorer.policyframework.AccessRight; 40 import com.sslexplorer.policyframework.AccessRights; 41 import com.sslexplorer.policyframework.DelegatedPoliciesDataSource; 42 import com.sslexplorer.policyframework.NoPermissionException; 43 import com.sslexplorer.policyframework.Permission; 44 import com.sslexplorer.policyframework.PolicyConstants; 45 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 46 import com.sslexplorer.policyframework.Resource; 47 import com.sslexplorer.policyframework.ResourceUtil; 48 import com.sslexplorer.policyframework.forms.AbstractResourceForm; 49 import com.sslexplorer.policyframework.forms.AccessRightsForm; 50 import com.sslexplorer.security.LogonControllerFactory; 51 import com.sslexplorer.security.SessionInfo; 52 53 54 60 public class AccessRightsDispatchAction extends AbstractResourceDispatchAction { 61 62 final static Log log = LogFactory.getLog(AccessRightsDispatchAction.class); 63 64 67 public AccessRightsDispatchAction() { 68 super(PolicyConstants.ACCESS_RIGHTS_RESOURCE_TYPE, null, null, null, null, null); 69 } 70 71 74 public Resource createResource(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 75 throws Exception { 76 throw new Exception ("Create not supported."); 77 } 78 79 89 public ActionForward addSelected(ActionMapping mapping, ActionForm form, HttpServletRequest request, 90 HttpServletResponse response) throws Exception { 91 AccessRightsForm drpf = (AccessRightsForm) form; 92 String [] sel = drpf.getSelectedAvailablePermissions(); 93 List l = drpf.getResourceTypePermissions(); 94 for (int i = 0; i < sel.length; i++) { 95 l.add(new AccessRight(drpf.getResourceType(), drpf.getResourceType().getPermission( 96 Integer.parseInt(sel[i])))); 97 } 98 drpf.rebuild(); 99 return display(mapping, form, request, response); 100 } 101 102 112 public ActionForward removeSelected(ActionMapping mapping, ActionForm form, HttpServletRequest request, 113 HttpServletResponse response) throws Exception { 114 AccessRightsForm drpf = (AccessRightsForm) form; 115 String [] sel = drpf.getSelectedResourceTypePermissions(); 116 if(sel != null) { 117 List l = drpf.getResourceTypePermissions(); 118 for (int i = 0; i < sel.length; i++) { 119 int idx = sel[i].indexOf('-'); 120 int resourceTypeId = Integer.parseInt(sel[i].substring(0, idx)); 121 int permissionId = Integer.parseInt(sel[i].substring(idx + 1)); 122 AccessRight sp = null; 123 for (Iterator j = l.iterator(); j.hasNext() && sp == null;) { 124 AccessRight p = (AccessRight) j.next(); 125 if (p.getResourceType().getResourceTypeId() == resourceTypeId && p.getPermission().getId() == permissionId) { 126 sp = p; 127 } 128 } 129 if (sp != null) { 130 l.remove(sp); 131 } 132 } 133 drpf.rebuild(); 134 } 135 return display(mapping, form, request, response); 136 } 137 138 148 public ActionForward selectResourceType(ActionMapping mapping, ActionForm form, HttpServletRequest request, 149 HttpServletResponse response) throws Exception { 150 AccessRightsForm drpf = (AccessRightsForm) form; 151 drpf.setResourceType(PolicyDatabaseFactory.getInstance().getResourceType(drpf.getSelectedResourceType())); 152 drpf.rebuild(); 153 return display(mapping, form, request, response); 154 } 155 156 159 public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 160 throws Exception { 161 ActionMessages msgs = new ActionMessages(); 162 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("editAccessRights.message.saved")); 163 this.addMessages(request, msgs); 164 return super.commit(mapping, form, request, response); 165 } 166 167 170 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 171 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 172 } 173 174 177 protected void checkValid(Resource r, Permission[] permissions, ActionMapping mapping, AbstractResourceForm form, 178 HttpServletRequest request) throws NoPermissionException { 179 ResourceUtil.checkAccessRightsValid((AccessRights)r, this.getSessionInfo(request), ((CoreForm) form).getActionTarget()); 180 } 181 182 185 protected MultiSelectDataSource createAvailablePoliciesDataSource(Resource resource, ActionMapping mapping, ActionForm form, 186 HttpServletRequest request, HttpServletResponse response) throws Exception { 187 return new DelegatedPoliciesDataSource(null, null, ((AccessRights) resource).getAccessRightsClass(), LogonControllerFactory.getInstance().getUser(request)); 188 } 189 190 @Override 191 protected void doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 192 super.doUpdate(mapping, form, request, response); 193 LogonControllerFactory.getInstance().applyMenuItemChanges(request); 195 } 196 197 } | Popular Tags |