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.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.UserDatabaseManager; 39 import com.sslexplorer.input.MultiSelectDataSource; 40 import com.sslexplorer.policyframework.DelegatedPoliciesDataSource; 41 import com.sslexplorer.policyframework.Policy; 42 import com.sslexplorer.policyframework.PolicyConstants; 43 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 44 import com.sslexplorer.policyframework.Principal; 45 import com.sslexplorer.policyframework.Resource; 46 import com.sslexplorer.policyframework.forms.PolicyForm; 47 import com.sslexplorer.properties.Property; 48 import com.sslexplorer.properties.attributes.AttributeDefinition; 49 import com.sslexplorer.properties.attributes.AttributeValueItem; 50 import com.sslexplorer.properties.impl.policyattributes.PolicyAttributeKey; 51 import com.sslexplorer.security.LogonControllerFactory; 52 import com.sslexplorer.security.SessionInfo; 53 import com.sslexplorer.security.User; 54 import com.sslexplorer.security.UserDatabase; 55 56 63 public class ShowPolicyDispatchAction extends AbstractResourceDispatchAction { 64 65 final static Log log = LogFactory.getLog(ShowPolicyDispatchAction.class); 66 67 70 public ShowPolicyDispatchAction() { 71 super(PolicyConstants.POLICY_RESOURCE_TYPE); 72 } 73 74 public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 75 throws Exception { 76 ActionForward fwd = super.commit(mapping, form, request, response); 77 PolicyForm policyForm = (PolicyForm) form; 78 Policy pol = (Policy) policyForm.getResource(); 79 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 80 if (pol.getResourceId() != PolicyDatabaseFactory.getInstance().getEveryonePolicyIDForRealm(udb.getRealm())) { 81 List wasAttached = PolicyDatabaseFactory.getInstance().getPrincipalsGrantedPolicy(pol, udb.getRealm()); List nowAttached = policyForm.getSelectedAccountsList(); 83 for (Iterator i = wasAttached.iterator(); i.hasNext();) { 84 Principal p = (Principal) i.next(); 85 try { 86 if (!nowAttached.contains(p.getPrincipalName())) { 87 CoreServlet.getServlet().fireCoreEvent( 88 new CoreEvent(this, CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL, null, null, 89 CoreEvent.STATE_SUCCESSFUL).addAttribute( 90 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, p instanceof User ? "user" : "group") 91 .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()) 92 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName())); 93 94 } 95 } catch (Exception e) { 96 CoreServlet.getServlet().fireCoreEvent( 97 new CoreEvent(this, CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL, null, null, e).addAttribute( 98 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, p instanceof User ? "user" : "group").addAttribute( 99 CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute( 100 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName())); 101 throw e; 102 } 103 } 104 105 PolicyDatabaseFactory.getInstance().revokePolicyFromAllPrincipals(pol, udb.getRealm()); 107 for (Iterator i = nowAttached.iterator(); i.hasNext();) { 108 Principal p = udb.getAccount((String ) i.next()); 109 try { 110 PolicyDatabaseFactory.getInstance().grantPolicyToPrincipal(pol, p); 111 CoreServlet.getServlet().fireCoreEvent( 112 new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_SUCCESSFUL) 113 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, 114 "user").addAttribute( 115 CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute( 116 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName())); 117 } catch (Exception e) { 118 CoreServlet.getServlet().fireCoreEvent( 119 new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, e).addAttribute( 120 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "user").addAttribute( 121 CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute( 122 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName())); 123 throw e; 124 } 125 } 126 for (Iterator i = policyForm.getSelectedRolesList().iterator(); i.hasNext();) { 127 Principal p = udb.getRole((String ) i.next()); 128 try { 129 PolicyDatabaseFactory.getInstance().grantPolicyToPrincipal(pol, p); 130 CoreServlet.getServlet().fireCoreEvent( 131 new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_SUCCESSFUL) 132 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "group") 133 .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()) 134 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName())); 135 } catch (Exception e) { 136 CoreServlet.getServlet().fireCoreEvent( 137 new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_UNSUCCESSFUL) 138 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "group") 139 .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()) 140 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName())); 141 throw e; 142 } 143 } 144 } 145 for(AttributeValueItem v : policyForm.getAttributeValueItems()) { 147 if(v.getDefinition().getVisibility() != AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 148 Property.setProperty(new PolicyAttributeKey(policyForm.getResourceId(), v.getDefinition().getName()), v.getDefinition().formatAttributeValue(v.getValue()), getSessionInfo(request)); 149 } 150 } 151 152 return fwd; 153 } 154 155 163 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 164 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 165 } 166 167 175 public Resource createResource(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 176 throws Exception { 177 throw new Exception ("Create resource not supported."); 178 } 179 180 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 181 ActionForward fwd = super.edit(mapping, form, request, response); 182 ((PolicyForm)form).initAttributes(request); 183 return fwd; 184 } 185 186 195 protected MultiSelectDataSource createAvailablePoliciesDataSource(Resource resource, ActionMapping mapping, ActionForm form, 196 HttpServletRequest request, HttpServletResponse response) 197 throws Exception { 198 return new DelegatedPoliciesDataSource((Policy) resource, PolicyConstants.POLICY_RESOURCE_TYPE, 199 PolicyConstants.SYSTEM_CLASS, getSessionInfo(request).getUser()); 200 } 201 202 @Override 203 protected void doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 204 super.doUpdate(mapping, form, request, response); 205 LogonControllerFactory.getInstance().applyMenuItemChanges(request); 207 } 208 209 } | Popular Tags |